import 'dart:math' as math; import 'package:flutter/material.dart'; class PuffProgressRing extends StatelessWidget { final int currentDoses; final int totalDoses; final double size; const PuffProgressRing({ super.key, required this.currentDoses, required this.totalDoses, this.size = 170, }); Color _getStatusColor() { if (currentDoses <= 10) return const Color(0xFFEF4444); // 红色报警 if (currentDoses <= 20) return const Color(0xFFF59E0B); // 黄色预警 return const Color(0xFF10B981); // 绿色健康 } @override Widget build(BuildContext context) { final percent = totalDoses > 0 ? (currentDoses / totalDoses).clamp(0.0, 1.0) : 0.0; final color = _getStatusColor(); return SizedBox( width: size, height: size, child: Stack( alignment: Alignment.center, children: [ CustomPaint( size: Size(size, size), painter: _RingPainter( percent: percent, strokeWidth: 12, activeColor: color, trackColor: Colors.grey.shade100, ), ), Column( mainAxisSize: MainAxisSize.min, children: [ Text( '$currentDoses', style: TextStyle( fontSize: size * 0.28, fontWeight: FontWeight.w900, color: const Color(0xFF1E293B), height: 1.0, ), ), const SizedBox(height: 4), Text( '/ $totalDoses 喷剩余', style: TextStyle( fontSize: size * 0.08, fontWeight: FontWeight.w500, color: Colors.grey.shade500, ), ), ], ), ], ), ); } } class _RingPainter extends CustomPainter { final double percent; final double strokeWidth; final Color activeColor; final Color trackColor; _RingPainter({ required this.percent, required this.strokeWidth, required this.activeColor, required this.trackColor, }); @override void paint(Canvas canvas, Size size) { final center = Offset(size.width / 2, size.height / 2); final radius = (size.width - strokeWidth) / 2; // 背景底环 final trackPaint = Paint() ..color = trackColor ..style = PaintingStyle.stroke ..strokeWidth = strokeWidth; canvas.drawCircle(center, radius, trackPaint); // 活跃进度环 final activePaint = Paint() ..color = activeColor ..style = PaintingStyle.stroke ..strokeCap = StrokeCap.round ..strokeWidth = strokeWidth; const startAngle = -math.pi / 2; final sweepAngle = 2 * math.pi * percent; canvas.drawArc( Rect.fromCircle(center: center, radius: radius), startAngle, sweepAngle, false, activePaint, ); } @override bool shouldRepaint(covariant _RingPainter oldDelegate) { return oldDelegate.percent != percent || oldDelegate.activeColor != activeColor || oldDelegate.trackColor != trackColor; } }