From 5d3ef95cb7d13a74d78d914f5a02ca6c2ad55e92 Mon Sep 17 00:00:00 2001 From: Faris Armoush Date: Wed, 28 May 2025 15:30:12 +0300 Subject: [PATCH] Refactor `AqiGauge` to use constants for range values, to allow for ease of change, and readability. --- .../air_quality/widgets/aqi_gauge.dart | 39 ++++++++++++------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/lib/pages/analytics/modules/air_quality/widgets/aqi_gauge.dart b/lib/pages/analytics/modules/air_quality/widgets/aqi_gauge.dart index 5d284afb..a287e8cd 100644 --- a/lib/pages/analytics/modules/air_quality/widgets/aqi_gauge.dart +++ b/lib/pages/analytics/modules/air_quality/widgets/aqi_gauge.dart @@ -7,24 +7,33 @@ class AqiGauge extends StatelessWidget { const AqiGauge({super.key, required this.aqi}); final double aqi; + static const _minRange = 0.0; + static const _goodRange = 50.0; + static const _moderateRange = 100.0; + static const _poorRange = 150.0; + static const _unhealthyToSevereRange = 225.0; + static const _maxRange = 300.0; Color _getPointerColor(double value) { - if (value <= 50) { + if (value <= _goodRange) { return ColorsManager.goodGreen; - } else if (value <= 100) { + } else if (value <= _moderateRange) { return ColorsManager.moderateYellow; - } else if (value <= 150) { + } else if (value <= _poorRange) { return ColorsManager.poorOrange; } else { - if (value <= 225) { - final t = (value - 151) / (225 - 151); + const unhealthyStart = _poorRange + 1; + if (value <= _unhealthyToSevereRange) { + final t = + (value - unhealthyStart) / (_unhealthyToSevereRange - unhealthyStart); return Color.lerp( ColorsManager.unhealthyRed, ColorsManager.severePink, t, )!; } else { - final t = (value - 226) / (300 - 226); + const severeStart = _unhealthyToSevereRange + 1; + final t = (value - severeStart) / (_maxRange - severeStart); return Color.lerp( ColorsManager.severePink, ColorsManager.hazardousPurple, @@ -81,7 +90,7 @@ class AqiGauge extends StatelessWidget { segmentSpacing: 4, ), min: 0, - max: 300, + max: _maxRange, pointer: GaugePointer.circle( position: const GaugePointerPosition.surface(), radius: MediaQuery.sizeOf(context).width * 0.004, @@ -97,26 +106,26 @@ class AqiGauge extends StatelessWidget { ), segments: [ const GaugeSegment( - from: 0, - to: 50, + from: _minRange, + to: _goodRange, cornerRadius: Radius.circular(16), color: ColorsManager.goodGreen, ), const GaugeSegment( - from: 51, - to: 100, + from: _goodRange + 1, + to: _moderateRange, cornerRadius: Radius.circular(16), color: ColorsManager.moderateYellow, ), const GaugeSegment( - from: 101, - to: 150, + from: _moderateRange + 1, + to: _poorRange, cornerRadius: Radius.circular(16), color: ColorsManager.poorOrange, ), const GaugeSegment( - from: 151, - to: 300, + from: _poorRange + 1, + to: _maxRange, cornerRadius: Radius.circular(16), gradient: GaugeAxisGradient( colorStops: [0.0, 0.5, 1.0],