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

            Line data    Source code
       1              : import 'dart:math' as math;
       2              : 
       3              : import 'package:flutter/widgets.dart';
       4              : import 'package:fossui/src/components/progress/foss_progress.dart';
       5              : import 'package:fossui/src/theme/theme.dart';
       6              : 
       7              : /// {@category Feedback}
       8              : /// {@template foss.spinner.preview}
       9              : /// <img src="https://fossui.org/components/spinner/overview/light.png"
      10              : ///   alt="FossSpinner, light theme" width="480"
      11              : ///   style="max-width:100%;height:auto" />
      12              : /// <img src="https://fossui.org/components/spinner/overview/dark.png"
      13              : ///   alt="FossSpinner, dark theme" width="480"
      14              : ///   style="max-width:100%;height:auto" />
      15              : ///
      16              : /// See the [spinner documentation ↗](https://fossui.org/docs/components/spinner) or
      17              : /// try it live in the
      18              : /// [playground ↗](https://play.fossui.org/components/#/?path=components/spinner/fossspinner/playground).
      19              : /// {@endtemplate}
      20              : ///
      21              : /// A circular loading indicator: an open arc that spins continuously.
      22              : ///
      23              : /// The arc paints in [color], defaulting to the theme `foreground` token. The
      24              : /// rotation cycle comes from the `spinner` motion token and stops under reduced
      25              : /// motion.
      26              : ///
      27              : /// See also [FossProgress] for determinate progress.
      28              : ///
      29              : /// ```dart
      30              : /// const FossSpinner(size: 18);
      31              : /// ```
      32              : class FossSpinner extends StatefulWidget {
      33              :   /// {@macro foss.spinner.preview}
      34              :   ///
      35              :   /// Creates a spinner [size] logical pixels across, in [color].
      36            3 :   const FossSpinner({
      37              :     this.size = 24,
      38              :     this.color,
      39              :     this.semanticLabel = 'Loading',
      40              :     super.key,
      41              :   });
      42              : 
      43              :   /// The width and height of the indicator, in logical pixels.
      44              :   final double size;
      45              : 
      46              :   /// The arc color. Defaults to the theme `foreground` token.
      47              :   final Color? color;
      48              : 
      49              :   /// The accessibility label announced while loading.
      50              :   final String semanticLabel;
      51              : 
      52            4 :   @override
      53            4 :   State<FossSpinner> createState() => _FossSpinnerState();
      54              : }
      55              : 
      56              : class _FossSpinnerState extends State<FossSpinner>
      57              :     with SingleTickerProviderStateMixin {
      58            8 :   late final AnimationController _controller = AnimationController(vsync: this);
      59              : 
      60            4 :   @override
      61              :   void didChangeDependencies() {
      62            4 :     super.didChangeDependencies();
      63              :     // Read inherited config here, not in initState. A new cycle length applies
      64              :     // on the next loop.
      65           24 :     _controller.duration = context.fossTheme.motion.spinner;
      66              :     // Only spin when motion is allowed: under reduced motion build drops the
      67              :     // rotation, so a running controller would tick invisibly. React either way
      68              :     // the flag flips.
      69              :     final reduceMotion =
      70           12 :         MediaQuery.maybeOf(context)?.disableAnimations ?? false;
      71              :     if (reduceMotion) {
      72            2 :       _controller.stop();
      73            8 :     } else if (!_controller.isAnimating) {
      74            8 :       _controller.repeat();
      75              :     }
      76              :   }
      77              : 
      78            4 :   @override
      79              :   void dispose() {
      80            8 :     _controller.dispose();
      81            4 :     super.dispose();
      82              :   }
      83              : 
      84            4 :   @override
      85              :   Widget build(BuildContext context) {
      86           11 :     final color = widget.color ?? context.fossTheme.colors.foreground;
      87              : 
      88            4 :     final arc = SizedBox.square(
      89            8 :       dimension: widget.size,
      90            8 :       child: CustomPaint(painter: _SpinnerPainter(color)),
      91              :     );
      92              : 
      93              :     final reduceMotion =
      94            8 :         MediaQuery.maybeOf(context)?.disableAnimations ?? false;
      95            4 :     return Semantics(
      96            8 :       label: widget.semanticLabel,
      97              :       liveRegion: true,
      98              :       child: reduceMotion
      99              :           ? arc
     100            8 :           : RotationTransition(turns: _controller, child: arc),
     101              :     );
     102              :   }
     103              : }
     104              : 
     105              : /// Paints a 270-degree arc with a round cap, the spinner glyph.
     106              : class _SpinnerPainter extends CustomPainter {
     107            4 :   const _SpinnerPainter(this.color);
     108              : 
     109              :   final Color color;
     110              : 
     111            3 :   @override
     112              :   void paint(Canvas canvas, Size size) {
     113            6 :     final stroke = size.width / 12;
     114            3 :     final paint = Paint()
     115            6 :       ..color = color
     116            3 :       ..style = PaintingStyle.stroke
     117            3 :       ..strokeWidth = stroke
     118            3 :       ..strokeCap = StrokeCap.round;
     119              :     // A 270-degree arc starting at the top.
     120            9 :     final rect = (Offset.zero & size).deflate(stroke / 2);
     121           12 :     canvas.drawArc(rect, -math.pi / 2, math.pi * 1.5, false, paint);
     122              :   }
     123              : 
     124            2 :   @override
     125            6 :   bool shouldRepaint(_SpinnerPainter oldDelegate) => oldDelegate.color != color;
     126              : }
        

Generated by: LCOV version 2.0-1