Flutter 自定义画圆 画笔(Paint)、绘制弧(drawArc)

自定义画圆,通过外部传入修改圆的颜色显示

1.首先创建一个dart文件,命名为line_painter.dart

class LinePainter extends CustomPainter {
  Color colors ;
  LinePainter({required this.colors});
  //定义画笔
  late final Paint _paint = Paint()
    ..color = colors
    ..strokeCap = StrokeCap.square
    ..isAntiAlias = true
    ..strokeWidth = 3.0
    ..style = PaintingStyle
        .fill; //画笔样式有填充PaintingStyle.fill 及没有填充PaintingStyle.stroke 两种

  void paint(Canvas canvas, Size size) {
    //绘制圆 参数为中心点 半径 画笔
    canvas.drawCircle(const Offset(10,10), 5.0, _paint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}

2.在需要显示的地方加上入下代码即可

 CustomPaint(
    painter: LinePainter(colors: Colors.green),
    child: const Text(''),
)

猜你喜欢

转载自blog.csdn.net/as425017946/article/details/127487182