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 : /// {@category Feedback}
6 : /// {@template foss.skeleton.preview}
7 : /// <img src="https://fossui.org/components/skeleton/overview/light.png"
8 : /// alt="FossSkeleton, light theme" width="480"
9 : /// style="max-width:100%;height:auto" />
10 : /// <img src="https://fossui.org/components/skeleton/overview/dark.png"
11 : /// alt="FossSkeleton, dark theme" width="480"
12 : /// style="max-width:100%;height:auto" />
13 : ///
14 : /// See the [skeleton documentation ↗](https://fossui.org/docs/components/skeleton)
15 : /// or try it live in the
16 : /// [playground ↗](https://play.fossui.org/components/#/?path=components/skeleton/fossskeleton/playground).
17 : /// {@endtemplate}
18 : ///
19 : /// A placeholder that stands in for content while it loads.
20 : ///
21 : /// Sits in the layout at the size of the content it replaces, filled with the
22 : /// theme `muted` token and swept by a soft shimmer. Compose several boxes to
23 : /// outline a card or list row; use [FossSkeleton.circle] for avatars.
24 : ///
25 : /// The shimmer runs on the `skeleton` motion token and stops under reduced
26 : /// motion, leaving a static fill.
27 : ///
28 : /// ```dart
29 : /// const FossSkeleton(width: 200, height: 16);
30 : /// const FossSkeleton.circle(size: 40);
31 : /// ```
32 : @FossSince('0.1.1')
33 : class FossSkeleton extends StatefulWidget {
34 : /// {@macro foss.skeleton.preview}
35 : ///
36 : /// Creates a rectangular placeholder [width] by [height] logical pixels,
37 : /// with corners rounded to the theme `sm` radius.
38 1 : const FossSkeleton({this.width, this.height, super.key}) : _circle = false;
39 :
40 : /// Creates a circular placeholder [size] logical pixels across, for avatars
41 : /// and other round content.
42 1 : const FossSkeleton.circle({required double size, super.key})
43 : : width = size,
44 : height = size,
45 : _circle = true;
46 :
47 : /// The placeholder width in logical pixels. Null defers to the parent
48 : /// constraints; pass `double.infinity` to fill the available width.
49 : final double? width;
50 :
51 : /// The placeholder height in logical pixels. Null defers to the parent
52 : /// constraints; pass `double.infinity` to fill the available height.
53 : final double? height;
54 :
55 : final bool _circle;
56 :
57 1 : @override
58 1 : State<FossSkeleton> createState() => _FossSkeletonState();
59 : }
60 :
61 : class _FossSkeletonState extends State<FossSkeleton>
62 : with SingleTickerProviderStateMixin {
63 2 : late final AnimationController _controller = AnimationController(vsync: this);
64 :
65 1 : @override
66 : void didChangeDependencies() {
67 1 : super.didChangeDependencies();
68 : // Read the cycle length from the theme here, not in initState. A new value
69 : // applies on the next loop.
70 6 : _controller.duration = context.fossTheme.motion.skeleton;
71 : final reduceMotion =
72 3 : MediaQuery.maybeOf(context)?.disableAnimations ?? false;
73 : if (reduceMotion) {
74 2 : _controller.stop();
75 2 : } else if (!_controller.isAnimating) {
76 2 : _controller.repeat();
77 : }
78 : }
79 :
80 1 : @override
81 : void dispose() {
82 2 : _controller.dispose();
83 1 : super.dispose();
84 : }
85 :
86 1 : @override
87 : Widget build(BuildContext context) {
88 2 : final colors = context.fossTheme.colors;
89 2 : final shape = widget._circle
90 : ? const CircleBorder()
91 1 : : RoundedSuperellipseBorder(
92 4 : borderRadius: BorderRadius.circular(context.fossTheme.radii.sm),
93 : );
94 :
95 1 : final base = DecoratedBox(
96 2 : decoration: ShapeDecoration(shape: shape, color: colors.muted),
97 : );
98 :
99 : final reduceMotion =
100 2 : MediaQuery.maybeOf(context)?.disableAnimations ?? false;
101 :
102 : // The shimmer is always a lightening band; only its strength shifts with
103 : // the theme, brighter on a light surface, barely-there on a dark one.
104 3 : final isDark = colors.background.computeLuminance() < 0.5;
105 1 : final highlight = Color.fromRGBO(255, 255, 255, isDark ? 0.04 : 0.64);
106 :
107 : // A skeleton is a decorative loading placeholder; keep it out of the
108 : // semantics tree so a reader is not read an empty box.
109 1 : return ExcludeSemantics(
110 1 : child: SizedBox(
111 2 : width: widget.width,
112 2 : height: widget.height,
113 : child: reduceMotion
114 : ? base
115 1 : : Stack(
116 : fit: StackFit.expand,
117 1 : children: [
118 : base,
119 1 : ClipPath(
120 1 : clipper: ShapeBorderClipper(shape: shape),
121 1 : child: AnimatedBuilder(
122 1 : animation: _controller,
123 2 : builder: (context, child) => DecoratedBox(
124 1 : decoration: BoxDecoration(
125 1 : gradient: LinearGradient(
126 1 : colors: [
127 : const Color(0x00FFFFFF),
128 : highlight,
129 : const Color(0x00FFFFFF),
130 : ],
131 : stops: const [0.35, 0.5, 0.65],
132 : // Tile the band so one sweeps in as the last sweeps
133 : // out: a continuous shimmer with no flat beat.
134 : tileMode: TileMode.repeated,
135 3 : transform: _SweepTranslate(_controller.value),
136 : ),
137 : ),
138 : ),
139 : ),
140 : ),
141 : ],
142 : ),
143 : ),
144 : );
145 : }
146 : }
147 :
148 : /// Slides the tiled shimmer gradient by exactly one width as [t] runs 0 to 1,
149 : /// so the loop is seamless: [t] = 1 lands on the same phase as [t] = 0.
150 : class _SweepTranslate extends GradientTransform {
151 1 : const _SweepTranslate(this.t);
152 :
153 : final double t;
154 :
155 1 : @override
156 : Matrix4 transform(Rect bounds, {TextDirection? textDirection}) =>
157 5 : Matrix4.translationValues(-t * bounds.width, 0, 0);
158 : }
|