arc_progress_widget.dart
13.7 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import 'dart:math';
import 'package:flutter/material.dart';
/// 弧形进度单环配置
class ArcProgressItem {
/// 进度比例 (0.0 ~ 1.0,支持超过 1.0,<= 0 或 null 表示无数据/未开始)
final double? ratio;
/// 高亮进度颜色
final Color color;
/// 底轨背景色
final Color backgroundColor;
/// 无数据时的底轨背景色 (若为空则使用 [backgroundColor])
final Color? noDataBackgroundColor;
const ArcProgressItem({
required this.ratio,
required this.color,
required this.backgroundColor,
this.noDataBackgroundColor,
});
}
/// 华为版开口朝下的彩虹弧形多环进度 Widget(支持平滑动画,无中心文字)
class ArcMultiProgressWidget extends StatefulWidget {
/// 尺寸宽高 (正方形)
final double size;
/// 环形线宽 (如果为 null,将根据环数自适应计算)
final double? strokeWidth;
/// 环与环之间的间距 (如果为 null,将根据环数自适应计算)
final double? gap;
/// 起始角度 (以度为单位,150° 表示从左下方开始)
final double startAngleDeg;
/// 总扫过角度 (以度为单位,240° 表示从左下顺时针扫至右下,底部开口 120°)
final double sweepTotalDeg;
/// 环项列表 (从最外环到最内环排序)
final List<ArcProgressItem> items;
/// 是否开启动画
final bool animate;
/// 动画时长
final Duration duration;
/// 动画曲线
final Curve curve;
/// 超过 1 圈时端头阴影颜色 (默认半透明黑)
final Color shadowColor;
/// 阴影模糊半径 (越大越发散,越小越清晰,默认 2.2)
final double shadowBlur;
/// 阴影顺时针偏移距离 (像素,默认 0.8)
final double shadowOffset;
/// 合环三角形宽度占 size 的比例 (默认 15.27 / 50 ≈ 0.3054)
final double triangleWidthRatio;
/// 合环三角形高度占 size 的比例 (默认 8.27 / 50 ≈ 0.1654)
final double triangleHeightRatio;
/// 合环三角形向下溢出底部的比例 (占 size 的比例,默认 0.0)
final double triangleBottomOverflowRatio;
/// 合环三角形圆角半径占 size 的比例 (默认 1.4 / 50 ≈ 0.028)
final double triangleCornerRadiusRatio;
const ArcMultiProgressWidget({
super.key,
required this.items,
this.size = 50,
this.strokeWidth,
this.gap,
this.startAngleDeg = 150,
this.sweepTotalDeg = 240,
this.animate = true,
this.duration = const Duration(milliseconds: 1000),
this.curve = Curves.easeOutCubic,
this.shadowColor = const Color.fromARGB(173, 0, 0, 0), // alpha ~ 0.35
this.shadowBlur = 2.2,
this.shadowOffset = 0.8,
this.triangleWidthRatio = 0.42,
this.triangleHeightRatio = 0.22,
this.triangleBottomOverflowRatio = 0.04,
this.triangleCornerRadiusRatio = 0.02,
});
@override
State<ArcMultiProgressWidget> createState() => _ArcMultiProgressWidgetState();
}
class _ArcMultiProgressWidgetState extends State<ArcMultiProgressWidget>
with SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> _animation;
late List<double> _currentProgressAngles;
List<double> get _targetProgressAngles {
final totalRad = widget.sweepTotalDeg * pi / 180.0;
return widget.items.map((item) {
if (item.ratio == null || item.ratio! <= 0) {
return 0.0;
}
return item.ratio! * totalRad;
}).toList();
}
@override
void initState() {
super.initState();
_currentProgressAngles = List.filled(widget.items.length, 0.0);
_controller = AnimationController(
vsync: this,
duration: widget.duration,
);
final targets = _targetProgressAngles;
_updateAnimation(_currentProgressAngles, targets);
}
@override
void didUpdateWidget(ArcMultiProgressWidget oldWidget) {
super.didUpdateWidget(oldWidget);
final oldTargets = oldWidget.items.map((e) => e.ratio ?? 0.0).toList();
final newTargets = widget.items.map((e) => e.ratio ?? 0.0).toList();
if (oldTargets.toString() != newTargets.toString()) {
_updateAnimation(_currentProgressAngles, _targetProgressAngles);
}
}
void _updateAnimation(List<double> fromAngles, List<double> toAngles) {
if (!widget.animate) {
setState(() {
_currentProgressAngles = List.from(toAngles);
});
return;
}
final startList = List<double>.from(fromAngles);
while (startList.length < toAngles.length) {
startList.add(0.0);
}
_controller.duration = widget.duration;
_animation = CurvedAnimation(parent: _controller, curve: widget.curve);
_controller.addListener(() {
final t = _animation.value;
setState(() {
_currentProgressAngles = List.generate(toAngles.length, (i) {
final start = i < startList.length ? startList[i] : 0.0;
final end = toAngles[i];
return start + (end - start) * t;
});
});
});
_controller.forward(from: 0);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final count = widget.items.length;
// 自动计算粗细和间距
final strokeWidth = widget.strokeWidth ?? (count <= 2 ? 4.5 : 3.6);
final gap = widget.gap ?? (count <= 2 ? 2.5 : 1.8);
final startAngleRad = widget.startAngleDeg * pi / 180.0;
final totalSweepRad = widget.sweepTotalDeg * pi / 180.0;
return SizedBox(
width: widget.size,
height: widget.size,
child: CustomPaint(
painter: _ArcMultiProgressPainter(
animatedProgresses: _currentProgressAngles,
items: widget.items,
strokeWidth: strokeWidth,
gap: gap,
startAngleRad: startAngleRad,
totalSweepRad: totalSweepRad,
shadowColor: widget.shadowColor,
shadowBlur: widget.shadowBlur,
shadowOffset: widget.shadowOffset,
triangleWidthRatio: widget.triangleWidthRatio,
triangleHeightRatio: widget.triangleHeightRatio,
triangleBottomOverflowRatio: widget.triangleBottomOverflowRatio,
triangleCornerRadiusRatio: widget.triangleCornerRadiusRatio,
),
),
);
}
}
class _ArcMultiProgressPainter extends CustomPainter {
final List<double> animatedProgresses;
final List<ArcProgressItem> items;
final double strokeWidth;
final double gap;
final double startAngleRad;
final double totalSweepRad;
final Color shadowColor;
final double shadowBlur;
final double shadowOffset;
final double triangleWidthRatio;
final double triangleHeightRatio;
final double triangleBottomOverflowRatio;
final double triangleCornerRadiusRatio;
_ArcMultiProgressPainter({
required this.animatedProgresses,
required this.items,
required this.strokeWidth,
required this.gap,
required this.startAngleRad,
required this.totalSweepRad,
required this.shadowColor,
required this.shadowBlur,
required this.shadowOffset,
required this.triangleWidthRatio,
required this.triangleHeightRatio,
required this.triangleBottomOverflowRatio,
required this.triangleCornerRadiusRatio,
});
@override
void paint(Canvas canvas, Size size) {
final center = Offset(size.width / 2, size.height / 2);
final count = items.length;
for (int i = 0; i < count; i++) {
final item = items[i];
final radius = (size.width - strokeWidth) / 2 - i * (strokeWidth + gap);
if (radius <= 0) continue;
final rect = Rect.fromCircle(center: center, radius: radius);
// 1. 绘制底轨 (Track)
final hasData = item.ratio != null && item.ratio! > 0;
final bgColor = (!hasData && item.noDataBackgroundColor != null)
? item.noDataBackgroundColor!
: item.backgroundColor;
final trackPaint = Paint()
..color = bgColor
..strokeWidth = strokeWidth
..style = PaintingStyle.stroke
..strokeCap = StrokeCap.round;
canvas.drawArc(rect, startAngleRad, totalSweepRad, false, trackPaint);
// 2. 绘制进度 (Progress)
final sweep = i < animatedProgresses.length ? animatedProgresses[i] : 0.0;
if (sweep <= 0.001) continue;
final fullLaps = (sweep / totalSweepRad).floor();
final remainingSweep = sweep - fullLaps * totalSweepRad;
final progressPaint = Paint()
..color = item.color
..strokeWidth = strokeWidth
..style = PaintingStyle.stroke
..strokeCap = StrokeCap.round;
// ① 如果至少满一圈,先绘制完整的底层圆弧
if (fullLaps >= 1) {
canvas.drawArc(
rect, startAngleRad, totalSweepRad, false, progressPaint);
}
// ② 绘制超出的部分(或第一圈未满的部分)
if (fullLaps >= 1) {
if (remainingSweep > 0.01) {
final currentEndAngle = startAngleRad + remainingSweep;
// 沿切线前进方向微小偏移
final tangentOffsetX = -shadowOffset * sin(currentEndAngle);
final tangentOffsetY = shadowOffset * cos(currentEndAngle);
final shadowCenter = Offset(
center.dx + radius * cos(currentEndAngle) + tangentOffsetX,
center.dy + radius * sin(currentEndAngle) + tangentOffsetY,
);
final capCenter = Offset(
center.dx + radius * cos(currentEndAngle),
center.dy + radius * sin(currentEndAngle),
);
// 构造当前圆环轨道的裁剪区域,严格限制阴影只在环槽内显示,绝不溢出到外圈或内圈
final trackClipPath = Path()
..addOval(Rect.fromCircle(
center: center, radius: radius + strokeWidth / 2))
..addOval(Rect.fromCircle(
center: center, radius: radius - strokeWidth / 2))
..fillType = PathFillType.evenOdd;
// 绘制被精准裁剪的自然阴影
canvas.save();
canvas.clipPath(trackClipPath);
final shadowPaint = Paint()
..color = shadowColor
..style = PaintingStyle.fill
..maskFilter = MaskFilter.blur(BlurStyle.normal, shadowBlur);
canvas.drawCircle(shadowCenter, strokeWidth / 2, shadowPaint);
canvas.restore();
// 绘制第二层圆弧(带圆角端头,压在阴影上)
canvas.drawArc(
rect, startAngleRad, remainingSweep, false, progressPaint);
// 显式绘制顶层端帽实心圆
final capFillPaint = Paint()
..color = item.color
..style = PaintingStyle.fill;
canvas.drawCircle(capCenter, strokeWidth / 2, capFillPaint);
}
} else {
// 第一圈未满,直接绘制
canvas.drawArc(rect, startAngleRad, sweep, false, progressPaint);
}
}
// 3. 当所有环都合环(>= 100%)时,在底部中央开口处绘制圆润的合环指示三角形
final isAllCompleted = items.isNotEmpty &&
List.generate(items.length, (idx) {
final sw =
idx < animatedProgresses.length ? animatedProgresses[idx] : 0.0;
return sw >= totalSweepRad - 0.01;
}).every((completed) => completed);
if (isAllCompleted && count > 0) {
// 按照 size 的比例计算宽度、高度、溢出与圆角
final triangleWidth = size.width * triangleWidthRatio;
final triangleHeight = size.height * triangleHeightRatio;
final halfWidth = triangleWidth / 2;
final outerRadius = (size.width - strokeWidth) / 2;
// 以最外环端点下边缘(合环底部最低点)为 0 基准
final ringBottomY =
center.dy + outerRadius * sin(startAngleRad) + strokeWidth / 2;
final overflow = size.height * triangleBottomOverflowRatio;
final bottomY = ringBottomY + overflow;
final topY = bottomY - triangleHeight;
final topPoint = Offset(center.dx, topY);
final rightPoint = Offset(center.dx + halfWidth, bottomY);
final leftPoint = Offset(center.dx - halfWidth, bottomY);
final cornerRadius =
size.width * (triangleCornerRadiusRatio ?? (1.4 / 50.0));
final roundedTrianglePath = _createRoundedTrianglePath(
top: topPoint,
right: rightPoint,
left: leftPoint,
cornerRadius: cornerRadius,
);
final trianglePaint = Paint()
..color = items.first.color
..style = PaintingStyle.fill;
canvas.drawPath(roundedTrianglePath, trianglePaint);
}
}
/// 绘制平滑圆角三角形
Path _createRoundedTrianglePath({
required Offset top,
required Offset right,
required Offset left,
required double cornerRadius,
}) {
final path = Path();
final points = [top, right, left];
final n = points.length;
for (int i = 0; i < n; i++) {
final p0 = points[(i - 1 + n) % n];
final p1 = points[i];
final p2 = points[(i + 1) % n];
final v1 = Offset(p0.dx - p1.dx, p0.dy - p1.dy);
final v2 = Offset(p2.dx - p1.dx, p2.dy - p1.dy);
final d1 = sqrt(v1.dx * v1.dx + v1.dy * v1.dy);
final d2 = sqrt(v2.dx * v2.dx + v2.dy * v2.dy);
final u1 = Offset(v1.dx / d1, v1.dy / d1);
final u2 = Offset(v2.dx / d2, v2.dy / d2);
final dot = (u1.dx * u2.dx + u1.dy * u2.dy).clamp(-1.0, 1.0);
final angle = acos(dot);
final cutDist = cornerRadius / tan(angle / 2);
final cut1 = Offset(p1.dx + u1.dx * cutDist, p1.dy + u1.dy * cutDist);
final cut2 = Offset(p1.dx + u2.dx * cutDist, p1.dy + u2.dy * cutDist);
if (i == 0) {
path.moveTo(cut1.dx, cut1.dy);
} else {
path.lineTo(cut1.dx, cut1.dy);
}
path.arcToPoint(
cut2,
radius: Radius.circular(cornerRadius),
clockwise: true,
);
}
path.close();
return path;
}
@override
bool shouldRepaint(covariant _ArcMultiProgressPainter oldDelegate) {
return true;
}
}