class WheelPainter extends CustomPainter {
Paint getColoredPaint(Color color) {
Paint paint = Paint();
paint.color = color;
return paint;
}
@override
void paint(Canvas canvas, Size size) {
//半径
double wheelSize = min(size.width, size.height) / 2;
//分成6份
double nbElem = 6;
//角度
double radius = (2 * pi) / nbElem;
//包裹饼图的矩形框 center:相对于原点的偏移量
Rect boundingRect = Rect.fromCircle(
center: Offset(wheelSize, wheelSize), radius: wheelSize);
//每次画1/6圆
canvas.drawArc(
boundingRect, 0, radius, true, getColoredPaint(Colors.orange));
canvas.drawArc(
boundingRect, radius, radius, true, getColoredPaint(Colors.green));
canvas.drawArc(
boundingRect, radius * 2, radius, true, getColoredPaint(Colors.red));
canvas.drawArc(
boundingRect, radius * 3, radius, true, getColoredPaint(Colors.blue));
canvas.drawArc(
boundingRect, radius * 4, radius, true, getColoredPaint(Colors.pink));
canvas.drawArc(boundingRect, radius * 5, radius, true,
getColoredPaint(Colors.deepOrange));
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
//判断是否需要重绘,简单做下比较
return oldDelegate != this;
}
}
class Cake extends StatelessWidget {
@override
Widget build(BuildContext context) {
//CustomPaint是用来承载自定义View的容器,需要自定义一个画笔,得继承自CustomPainter
return CustomPaint(
size: Size(200, 200),
painter: WheelPainter(),
);
}
}
MaterialApp(
title: 'Flutter Demo',//标题
theme: ThemeData(//设置主题
brightness: Brightness.dark,//设置明暗模式为暗色
accentColor: Colors.black,//(按钮)Widget前景色为黑色
primaryColor: Colors.cyan,//主色调为青色
iconTheme:IconThemeData(color: Colors.yellow),//设置icon主题色为黄色
textTheme: TextTheme(body1: TextStyle(color: Colors.red))//设置文本颜色为红色
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
// 新建主题
Theme(
data: ThemeData(iconTheme: IconThemeData(color: Colors.red)),
child: Icon(Icons.favorite)
);
// 继承主题
Theme(
data: Theme.of(context).copyWith(iconTheme: IconThemeData(color: Colors.green)),
child: Icon(Icons.feedback)
);
Container(
color: Theme.of(context).primaryColor,//容器背景色复用应用主题色
child: Text(
'Text with a background color',
style: Theme.of(context).textTheme.title,//Text组件文本样式复用应用文本样式
));
扫一扫
在手机上阅读