Refactor AqiGauge to use constants for range values, to allow for ease of change, and readability.

This commit is contained in:
Faris Armoush
2025-05-28 15:30:12 +03:00
parent a87b11d084
commit 5d3ef95cb7

View File

@ -7,24 +7,33 @@ 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 _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) { Color _getPointerColor(double value) {
if (value <= 50) { if (value <= _goodRange) {
return ColorsManager.goodGreen; return ColorsManager.goodGreen;
} else if (value <= 100) { } else if (value <= _moderateRange) {
return ColorsManager.moderateYellow; return ColorsManager.moderateYellow;
} else if (value <= 150) { } else if (value <= _poorRange) {
return ColorsManager.poorOrange; return ColorsManager.poorOrange;
} else { } else {
if (value <= 225) { const unhealthyStart = _poorRange + 1;
final t = (value - 151) / (225 - 151); if (value <= _unhealthyToSevereRange) {
final t =
(value - unhealthyStart) / (_unhealthyToSevereRange - unhealthyStart);
return Color.lerp( return Color.lerp(
ColorsManager.unhealthyRed, ColorsManager.unhealthyRed,
ColorsManager.severePink, ColorsManager.severePink,
t, t,
)!; )!;
} else { } else {
final t = (value - 226) / (300 - 226); const severeStart = _unhealthyToSevereRange + 1;
final t = (value - severeStart) / (_maxRange - severeStart);
return Color.lerp( return Color.lerp(
ColorsManager.severePink, ColorsManager.severePink,
ColorsManager.hazardousPurple, ColorsManager.hazardousPurple,
@ -81,7 +90,7 @@ class AqiGauge extends StatelessWidget {
segmentSpacing: 4, segmentSpacing: 4,
), ),
min: 0, min: 0,
max: 300, max: _maxRange,
pointer: GaugePointer.circle( pointer: GaugePointer.circle(
position: const GaugePointerPosition.surface(), position: const GaugePointerPosition.surface(),
radius: MediaQuery.sizeOf(context).width * 0.004, radius: MediaQuery.sizeOf(context).width * 0.004,
@ -97,26 +106,26 @@ class AqiGauge extends StatelessWidget {
), ),
segments: [ segments: [
const GaugeSegment( const GaugeSegment(
from: 0, from: _minRange,
to: 50, to: _goodRange,
cornerRadius: Radius.circular(16), cornerRadius: Radius.circular(16),
color: ColorsManager.goodGreen, color: ColorsManager.goodGreen,
), ),
const GaugeSegment( const GaugeSegment(
from: 51, from: _goodRange + 1,
to: 100, to: _moderateRange,
cornerRadius: Radius.circular(16), cornerRadius: Radius.circular(16),
color: ColorsManager.moderateYellow, color: ColorsManager.moderateYellow,
), ),
const GaugeSegment( const GaugeSegment(
from: 101, from: _moderateRange + 1,
to: 150, to: _poorRange,
cornerRadius: Radius.circular(16), cornerRadius: Radius.circular(16),
color: ColorsManager.poorOrange, color: ColorsManager.poorOrange,
), ),
const GaugeSegment( const GaugeSegment(
from: 151, from: _poorRange + 1,
to: 300, to: _maxRange,
cornerRadius: Radius.circular(16), cornerRadius: Radius.circular(16),
gradient: GaugeAxisGradient( gradient: GaugeAxisGradient(
colorStops: [0.0, 0.5, 1.0], colorStops: [0.0, 0.5, 1.0],