dashed_line.dart 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import 'package:flutter/material.dart';
  2. class DashedLine extends StatelessWidget {
  3. final double height;
  4. final Color color;
  5. final double dashWidth;
  6. final double dashSpace;
  7. const DashedLine({
  8. super.key,
  9. this.height = 1,
  10. this.color = Colors.black,
  11. this.dashWidth = 5,
  12. this.dashSpace = 3,
  13. });
  14. @override
  15. Widget build(BuildContext context) {
  16. return CustomPaint(
  17. size: Size(double.infinity, height),
  18. painter: _DashedLinePainter(
  19. color: color,
  20. dashWidth: dashWidth,
  21. dashSpace: dashSpace,
  22. strokeWidth: height,
  23. ),
  24. );
  25. }
  26. }
  27. class _DashedLinePainter extends CustomPainter {
  28. final Color color;
  29. final double dashWidth;
  30. final double dashSpace;
  31. final double strokeWidth;
  32. _DashedLinePainter({
  33. required this.color,
  34. required this.dashWidth,
  35. required this.dashSpace,
  36. required this.strokeWidth,
  37. });
  38. @override
  39. void paint(Canvas canvas, Size size) {
  40. final paint = Paint()
  41. ..color = color
  42. ..strokeWidth = strokeWidth;
  43. double startX = 0;
  44. while (startX < size.width) {
  45. canvas.drawLine(
  46. Offset(startX, 0),
  47. Offset(startX + dashWidth, 0),
  48. paint,
  49. );
  50. startX += dashWidth + dashSpace;
  51. }
  52. }
  53. @override
  54. bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
  55. }