flutter canvas 簡單繪畫直線

1. 定義一個classcanvas

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. 使用ide

Container(
    child:CustomPaint(
        foregroundPainter: new MyPainter(
             lineColor: Colors.lightBlueAccent,
             width: 8.0,
       ),
    ),
),
相關文章
相關標籤/搜索