import 'dart:math' as math; import 'package:flutter/material.dart'; class RingPainter extends CustomPainter { final double value; final Color color; final double strokeWidth; const RingPainter({ required this.value, required this.color, this.strokeWidth = 9.0, }); @override void paint(Canvas canvas, Size size) { final center = Offset(size.width / 2, size.height / 2); final radius = (math.min(size.width, size.height) - strokeWidth) / 2; canvas.drawCircle( center, radius, Paint() ..color = color.withValues(alpha: 0.12) ..style = PaintingStyle.stroke ..strokeWidth = strokeWidth, ); if (value > 0.005) { canvas.drawArc( Rect.fromCircle(center: center, radius: radius), -math.pi / 2, 2 * math.pi * value.clamp(0.0, 1.0), false, Paint() ..color = color ..style = PaintingStyle.stroke ..strokeWidth = strokeWidth ..strokeCap = StrokeCap.round, ); } } @override bool shouldRepaint(RingPainter old) => old.value != value || old.color != color || old.strokeWidth != strokeWidth; }