flutter canvas 简单绘画直线

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,
       ),
    ),
),
posted @ 2019-07-17 17:34  He_John  阅读(2924)  评论(0编辑  收藏  举报