import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/physics.dart'; import '../motion/motion.dart'; import 'glass_pill.dart'; /// One island of a [SegmentedBar] (a pill, or an action inside one) popping /// in: it grows from [Motion.barEntranceScaleBegin] with a gentle overshoot /// while it fades in, after [delay]. /// /// It plays once, when the island is first built; rebuilding it (a new /// title, new actions, a scroll) does not play it again, so the bar keys /// its islands to keep them while they stay on screen. Under reduced motion, /// or when [play] is false, the island simply appears. /// /// A [glass] island fades its [GlassPill] through a [GlassReveal], so the /// blur fades in with the fill instead of snapping on at the end; anything /// else fades as a whole. class SegmentedEntrance extends StatefulWidget { const SegmentedEntrance({ super.key, required this.child, this.delay = Duration.zero, this.play = true, this.glass = true, }); final Widget child; final Duration delay; final bool play; final bool glass; @override State createState() => _SegmentedEntranceState(); } class _SegmentedEntranceState extends State with SingleTickerProviderStateMixin { AnimationController? _controller; Animation _pop = kAlwaysCompleteAnimation; Animation _fade = kAlwaysCompleteAnimation; bool _decided = false; @override void didChangeDependencies() { super.didChangeDependencies(); // Decided once, on the first build: later dependency changes (a theme, // a text scale) never replay it. if (_decided) return; _decided = true; final window = Motion.of(context, Motion.barEntranceDuration); if (!widget.play || window == Duration.zero) return; final total = (widget.delay + window).inMicroseconds; final begin = widget.delay.inMicroseconds / total; final faded = (widget.delay.inMicroseconds + window.inMicroseconds * Motion.barEntranceOpacityPortion) / total; final controller = AnimationController( vsync: this, duration: widget.delay + window, ); _controller = controller; _pop = CurvedAnimation( parent: controller, curve: Interval(begin, 1, curve: Motion.barEntranceCurve), ); _fade = CurvedAnimation( parent: controller, curve: Interval(begin, faded, curve: Curves.easeOut), ); controller.forward(); } @override void dispose() { if (_pop case final CurvedAnimation curved) curved.dispose(); if (_fade case final CurvedAnimation curved) curved.dispose(); _controller?.dispose(); super.dispose(); } @override Widget build(BuildContext context) { final child = widget.glass ? GlassReveal(opacity: _fade, child: widget.child) : FadeTransition( opacity: _fade, // Controls stay announced (and findable) while they fade in. alwaysIncludeSemantics: true, child: widget.child, ); return AnimatedBuilder( animation: _pop, child: child, builder: (context, child) => Transform.scale( scale: Motion.barEntranceScaleBegin + (1 - Motion.barEntranceScaleBegin) * _pop.value, child: child, ), ); } } /// Press feedback for a control of a [SegmentedBar]: while a pointer is down /// on [child] it sinks to [Motion.barPressScale] under a tint clipped to a /// stadium over its bounds (the pill's own shape), and on release it springs /// back. /// /// It only listens: the control below still takes the tap, and its own /// feedback (an ink ripple, a Cupertino button's dimming) still shows. A /// quick tap plays the whole press before springing back, so it is seen. /// Under reduced motion only the tint shows. When [enabled] is false it does /// nothing, so a bar can switch a piece's feedback on and off without /// rebuilding the control. class SegmentedPress extends StatefulWidget { const SegmentedPress({ super.key, required this.cupertino, required this.child, this.enabled = true, this.tint, }); final bool cupertino; final Widget child; final bool enabled; /// The tint's colour before its opacity; defaults to the host's text /// colour, which reads over the glass in light and dark themes. final Color? tint; @override State createState() => _SegmentedPressState(); } class _SegmentedPressState extends State with SingleTickerProviderStateMixin { /// 0 at rest, 1 fully pressed; the release spring may pass 0 slightly. late final AnimationController _press = AnimationController.unbounded( vsync: this, ); final Set _pointers = {}; bool _released = true; bool get _reduced => Motion.of(context, Motion.barPressDuration) == Duration.zero; void _down(PointerDownEvent event) { if (!widget.enabled) return; _pointers.add(event.pointer); _released = false; if (_reduced) { _press.value = 1; return; } _press .animateTo(1, duration: Motion.barPressDuration, curve: Curves.easeOut) .then((_) { if (_released && mounted) _springBack(); }); } void _up(PointerEvent event) { if (!_pointers.remove(event.pointer) || _pointers.isNotEmpty) return; _released = true; // Still sinking: the press completes first, then springs back. if (_press.isAnimating && _press.value < 1) return; _springBack(); } void _springBack() { if (_reduced) { _press.value = 0; return; } _press .animateWith( SpringSimulation(Motion.barReleaseSpring, _press.value, 0, 0), ) // The spring settles within a tolerance; rest is exactly at rest. .then((_) { if (mounted) _press.value = 0; }); } @override void didUpdateWidget(SegmentedPress oldWidget) { super.didUpdateWidget(oldWidget); if (!widget.enabled && oldWidget.enabled) { _pointers.clear(); _released = true; _press.value = 0; } } @override void dispose() { _press.dispose(); super.dispose(); } Color _tint(BuildContext context) { if (widget.tint case final tint?) return tint; return widget.cupertino ? CupertinoColors.label.resolveFrom(context) : Theme.of(context).colorScheme.onSurface; } @override Widget build(BuildContext context) { final tint = _tint(context); return Listener( onPointerDown: _down, onPointerUp: _up, onPointerCancel: _up, child: AnimatedBuilder( animation: _press, child: widget.child, builder: (context, child) { final pressed = _press.value; final reduced = _reduced; return Transform.scale( scale: reduced ? 1 : 1 - (1 - Motion.barPressScale) * pressed, child: DecoratedBox( position: DecorationPosition.foreground, decoration: ShapeDecoration( shape: const StadiumBorder(), color: tint.withValues( alpha: tint.a * Motion.barPressTintOpacity * pressed.clamp(0.0, 1.0), ), ), child: child, ), ); }, ), ); } }