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,
       ),
    ),
),


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM