How to add line dash in Flutter

前端 未结 10 1008
清歌不尽
清歌不尽 2021-02-07 02:54

How to make a line dash in Flutter like this?

10条回答
  •  温柔的废话
    2021-02-07 03:11

    CustomPainter can help here as well. In this example is a vertical dash line but could be changed easily.

    class LineDashedPainter extends CustomPainter {
      @override
      void paint(Canvas canvas, Size size) {
        var paint = Paint()..strokeWidth = 2;
        var max = 35;
        var dashWidth = 5;
        var dashSpace = 5;
        double startY = 0;
        while (max >= 0) {
          canvas.drawLine(Offset(0, startY), Offset(0, startY + dashWidth), paint);
          final space = (dashSpace + dashWidth);
          startY += space;
          max -= space;
        }
      }
    
      @override
      bool shouldRepaint(CustomPainter oldDelegate) => false;
    }
    

    and that use CustomPaint Widget:

    CustomPaint(painter: LineDashedPainter())
    

提交回复
热议问题