55 lines
1.6 KiB
Dart
55 lines
1.6 KiB
Dart
import 'package:flutter/material.dart';
|
|||
|
|
|
||
|
|
/// The composer's context usage ring, colored by the official thresholds.
|
||
|
|
class UsageRing extends StatelessWidget {
|
||
|
|
final double pct; // 0..100
|
||
|
|
final double size;
|
||
|
|
final Color accent;
|
||
|
|
const UsageRing({super.key, required this.pct, this.size = 18, required this.accent});
|
||
|
|
|
||
|
|
Color get _color {
|
||
|
|
if (pct >= 90) return Colors.red.shade600;
|
||
|
|
if (pct >= 75) return Colors.orange.shade700;
|
||
|
|
return Colors.green.shade600;
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
Widget build(BuildContext context) {
|
||
|
|
return SizedBox(
|
||
|
|
width: size,
|
||
|
|
height: size,
|
||
|
|
child: CustomPaint(
|
||
|
|
painter: _RingPainter(pct: pct.clamp(0, 100), color: _color, bg: accent.withValues(alpha: 0.2)),
|
||
|
|
),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
class _RingPainter extends CustomPainter {
|
||
|
|
final double pct;
|
||
|
|
final Color color;
|
||
|
|
final Color bg;
|
||
|
|
_RingPainter({required this.pct, required this.color, required this.bg});
|
||
|
|
|
||
|
|
@override
|
||
|
|
void paint(Canvas canvas, Size size) {
|
||
|
|
final stroke = size.width * 0.14;
|
||
|
|
final rect = Offset.zero & size;
|
||
|
|
final paint = Paint()
|
||
|
|
..style = PaintingStyle.stroke
|
||
|
|
..strokeWidth = stroke
|
||
|
|
..color = bg;
|
||
|
|
canvas.drawArc(rect.deflate(stroke / 2), 0, 3.14159 * 2, false, paint);
|
||
|
|
final sweep = 3.14159 * 2 * (pct / 100);
|
||
|
|
final fg = Paint()
|
||
|
|
..style = PaintingStyle.stroke
|
||
|
|
..strokeWidth = stroke
|
||
|
|
..strokeCap = StrokeCap.round
|
||
|
|
..color = color;
|
||
|
|
canvas.drawArc(rect.deflate(stroke / 2), -3.14159 / 2, sweep, false, fg);
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
bool shouldRepaint(_RingPainter old) =>
|
||
|
|
old.pct != pct || old.color != color || old.bg != bg;
|
||
|
|
}
|