There's no native API for drawing a dashed line in Flutter. An existing snippet allows drawing horizontal dashed lines but I can't find a snippet for drawing arbitrary dashed line from one point to another. There also exists a library called dash_painter that draws a dashed path. However, I'm only interested in drawing simple dashed lines. In particular, I'm looking for a snippet for drawing dashed lines that's similar to canvas.drawLine(Offset p1, Offset p2, Paint paint)
.
How to draw a dashed line in a CustomPainter in Flutter?
2.6k Views Asked by AlienKevin At
1
Here's a function for drawing a dashed line from point p1 to point p2 in a
CustomPainter
:Example usage: Draw a red dashed line from (0, 0) to (100, 100) with dash width of 6 and spacing of 4.
EDIT
this is a version that uses one
Canvas.drawPoints
method call:in the most simple form you can call it with
pattern: [20, 10]
but more complex patterns are possible:pattern: [20, 5, 5, 5]