Line data Source code
1 : import 'package:flutter/widgets.dart';
2 : import 'package:fossui/src/foundation/foss_since.dart';
3 : import 'package:fossui/src/theme/theme.dart';
4 :
5 : part 'foss_meter_style.dart';
6 :
7 : // The track is a fixed-height bar; its height is a component constant, not a
8 : // token (nothing else references it). It runs two px taller than the progress
9 : // track: the two are siblings, not the same widget. The fill eases over its
10 : // width with a plain ease; curves are widget constants, not tokens.
11 : const double _trackHeight = 8;
12 : const Curve _fillCurve = Curves.ease;
13 :
14 : /// {@category Feedback}
15 : /// {@template foss.meter.preview}
16 : /// <img src="https://fossui.org/components/meter/overview/light.png"
17 : /// alt="FossMeter, light theme" width="480"
18 : /// style="max-width:100%;height:auto" />
19 : /// <img src="https://fossui.org/components/meter/overview/dark.png"
20 : /// alt="FossMeter, dark theme" width="480"
21 : /// style="max-width:100%;height:auto" />
22 : ///
23 : /// See the [meter documentation ↗](https://fossui.org/docs/components/meter)
24 : /// or try it live in the
25 : /// [playground ↗](https://play.fossui.org/components/#/?path=components/meter/fossmeter/playground).
26 : /// {@endtemplate}
27 : ///
28 : /// A static gauge: a full-width track with a leading fill that shows one
29 : /// measurement inside a fixed range (disk used, a quota, a rating). It is the
30 : /// display-only sibling of the progress bar: bounded and non-interactive.
31 : ///
32 : /// [value] sits in `[min, max]` (default `0..100`) and is clamped on input; the
33 : /// fill fraction is `(value - min) / (max - min)`. An optional [label] sits at
34 : /// the start of a row above the track and the value at the end; the row renders
35 : /// when [label] is set or [showValue] is true. [formatValue] builds the
36 : /// right-hand text (default a percentage of the range); pass one for a raw or
37 : /// unit form. The fill animates its width over the `progress` motion token and
38 : /// jumps under reduced motion. Colors, type, and radius come from
39 : /// `context.fossTheme`; pass a [FossMeterStyle] for a one-off override.
40 : ///
41 : /// {@macro foss.customize}
42 : ///
43 : /// ```dart
44 : /// FossMeter(
45 : /// value: 40,
46 : /// label: 'Storage',
47 : /// );
48 : /// ```
49 : @FossSince('0.1.1')
50 : class FossMeter extends StatelessWidget {
51 : /// {@macro foss.meter.preview}
52 : ///
53 : /// Creates a gauge showing [value] within `[min, max]` (clamped). The value
54 : /// is shown at the end of the label row unless [showValue] is false.
55 1 : const FossMeter({
56 : required this.value,
57 : this.min = 0,
58 : this.max = 100,
59 : this.label,
60 : this.showValue = true,
61 : this.formatValue,
62 : this.semanticsLabel,
63 : this.style,
64 : super.key,
65 : });
66 :
67 : /// The current measurement, clamped into `[min, max]`.
68 : final num value;
69 :
70 : /// The range floor.
71 : final num min;
72 :
73 : /// The range ceiling.
74 : final num max;
75 :
76 : /// Text at the start of the row above the track; null hides it.
77 : final String? label;
78 :
79 : /// Whether the value is shown at the end of the row above the track.
80 : final bool showValue;
81 :
82 : /// Builds the value text from the value and range. Defaults to a percentage
83 : /// of the range (`40%`).
84 : final String Function(num value, num min, num max)? formatValue;
85 :
86 : /// Accessibility name for the gauge when there is no visible [label].
87 : final String? semanticsLabel;
88 :
89 : /// Per-instance visual overrides.
90 : final FossMeterStyle? style;
91 :
92 1 : @override
93 : Widget build(BuildContext context) {
94 1 : final theme = context.fossTheme;
95 1 : final colors = theme.colors;
96 1 : final s = style;
97 :
98 3 : final span = max - min;
99 5 : final raw = span > 0 ? (value - min) / span : 0.0;
100 : // A non-finite value (NaN from a bad computation) must read as empty, not
101 : // as a full bar: NaN.clamp returns the upper bound on the current SDK.
102 2 : final fraction = raw.isFinite ? raw.clamp(0.0, 1.0) : 0.0;
103 : // The default value text is the fill as a whole-number percentage of the
104 : // range; a caller formatter takes the raw value and bounds instead.
105 1 : final format = formatValue;
106 : final valueText = format != null
107 4 : ? format(value, min, max)
108 3 : : '${(fraction * 100).round()}%';
109 :
110 1 : final shape = RoundedSuperellipseBorder(
111 1 : borderRadius: BorderRadius.circular(FossRadii.full),
112 : );
113 :
114 : final reduceMotion =
115 2 : MediaQuery.maybeOf(context)?.disableAnimations ?? false;
116 :
117 1 : final track = DecoratedBox(
118 1 : decoration: ShapeDecoration(
119 2 : color: s?.trackColor ?? colors.input,
120 : shape: shape,
121 : ),
122 1 : child: ClipPath(
123 1 : clipper: ShapeBorderClipper(shape: shape),
124 1 : child: SizedBox(
125 : height: _trackHeight,
126 : width: double.infinity,
127 1 : child: TweenAnimationBuilder<double>(
128 1 : tween: Tween(end: fraction),
129 2 : duration: reduceMotion ? Duration.zero : theme.motion.progress,
130 : curve: _fillCurve,
131 2 : builder: (context, t, _) => FractionallySizedBox(
132 : alignment: AlignmentDirectional.centerStart,
133 : widthFactor: t,
134 1 : child: DecoratedBox(
135 1 : decoration: ShapeDecoration(
136 2 : color: s?.fillColor ?? colors.primary,
137 : shape: shape,
138 : ),
139 : ),
140 : ),
141 : ),
142 : ),
143 : ),
144 : );
145 :
146 1 : return Semantics(
147 : container: true,
148 : // Flutter carries no ARIA meter role, so the gauge announces its value
149 : // and range through the value fields rather than a role.
150 2 : label: label ?? semanticsLabel,
151 : // Announce on the raw scale so value and bounds agree; the visible
152 : // percent text is for sighted users only.
153 2 : value: '$value',
154 2 : minValue: '$min',
155 2 : maxValue: '$max',
156 1 : child: Column(
157 : mainAxisSize: MainAxisSize.min,
158 : crossAxisAlignment: CrossAxisAlignment.stretch,
159 2 : spacing: theme.spacing(2),
160 1 : children: [
161 1 : ?_buildRow(theme, colors, s, valueText),
162 1 : track,
163 : ],
164 : ),
165 : );
166 : }
167 :
168 1 : Widget? _buildRow(
169 : FossThemeData theme,
170 : FossColors colors,
171 : FossMeterStyle? s,
172 : String valueText,
173 : ) {
174 2 : if (label == null && !showValue) return null;
175 :
176 : // The gauge's name and value live on the Semantics container, so the
177 : // visible row text is excluded to avoid a double announcement.
178 :
179 3 : final labelStyle = theme.typography.xs.medium
180 2 : .copyWith(color: colors.foreground)
181 2 : .merge(s?.labelStyle);
182 : // Tabular figures keep the digits from jittering as the value changes.
183 3 : final valueStyle = theme.typography.xs.medium
184 1 : .copyWith(
185 1 : color: colors.foreground,
186 : fontFeatures: const [FontFeature.tabularFigures()],
187 : )
188 2 : .merge(s?.valueStyle);
189 :
190 1 : return ExcludeSemantics(
191 1 : child: Row(
192 1 : children: [
193 1 : Expanded(
194 1 : child: Text(
195 1 : label ?? '',
196 : style: labelStyle,
197 : maxLines: 1,
198 : overflow: TextOverflow.ellipsis,
199 : ),
200 : ),
201 1 : if (showValue)
202 1 : Text(
203 : valueText,
204 : style: valueStyle,
205 : maxLines: 1,
206 : overflow: TextOverflow.ellipsis,
207 : ),
208 : ],
209 : ),
210 : );
211 : }
212 : }
|