import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/physics.dart'; import '../motion/motion.dart'; /// 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, ), ); }, ), ); } }