LCOV - code coverage report
Current view: top level - components/progress - foss_progress.dart Coverage Total Hit
Test: fossui coverage Lines: 100.0 % 48 48
Test Date: 2026-11-29 09:15:47 Functions: - 0 0

            Line data    Source code
       1              : import 'package:flutter/semantics.dart' show SemanticsRole;
       2              : import 'package:flutter/widgets.dart';
       3              : import 'package:fossui/src/components/spinner/foss_spinner.dart';
       4              : import 'package:fossui/src/theme/theme.dart';
       5              : 
       6              : part 'foss_progress_style.dart';
       7              : 
       8              : // The track is a fixed-height bar; its height is a component constant, not a
       9              : // token (nothing else references it). The fill eases over its width with a
      10              : // plain ease; curves are widget constants, not tokens.
      11              : const double _trackHeight = 6;
      12              : const Curve _fillCurve = Curves.ease;
      13              : 
      14              : /// {@category Feedback}
      15              : /// {@template foss.progress.preview}
      16              : /// <img src="https://fossui.org/components/progress/overview/light.png"
      17              : ///   alt="FossProgress, light theme" width="480"
      18              : ///   style="max-width:100%;height:auto" />
      19              : /// <img src="https://fossui.org/components/progress/overview/dark.png"
      20              : ///   alt="FossProgress, dark theme" width="480"
      21              : ///   style="max-width:100%;height:auto" />
      22              : ///
      23              : /// See the [progress documentation ↗](https://fossui.org/docs/components/progress) or
      24              : /// try it live in the
      25              : /// [playground ↗](https://play.fossui.org/components/#/?path=components/progress/fossprogress/playground).
      26              : /// {@endtemplate}
      27              : ///
      28              : /// A determinate progress bar: a full-width track with a leading fill that
      29              : /// grows from the start to show how far a long task has run. It is static and
      30              : /// non-interactive.
      31              : ///
      32              : /// [value] is the filled fraction in `0..1`, clamped on input. An optional
      33              : /// [label] sits at the start of a row above the track and [valueLabel] at the
      34              : /// end; the row renders only when at least one is set. The fill animates its
      35              : /// width over the `progress` motion token and jumps under reduced motion.
      36              : /// Colors, type, and radius come from `context.fossTheme`; pass a
      37              : /// [FossProgressStyle] for a one-off override.
      38              : ///
      39              : /// {@macro foss.customize}
      40              : ///
      41              : /// See also [FossSpinner] for indeterminate progress.
      42              : ///
      43              : /// ```dart
      44              : /// FossProgress(
      45              : ///   value: 0.4,
      46              : ///   label: 'Uploading',
      47              : ///   valueLabel: '40%',
      48              : /// );
      49              : /// ```
      50              : class FossProgress extends StatelessWidget {
      51              :   /// {@macro foss.progress.preview}
      52              :   ///
      53              :   /// Creates a progress bar filled to [value] (`0..1`, clamped). A bare track
      54              :   /// with no label row is the default.
      55            1 :   const FossProgress({
      56              :     required this.value,
      57              :     this.label,
      58              :     this.valueLabel,
      59              :     this.semanticLabel,
      60              :     this.style,
      61              :     super.key,
      62              :   });
      63              : 
      64              :   /// The filled fraction, in `0..1`. Values outside the range are clamped.
      65              :   final double value;
      66              : 
      67              :   /// Text at the start of the row above the track; null hides it.
      68              :   final String? label;
      69              : 
      70              :   /// Text at the end of the row above the track, the caller's formatted value
      71              :   /// (`'40%'`, `'3 of 8'`); null hides it.
      72              :   final String? valueLabel;
      73              : 
      74              :   /// Accessibility name for the bar when there is no visible [label].
      75              :   final String? semanticLabel;
      76              : 
      77              :   /// Per-instance visual overrides.
      78              :   final FossProgressStyle? style;
      79              : 
      80            1 :   @override
      81              :   Widget build(BuildContext context) {
      82            1 :     final theme = context.fossTheme;
      83            1 :     final colors = theme.colors;
      84            1 :     final s = style;
      85              : 
      86            2 :     final fraction = value.clamp(0.0, 1.0);
      87            1 :     final radius = BorderRadius.circular(FossRadii.full);
      88            1 :     final shape = RoundedSuperellipseBorder(borderRadius: radius);
      89              : 
      90              :     final reduceMotion =
      91            2 :         MediaQuery.maybeOf(context)?.disableAnimations ?? false;
      92              : 
      93            1 :     final track = DecoratedBox(
      94            1 :       decoration: ShapeDecoration(
      95            2 :         color: s?.trackColor ?? colors.input,
      96              :         shape: shape,
      97              :       ),
      98            1 :       child: ClipPath(
      99            1 :         clipper: ShapeBorderClipper(shape: shape),
     100            1 :         child: SizedBox(
     101              :           height: _trackHeight,
     102              :           width: double.infinity,
     103            1 :           child: TweenAnimationBuilder<double>(
     104            1 :             tween: Tween(end: fraction),
     105            2 :             duration: reduceMotion ? Duration.zero : theme.motion.progress,
     106              :             curve: _fillCurve,
     107            2 :             builder: (context, t, _) => FractionallySizedBox(
     108              :               alignment: AlignmentDirectional.centerStart,
     109              :               widthFactor: t,
     110            1 :               child: DecoratedBox(
     111            1 :                 decoration: ShapeDecoration(
     112            2 :                   color: s?.fillColor ?? colors.primary,
     113              :                   shape: shape,
     114              :                 ),
     115              :               ),
     116              :             ),
     117              :           ),
     118              :         ),
     119              :       ),
     120              :     );
     121              : 
     122            1 :     final row = _buildRow(theme, colors, s);
     123              : 
     124            1 :     return Semantics(
     125              :       container: true,
     126              :       role: SemanticsRole.progressBar,
     127            2 :       label: label ?? semanticLabel,
     128              :       // The progressbar role bounds the value; it runs 0..1 like the fraction,
     129              :       // and assistive tech announces the percentage from the range.
     130            1 :       value: fraction.toStringAsFixed(2),
     131              :       minValue: '0',
     132              :       maxValue: '1',
     133            1 :       child: Column(
     134              :         mainAxisSize: MainAxisSize.min,
     135              :         crossAxisAlignment: CrossAxisAlignment.stretch,
     136            2 :         spacing: theme.spacing(2),
     137            1 :         children: [
     138            1 :           ?row,
     139            1 :           track,
     140              :         ],
     141              :       ),
     142              :     );
     143              :   }
     144              : 
     145            1 :   Widget? _buildRow(
     146              :     FossThemeData theme,
     147              :     FossColors colors,
     148              :     FossProgressStyle? s,
     149              :   ) {
     150            2 :     if (label == null && valueLabel == null) return null;
     151              : 
     152              :     // The bar's name and value live on the Semantics container, so the visible
     153              :     // row text is excluded to avoid a double announcement.
     154              : 
     155            3 :     final labelStyle = theme.typography.sm.medium
     156            2 :         .copyWith(color: colors.foreground)
     157            2 :         .merge(s?.labelStyle);
     158              :     // Tabular figures keep the digits from jittering as the value changes.
     159            2 :     final valueStyle = theme.typography.sm
     160            1 :         .copyWith(
     161            1 :           color: colors.foreground,
     162              :           fontFeatures: const [FontFeature.tabularFigures()],
     163              :         )
     164            2 :         .merge(s?.valueLabelStyle);
     165              : 
     166            1 :     return ExcludeSemantics(
     167            1 :       child: Row(
     168            1 :         children: [
     169            1 :           Expanded(
     170            1 :             child: Text(
     171            1 :               label ?? '',
     172              :               style: labelStyle,
     173              :               maxLines: 1,
     174              :               overflow: TextOverflow.ellipsis,
     175              :             ),
     176              :           ),
     177            1 :           if (valueLabel case final valueLabel?)
     178            1 :             Text(
     179              :               valueLabel,
     180              :               style: valueStyle,
     181              :               maxLines: 1,
     182              :               overflow: TextOverflow.ellipsis,
     183              :             ),
     184              :         ],
     185              :       ),
     186              :     );
     187              :   }
     188              : }
        

Generated by: LCOV version 2.0-1