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
|
import 'package:flutter_ics_homescreen/export.dart';
import 'dart:math' as math;
class AnimatedColorPainter extends CustomPainter {
final AnimationController animationController;
final double progress;
final Color progressColor; // New parameter for progress color
final Color backgroundColor;
final double strokeWidth;
AnimatedColorPainter(this.animationController, this.progress,
this.progressColor, this.backgroundColor, this.strokeWidth);
@override
void paint(Canvas canvas, Size size) {
// const strokeWidth = 25.0;
const borderWidth = 2.0;
// Divide the arc into equal parts based on the number of colors
const arcAngle = math.pi;
const arcPart = arcAngle / 3;
const gapAngle = arcAngle / 150;
// Calculate the current color index based on animation progress and progress value
final double normalizedProgress = progress * 3;
int currentColorIndex =
(animationController.value * normalizedProgress).floor();
if (progress == 0.0) {
currentColorIndex = -1; // Force background color when progress is 0
}
// Draw each part with a border and inner color
double startAngle = -math.pi; // Start from left
for (int i = 0; i < 3; i++) {
Color? currentColor = backgroundColor;
if (i <= currentColorIndex) {
// Use progress color if within progress range
currentColor = progressColor;
} else {
// Use background color if outside progress range
currentColor = backgroundColor;
}
// Draw border
final borderPaint = Paint()
..strokeWidth = strokeWidth + borderWidth
..style = PaintingStyle.stroke
..color = Colors.white12;
canvas.drawArc(
Rect.fromCircle(
center: Offset(size.width / 2, size.height / 2),
radius: size.width / 2,
),
startAngle,
arcPart - 2 * gapAngle,
false, // Draw clockwise
borderPaint,
);
// Draw inner color
final colorPaint = Paint()
..strokeWidth = strokeWidth
..style = PaintingStyle.stroke
..shader = _createColorShader(currentColor, size);
canvas.drawArc(
Rect.fromCircle(
center: Offset(size.width / 2, size.height / 2),
radius: size.width / 2,
),
startAngle,
arcPart - 2 * gapAngle,
false, // Draw clockwise
colorPaint,
);
startAngle += arcPart + gapAngle;
}
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
Shader _createColorShader(Color color, Size size) {
if (color == progressColor) {
return const RadialGradient(
center: Alignment.center,
radius: 2,
tileMode: TileMode.repeated,
focal: Alignment.center,
focalRadius: 8,
colors: [
AGLDemoColors.blueGlowFillColor,
AGLDemoColors.jordyBlueColor,
AGLDemoColors.neonBlueColor
],
).createShader(
Rect.fromCircle(
center: Offset(size.width / 2, size.height / 2),
radius: size.width / 2,
),
);
}
return LinearGradient(colors: [color, color]).createShader(
Rect.fromCircle(
center: Offset(size.width / 2, size.height / 2),
radius: size.width / 2,
),
);
}
}
|