paint.dart
7.4 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import 'dart:math' show min, pi, asin;
import 'package:flutter/material.dart';
/// convert
extension NumToRadians on num {
/// toRadians
double toRadians() {
return toDouble() * (pi / 180.0);
}
/// toDegree
double toDegree() {
return toDouble() * (180 / pi);
}
}
/// painter for progress
class ProgressPainter extends CustomPainter {
///0 to max
final double sweepAngle;
/// circle stroke width
final double strokeWidth;
/// multi color
final List<List<Color>> progressColor;
/// base background color
final Color backgroundColor;
/// reverse
final bool reverse;
/// painter for progress
ProgressPainter({
this.sweepAngle = 360,
this.strokeWidth = 20,
this.reverse = false,
required this.progressColor,
required this.backgroundColor,
});
@override
void paint(Canvas canvas, Size size) {
double wh = min(size.width, size.height);
var centerX = wh / 2;
var centerY = wh / 2;
var radius = wh / 2 - strokeWidth / 2;
var rect = Rect.fromCenter(
center: Offset(centerX, centerY),
width: wh - strokeWidth,
height: wh - strokeWidth,
);
var backgroundPaint = Paint()
..strokeWidth = strokeWidth
..color = backgroundColor
..strokeCap = StrokeCap.butt
..style = PaintingStyle.stroke;
canvas.drawArc(
rect,
0.toRadians(),
360.toRadians(),
false,
backgroundPaint,
);
if (sweepAngle > 0) {
double capAngle = 0;
if (radius > 0) {
capAngle = asin((strokeWidth / 2) / radius) * 180 / pi;
}
int fullLaps = (sweepAngle / 360).floor();
double remainingAngle = sweepAngle % 360;
// Draw full laps first
for (int i = 0; i < fullLaps; i++) {
int colorIdx = i;
if (colorIdx > progressColor.length - 1) {
colorIdx = progressColor.length - 1;
}
var shader = SweepGradient(
startAngle: 0.toRadians(),
endAngle: 360.toRadians(),
colors: reverse
? progressColor[colorIdx].reversed.toList()
: progressColor[colorIdx],
).createShader(rect);
var lapPaint = Paint()
..strokeWidth = strokeWidth
..shader = shader
..strokeCap = StrokeCap.butt
..style = PaintingStyle.stroke;
canvas.save();
canvas.translate(centerX, centerY);
canvas.rotate((-90).toRadians());
canvas.translate(-centerX, -centerY);
canvas.drawArc(
rect,
0.toRadians(),
reverse ? -360.toRadians() : 360.toRadians(),
false,
lapPaint,
);
canvas.restore();
}
// Draw the remaining fractional lap
if (remainingAngle > 0.0) {
int colorIdx = fullLaps;
if (colorIdx > progressColor.length - 1) {
colorIdx = progressColor.length - 1;
}
var shader = SweepGradient(
startAngle: 0.toRadians(),
endAngle: 360.toRadians(),
colors: reverse
? progressColor[colorIdx].reversed.toList()
: progressColor[colorIdx],
).createShader(rect);
var lapPaint = Paint()
..strokeWidth = strokeWidth
..shader = shader
..strokeCap = StrokeCap.butt
..style = PaintingStyle.stroke;
double actualCapAngle = capAngle;
if (remainingAngle < 2 * capAngle) {
actualCapAngle = remainingAngle / 2;
}
double startAngle = actualCapAngle;
double drawnSweepAngle = remainingAngle - 2 * actualCapAngle;
if (drawnSweepAngle < 0) {
drawnSweepAngle = 0;
}
canvas.save();
canvas.translate(centerX, centerY);
canvas.rotate((-90).toRadians());
canvas.translate(-centerX, -centerY);
// Draw start cap for this fractional lap
canvas.save();
canvas.translate(centerX, centerY);
canvas
.rotate(reverse ? -startAngle.toRadians() : startAngle.toRadians());
canvas.drawArc(
Rect.fromCenter(
center: Offset(radius, 0),
width: strokeWidth,
height: strokeWidth,
),
0,
reverse ? 180.toRadians() : -180.toRadians(),
true,
Paint()
..color = reverse
? progressColor[colorIdx].last
: progressColor[colorIdx].first,
);
canvas.drawRect(
Rect.fromCenter(
center: Offset(radius, 0),
width: strokeWidth,
height: 1,
),
Paint()
..color = reverse
? progressColor[colorIdx].last
: progressColor[colorIdx].first,
);
canvas.restore();
// Draw the fractional arc
canvas.drawArc(
rect,
reverse ? -startAngle.toRadians() : startAngle.toRadians(),
reverse ? -drawnSweepAngle.toRadians() : drawnSweepAngle.toRadians(),
false,
lapPaint,
);
// Draw end cap for this fractional lap
final endAngle = startAngle + drawnSweepAngle;
canvas.save();
canvas.translate(centerX, centerY);
canvas.rotate(reverse ? -endAngle.toRadians() : endAngle.toRadians());
final endColor = getColorAtAngleOf360(
remainingAngle,
reverse
? progressColor[colorIdx].reversed.toList()
: progressColor[colorIdx],
);
final bool shouldDrawShadow =
sweepAngle > 360 || (remainingAngle > 355 && remainingAngle < 360);
if (shouldDrawShadow) {
//Draw arc shadow
var shadowPosition = Offset(
radius,
reverse ? -4 : 4,
);
final Paint shadowPaint = Paint()
..color = Colors.black.withOpacity(0.35)
..style = PaintingStyle.fill
..maskFilter = const MaskFilter.blur(BlurStyle.normal, 3);
canvas.drawCircle(shadowPosition, strokeWidth / 2, shadowPaint);
}
//Draw the ending arc
canvas.drawArc(
Rect.fromCenter(
center: Offset(radius, 0),
width: strokeWidth,
height: strokeWidth,
),
0,
reverse ? -180.toRadians() : 180.toRadians(),
true,
Paint()..color = endColor,
);
canvas.drawRect(
Rect.fromCenter(
center: Offset(radius, 0),
width: strokeWidth,
height: 1,
),
Paint()..color = endColor,
);
canvas.restore();
canvas.restore();
}
}
}
@override
bool shouldRepaint(covariant ProgressPainter oldDelegate) {
return sweepAngle != oldDelegate.sweepAngle;
}
/// get color by angle of a single 360 degree span
Color getColorAtAngleOf360(double angle, List<Color> colors) {
double fraction = angle / 360.0;
if (fraction > 1.0) fraction = 1.0;
if (fraction < 0.0) fraction = 0.0;
int segments = colors.length - 1;
if (segments <= 0) return colors.first;
double segmentFraction = fraction * segments;
int segmentIndex = segmentFraction.floor();
if (segmentIndex >= segments) segmentIndex = segments - 1;
double segmentInnerFraction = segmentFraction - segmentIndex;
Color startColor = colors[segmentIndex];
Color endColor = colors[segmentIndex + 1];
return Color.lerp(startColor, endColor, segmentInnerFraction)!;
}
}