Refactored AqiGauge to consolidate status text and color logic into a single method, improving code readability and maintainability.

This commit is contained in:
Faris Armoush
2025-05-29 14:56:56 +03:00
parent b95f4063d9
commit 3d4c17214c

View File

@ -7,35 +7,15 @@ class AqiGauge extends StatelessWidget {
const AqiGauge({super.key, required this.aqi}); const AqiGauge({super.key, required this.aqi});
final double aqi; final double aqi;
static const _minRange = 0.0; static const _minRange = 0.0;
static const _goodRange = 50.0; static const _goodRange = 50.0;
static const _moderateRange = 100.0; static const _moderateRange = 100.0;
static const _maxRange = 150.0; static const _maxRange = 150.0;
String _getStatusText(double value) {
return switch (value) {
<= _goodRange => 'Good',
<= _moderateRange => 'Moderate',
_ => 'Poor',
};
}
Color _darkenColor(Color color) => Color.lerp(
color,
Colors.black.withValues(alpha: 0.8),
0.4,
)!;
Color _getStatusColor(double value) => switch (value) {
<= _goodRange => ColorsManager.goodGreen,
<= _moderateRange => ColorsManager.moderateYellow,
_ => ColorsManager.poorOrange,
};
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final status = _getStatusText(aqi); final (status, statusColor) = _getStatusData(aqi);
final statusColor = _getStatusColor(aqi);
return AnimatedRadialGauge( return AnimatedRadialGauge(
value: aqi, value: aqi,
debug: false, debug: false,
@ -119,4 +99,17 @@ class AqiGauge extends StatelessWidget {
), ),
); );
} }
(String status, Color color) _getStatusData(double value) {
return switch (value) {
<= _goodRange => ('Good', ColorsManager.goodGreen),
<= _moderateRange => ('Moderate', ColorsManager.moderateYellow),
_ => ('Poor', ColorsManager.poorOrange),
};
}
Color _darkenColor(Color color) {
final black = Colors.black.withValues(alpha: 0.8);
return Color.lerp(color, black, 0.4)!;
}
} }