-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchevron.dart
More file actions
111 lines (90 loc) · 2.26 KB
/
chevron.dart
File metadata and controls
111 lines (90 loc) · 2.26 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import 'package:flutter/material.dart';
/// chevron Icon
class Chevron extends StatelessWidget {
/// size of the canvas to draw a chevron on.
final Size size;
/// color of the lines.
final Color color;
/// width of chevron lines.
final double lineWidth;
/// Direction where the chevron point will be facing.
final ChevronDirection direction;
/// Constructor
const Chevron({
Key? key,
required this.size,
required this.color,
required this.lineWidth,
required this.direction,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return CustomPaint(
size: size,
painter: _ChevronPainter(
color: color,
lineWidth: lineWidth,
radians: _getRadiansForDirection(direction),
),
);
}
double _getRadiansForDirection(ChevronDirection direction) {
const double _pi = 3.1415;
const double _halfOfPi = _pi / 2;
return {
ChevronDirection.down: 0.0,
ChevronDirection.left: _halfOfPi,
ChevronDirection.up: _pi,
ChevronDirection.right: -_halfOfPi,
}[direction] ??
0.0;
}
}
/// The direction where the chevron arrow will be pointing.
enum ChevronDirection {
/// Chevron pointing upwards
up,
/// Chevron pointing to the right
right,
/// Chevron pointing downwards
down,
/// Chevron pointing to the left
left
}
class _ChevronPainter extends CustomPainter {
final double lineWidth;
final Color color;
final double radians;
/// constructor
_ChevronPainter({
required this.color,
required this.lineWidth,
required this.radians,
});
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = color
..style = PaintingStyle.stroke
..strokeWidth = lineWidth;
final halfWidth = size.width / 2;
final halfHeight = size.height / 2;
final halfLine = lineWidth / 2;
final x = halfLine / 2;
canvas.rotate(radians);
canvas.drawLine(
Offset.zero,
Offset(halfWidth + x, halfHeight + x),
paint,
);
canvas.drawLine(
Offset(size.width, 0),
Offset(halfWidth - x, halfHeight + x),
paint,
);
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return true;
}
}