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});
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],