9551eceaed
Remove obsolete control-family flags and icon metadata across all callers, dead alternate themes and host helpers, obsolete test setup, and the unused icon-font dependency. Preserve native Material Back transitions. Make navigation fixtures independent of update plugins and answer auxiliary shell snapshots so the complete application suite can run deterministically.
141 lines
3.9 KiB
Dart
141 lines
3.9 KiB
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) 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.child,
|
|
this.enabled = true,
|
|
this.tint,
|
|
});
|
|
|
|
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<SegmentedPress> createState() => _SegmentedPressState();
|
|
}
|
|
|
|
class _SegmentedPressState extends State<SegmentedPress>
|
|
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<int> _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 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,
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|