1. 定义一个class
class MyPainter extends CustomPainter { Color lineColor; double width; MyPainter({ this.lineColor, this.width}); @override void paint(Canvas canvas, Size size) { Paint _paint = new Paint() ..color = Colors.blueAccent ..strokeCap = StrokeCap.round ..isAntiAlias = true ..strokeWidth = 5.0 ..style = PaintingStyle.stroke; canvas.drawLine(Offset(20.0, 20.0), Offset(100.0, 100.0), _paint); } @override bool shouldRepaint(CustomPainter oldDelegate) => false;}
2. 使用
Container( child:CustomPaint( foregroundPainter: new MyPainter( lineColor: Colors.lightBlueAccent, width: 8.0, ), ),),