Skip to content

Instantly share code, notes, and snippets.

@divyanshub024
Last active April 5, 2020 13:45
Show Gist options
  • Save divyanshub024/dbae28246a2a5ffd881d4a84bc744981 to your computer and use it in GitHub Desktop.
Save divyanshub024/dbae28246a2a5ffd881d4a84bc744981 to your computer and use it in GitHub Desktop.
class DashLinePainter extends CustomPainter {
final double progress;
DashLinePainter({this.progress});
Paint _paint = Paint()
..color = Colors.black
..strokeWidth = 4.0
..style = PaintingStyle.stroke
..strokeJoin = StrokeJoin.round;
@override
void paint(Canvas canvas, Size size) {
var path = Path()
..moveTo(0, size.height / 2)
..lineTo(size.width * progress, size.height / 2);
Path dashPath = Path();
double dashWidth = 10.0;
double dashSpace = 5.0;
double distance = 0.0;
for (PathMetric pathMetric in path.computeMetrics()) {
while (distance < pathMetric.length) {
dashPath.addPath(
pathMetric.extractPath(distance, distance + dashWidth),
Offset.zero,
);
distance += dashWidth;
distance += dashSpace;
}
}
canvas.drawPath(dashPath, _paint);
}
@override
bool shouldRepaint(DashLinePainter oldDelegate) {
return oldDelegate.progress != progress;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment