dash_divider.dart
1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import 'package:flutter/material.dart';
class DashedDivider extends StatelessWidget {
const DashedDivider({
super.key,
this.color = const Color(0xFFD9D9D9),
this.height = 1,
this.dashWidth = 2,
this.gap = 2,
});
final Color color;
final double height;
final double dashWidth;
final double gap;
@override
Widget build(BuildContext context) {
return SizedBox(
height: height,
width: double.infinity,
child: CustomPaint(
painter: _DashedLinePainter(
color: color,
strokeWidth: height,
dashWidth: dashWidth,
gap: gap,
),
),
);
}
}
class _DashedLinePainter extends CustomPainter {
_DashedLinePainter({
required this.color,
required this.strokeWidth,
required this.dashWidth,
required this.gap,
});
final Color color;
final double strokeWidth;
final double dashWidth;
final double gap;
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = color
..strokeWidth = strokeWidth
..style = PaintingStyle.stroke;
final y = size.height / 2;
var x = 0.0;
while (x < size.width) {
final x2 = (x + dashWidth).clamp(0.0, size.width).toDouble();
canvas.drawLine(Offset(x, y), Offset(x2, y), paint);
x += dashWidth + gap;
}
}
@override
bool shouldRepaint(covariant _DashedLinePainter oldDelegate) {
return oldDelegate.color != color ||
oldDelegate.strokeWidth != strokeWidth ||
oldDelegate.dashWidth != dashWidth ||
oldDelegate.gap != gap;
}
}