本文将向您展示如何使用Flutter 中的**CustomClipper类绘制n 角星**(5 角星、6 角星、10 角星、20 角星等)。无需安装任何第三方软件包。我们将从头开始制作东西。

重点是什么?

1.通过扩展CustomClipper类实现一个可重用的自定义裁剪器:

// This custom clipper help us achieve n-pointed star shape
class StarClipper extends CustomClipper<Path> {/// The number of points of the starfinal int points;StarClipper(this.points);// Degrees to radians conversiondouble _degreeToRadian(double deg) => deg * (math.pi / 180.0);@overridePath getClip(Size size) {Path path = Path();double max = 2 * math.pi;double width = size.width;double halfWidth = width / 2;double wingRadius = halfWidth;double radius = halfWidth / 2;double degreesPerStep = _degreeToRadian(360 / points);double halfDegreesPerStep = degreesPerStep / 2;path.moveTo(width, halfWidth);for (double step = 0; step < max; step += degreesPerStep) {path.lineTo(halfWidth + wingRadius * math.cos(step),halfWidth + wingRadius * math.sin(step));path.lineTo(halfWidth + radius * math.cos(step + halfDegreesPerStep),halfWidth + radius * math.sin(step + halfDegreesPerStep));}path.close();return path;}// If the new instance represents different information than the old instance, this method will return true, otherwise it should return false.@overridebool shouldReclip(CustomClipper<Path> oldClipper) {StarClipper starClipper = oldClipper as StarClipper;return points != starClipper.points;}
}
  1. 现在您可以轻松绘制星星了,如下所示:
 SizedBox(height: 360,width: 360,child: ClipPath(clipper: StarClipper(6),child: Container(height: 300,color: Colors.amber,),),),

完整示例

预览

此示例生成 4 种不同的星形:5 角星、6 角星、10 角星和 20 角星。

编码

main.dart 中的完整源代码和解释:

// main.dart
import 'package:flutter/material.dart';
import 'dart:math' as math;// This custom clipper help us achieve n-pointed star shape
class StarClipper extends CustomClipper<Path> {/// The number of points of the starfinal int points;StarClipper(this.points);// Degrees to radians conversiondouble _degreeToRadian(double deg) => deg * (math.pi / 180.0);@overridePath getClip(Size size) {Path path = Path();double max = 2 * math.pi;double width = size.width;double halfWidth = width / 2;double wingRadius = halfWidth;double radius = halfWidth / 2;double degreesPerStep = _degreeToRadian(360 / points);double halfDegreesPerStep = degreesPerStep / 2;path.moveTo(width, halfWidth);for (double step = 0; step < max; step += degreesPerStep) {path.lineTo(halfWidth + wingRadius * math.cos(step),halfWidth + wingRadius * math.sin(step));path.lineTo(halfWidth + radius * math.cos(step + halfDegreesPerStep),halfWidth + radius * math.sin(step + halfDegreesPerStep));}path.close();return path;}// If the new instance represents different information than the old instance, this method will return true, otherwise it should return false.@overridebool shouldReclip(CustomClipper<Path> oldClipper) {StarClipper starClipper = oldClipper as StarClipper;return points != starClipper.points;}
}void main() {runApp(const MyApp());
}class MyApp extends StatelessWidget {const MyApp({Key? key}) : super(key: key);@overrideWidget build(BuildContext context) {return MaterialApp(debugShowCheckedModeBanner: false,title: 'KindaCode.com',theme: ThemeData(primarySwatch: Colors.indigo,),home: const MyHomePage(),);}
}class MyHomePage extends StatefulWidget {const MyHomePage({Key? key}) : super(key: key);@override_MyHomePageState createState() => _MyHomePageState();
}class _MyHomePageState extends State<MyHomePage> {@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: const Text('Kindacode.com'),),body: Padding(padding: const EdgeInsets.all(20),child: Center(child: Column(children: [// 5-pointed starSizedBox(height: 180,width: 180,child: ClipPath(clipper: StarClipper(5),child: Container(height: 150,color: Colors.green,),),),// 6-pointed starSizedBox(height: 180,width: 180,child: ClipPath(clipper: StarClipper(6),child: Container(height: 150,color: Colors.amber,),),),// 10-pointed starSizedBox(height: 180,width: 180,child: ClipPath(clipper: StarClipper(10),child: Container(height: 150,color: Colors.indigo,),),),// 20-pointed starSizedBox(height: 180,width: 180,child: ClipPath(clipper: StarClipper(20),child: Container(height: 150,color: Colors.cyan,),),),],),),),);}
}

结论

我们从头开始绘制自定义星形,没有使用任何第三个插件。如果您想在现代 Flutter 中探索更多新奇有趣的东西

Flutter:使用 CustomClipper 绘制 N 角星相关推荐

  1. Python如何绘制六角星

    这是第十一届蓝桥杯的一道考题,绘制一个中间是六边形的六角星. 今天我教一下大家怎么用turtle库绘制图形. import  turtle t=turtle.Pen() t.hideturtle() ...

  2. AUTOCAD——跨文件原位粘贴命令、如何用CAD绘制六角星

    ** 跨文件原位粘贴命令 ** 将一个文件中图形带固定基点复制到另一个文件中去. 执行方式 1.选择复制对象. 2.输入跨文件原位粘贴命令"copybase"或"Ctrl ...

  3. Python实现绘制多角星

    引言 在具备一定的Python编程基础以后,我们可以结合for循环进行多角星的编写,只要简单的几次循环,即可以极大的解决重复编写相同代码方面的问题,下面小编将以三角星.五角星为例,进而引入如何绘制多角 ...

  4. Python绘制六角星、多角星、小太阳、小风车《打包好的各种游戏源码,画图源码》

    绘制如下图的,多角图形.思路. (1)每个角是一个标准的等边三角形,把绘制等边三角形作为一个标准函数. (2)观察图形,可以看出,画的三角形在不断的旋转和移动,因此第一步找到三角形画法起始点的海龟头旋 ...

  5. 使用OpenGL绘制六角星

    使用OpenGL绘制凸多边形时,不能直接使用GL_POLYGON来绘制:于是,我们便想到使用GL_LINE_LOOP来绘制凸多边形的边界 我们的六角星是凸多边形,可以使用GL_LINE_LOOP来绘制 ...

  6. python绘制六角星外廓_Python之OpenGL笔记(32):正交投影画六角星

    一.目的 1.摄像机应用,正交投影画六角星: 二.程序运行结果 三.摄像机的设置 吴亚峰<OpenGL ES 3.x游戏开发>(上卷)内容 从日常生活的经验中可以很容易地了解到,随着摄像机 ...

  7. python绘制六角星_python画五角星和六角星程序 | 学步园

    1.五角星 import turtle turtle.forward(100) turtle.right(144) turtle.forward(100) turtle.right(144) turt ...

  8. python循环绘制六角星_python画五角星和六角星程序

    1.五角星 import turtle turtle.forward(100) turtle.right(144) turtle.forward(100) turtle.right(144) turt ...

  9. Python turtle 绘制六角星、多角星、小太阳

    绘制如下图的,多角图形.思路. (1)每个角是一个标准的等边三角形,把绘制等边三角形作为一个标准函数. (2)观察图形,可以看出,画的三角形在不断的旋转和移动,因此第一步找到三角形画法起始点的海龟头旋 ...

最新文章

  1. C# 利用net 命令获取域用户列表
  2. PHP explode() 函数
  3. Documentum中关于日期时间类型字段的特殊处理
  4. element中upload单图片转base64后添加进数组,请求接口
  5. vue实现HTML转PDF (已解决清晰、页边距、图片跨域导出等问题)
  6. GitHub 最受欢迎的Top 20 JavaScript 项目
  7. Java PipedInputStream connect()方法与示例
  8. everything 中文免安装_GTA5中文免安装版
  9. 中小型网络工程设计与实现_小型网络如何实现经济可靠的设计和部署 (一)...
  10. 无法安装数据库关系图支持对象的解决方法
  11. Centos安装Kafka集群
  12. html 制作静态页面新知识
  13. UCDOS和鲍岳桥and 我和我的嵌入式GUI(DOS,Linux,uC/OS-II等)
  14. PHP(gzdeflate/gzinflate)+JS(pako)前后端数据压缩
  15. ”周期天王”20大预言,针针见血,看完整个人通透多了
  16. Win10电脑浏览器连不上网
  17. 基于单幅图像Patch Map的稳健除雾(PMS-Net: Robust Haze Removal Based on Patch Map for Single Images_CVPR_2019)
  18. 小程序后台持续定位功能
  19. ESD元器件防护原理及选型
  20. 相机计算坐标公式_相机采样点的坐标转换方法与流程

热门文章

  1. json java 实例_java JSON数据转对象
  2. pb 如何判断缺纸_如何快速判断是否低估?四种相对估值法应用精析
  3. osg 倾斜数据纹理_高科技构筑逼真效果——无人机倾斜摄影技术在实景三维建模的应用及展望...
  4. mysql 创建外键实例_mysql 外键创建实例
  5. 博通高通迈威尔螃蟹全志南方硅谷WiFi本质的区别
  6. C语言函数sscanf()的用法
  7. 主成分分析法怎么提取图片中的字_在主成分分析里,如何提取主成分
  8. vue调用接口获取后台数据_Vuex 存储||获取后台接口数据
  9. 扩展方法必须在非泛型静态类中定义_第11篇:Cython面向对象编程--扩展类的实例化...
  10. 微型计算机 2018 5月,现在是2018年,我的电脑最近总是弹出说有重要更新,提示自2017年5月9日后,microsoft - Microsoft Community...