日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Flutter:使用 CustomClipper 绘制 N 角星

發(fā)布時(shí)間:2025/3/19 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Flutter:使用 CustomClipper 绘制 N 角星 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本文將向您展示如何使用Flutter 中的**CustomClipper類繪制n 角星**(5 角星、6 角星、10 角星、20 角星等)。無需安裝任何第三方軟件包。我們將從頭開始制作東西。

重點(diǎn)是什么?

1.通過擴(kuò)展CustomClipper類實(shí)現(xiàn)一個(gè)可重用的自定義裁剪器:

// 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;} }
  • 現(xiàn)在您可以輕松繪制星星了,如下所示:
  • SizedBox(height: 360,width: 360,child: ClipPath(clipper: StarClipper(6),child: Container(height: 300,color: Colors.amber,),),),

    完整示例

    預(yù)覽

    此示例生成 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,),),),],),),),);} }

    結(jié)論

    我們從頭開始繪制自定義星形,沒有使用任何第三個(gè)插件。如果您想在現(xiàn)代 Flutter 中探索更多新奇有趣的東西

    總結(jié)

    以上是生活随笔為你收集整理的Flutter:使用 CustomClipper 绘制 N 角星的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。