gpt4 book ai didi

android - Flutter:如何画星星

转载 作者:行者123 更新时间:2023-12-05 00:10:30 26 4
gpt4 key购买 nike

我正在尝试自定义我的容器形状,使其看起来像这样:

enter image description here

我试着用 customPaint 来做,但我不太了解这个小部件,所以我需要帮助。

我怎样才能画出这样的形状? customPaint 是正确的解决方案吗?

最佳答案

您可以在下面复制粘贴运行完整代码
包的修改代码 https://pub.dev/packages/flutter_custom_clippers
StarClipper https://github.com/lohanidamodar/flutter_custom_clippers/blob/master/lib/src/star_clipper.dart

代码 fragment

class StarClipper extends CustomClipper<Path>
@override
Path getClip(Size size) {
...
double radius = halfWidth / 1.3;
...
Container(
height: 200,
width: 200,
child: ClipPath(
clipper: StarClipper(14),
child: Container(
height: 150,
color: Colors.green[500],
child: Center(child: Text("+6", style: TextStyle(fontSize: 50),)),
),
),
),

工作演示

enter image description here

完整代码

import 'package:flutter/material.dart';
import 'dart:math' as math;

class StarClipper extends CustomClipper<Path> {
StarClipper(this.numberOfPoints);

/// The number of points of the star
final int numberOfPoints;

@override
Path getClip(Size size) {
double width = size.width;
print(width);
double halfWidth = width / 2;

double bigRadius = halfWidth;

double radius = halfWidth / 1.3;

double degreesPerStep = _degToRad(360 / numberOfPoints);

double halfDegreesPerStep = degreesPerStep / 2;

var path = Path();

double max = 2 * math.pi;

path.moveTo(width, halfWidth);

for (double step = 0; step < max; step += degreesPerStep) {
path.lineTo(halfWidth + bigRadius * math.cos(step),
halfWidth + bigRadius * math.sin(step));
path.lineTo(halfWidth + radius * math.cos(step + halfDegreesPerStep),
halfWidth + radius * math.sin(step + halfDegreesPerStep));
}

path.close();
return path;
}

num _degToRad(num deg) => deg * (math.pi / 180.0);

@override
bool shouldReclip(CustomClipper<Path> oldClipper) {
StarClipper oldie = oldClipper as StarClipper;
return numberOfPoints != oldie.numberOfPoints;
}
}


void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;

void _incrementCounter() {
setState(() {
_counter++;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
height: 200,
width: 200,
child: ClipPath(
clipper: StarClipper(14),
child: Container(
height: 150,
color: Colors.green[500],
child: Center(child: Text("+6", style: TextStyle(fontSize: 50),)),
),
),
),

],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}

关于android - Flutter:如何画星星,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63700728/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com