flutter CustomPainter 简单绘制 三角形 多边形

本文详细介绍如何在Flutter中使用自定义坐标系统绘制三角形,包括创建坐标类、实现CustomPainter绘制过程,以及如何填充颜色。同时,文章还介绍了如何使用相同原理绘制多边形。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//自定义 坐标
class Coordinate {
final double cx;
final double cy;
Coordinate({this.cx, this.cy});
}

//绘制三角形。绘制的过程。是三个点依次线连接然后填充。点的坐标是相对父布局坐标 而不是绝对坐标(传统意义上的屏幕左上角)
效果图:
在这里插入图片描述
三角形代码:
class TriangleCustomPainter extends CustomPainter {
Paint _paint = new Paint();//画笔富含各种属性方法。仔细查看源码
final BuildContext context;
final List spots;
final Color color;

TriangleCustomPainter(this.context, this.spots, {this.color});

@override
void paint(Canvas canvas, Size size) {
Path path = new Path()…moveTo(spots[0].cx, spots[0].cy);
path.lineTo(spots[1].cx, spots[1].cy);
path.lineTo(spots[2].cx, spots[2].cy);
canvas.drawPath(
path,
_paint
…style = PaintingStyle.fill
…color = color);
}

@override
bool shouldRepaint(CustomPainter oldDelegate) {

return true;

}
}

使用:
List leftSpots = new List(3);
leftSpots[0] = Coordinate(cx: 10, cy: 0);
leftSpots[1] = Coordinate(cx: 22, cy: 6);
leftSpots[2] = Coordinate(cx: 22, cy: -6);
CustomPaint(
painter: new TriangleCustomPainter(context, leftSpots,
color: Colors.red),
),

同理。多边形也是如此。

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值