Pop the top bar's islands in and give its controls press feedback

Each island (Back, title, actions pill, each action) scales in from 0.85
with a gentle overshoot, staggered from the leading edge on first show and
at once when it newly appears; rebuilds and title changes keep it. Glass
fades its fill and blur together so backdrop grouping is unchanged.
Every tappable piece sinks to 0.94 under a stadium tint while pressed and
springs back. Reduced motion skips the animation.
This commit is contained in:
2026-11-02 16:47:01 +00:00
parent d78cbe3f66
commit 009bf3d853
8 changed files with 801 additions and 70 deletions
+41 -3
View File
@@ -43,6 +43,24 @@ class GlassPill extends StatelessWidget {
@override
Widget build(BuildContext context) {
final reveal = GlassReveal.maybeOf(context);
if (reveal == null) return _build(context, 1, child);
// The fill, edge and blur fade in together, and the content with them:
// an opacity layer over the whole piece would hide the content behind
// it from the blur until the fade ends, and the blur would snap on.
final content = FadeTransition(
opacity: reveal,
alwaysIncludeSemantics: true,
child: child,
);
return AnimatedBuilder(
animation: reveal,
builder: (context, _) =>
_build(context, reveal.value.clamp(0.0, 1.0), content),
);
}
Widget _build(BuildContext context, double reveal, Widget child) {
final solid = MediaQuery.maybeHighContrastOf(context) ?? false;
final Color base;
final Color edge;
@@ -56,10 +74,11 @@ class GlassPill extends StatelessWidget {
base = scheme.surfaceContainerHigh;
edge = scheme.outlineVariant;
}
final color = fill ?? (solid ? base : base.withValues(alpha: fillOpacity));
final opaque = fill ?? (solid ? base : base.withValues(alpha: fillOpacity));
final color = opaque.withValues(alpha: opaque.a * reveal);
final border = shape.copyWith(
side: BorderSide(
color: solid ? edge : edge.withValues(alpha: edge.a * 0.8),
color: edge.withValues(alpha: edge.a * (solid ? 1 : 0.8) * reveal),
width: 0.5,
),
);
@@ -74,7 +93,10 @@ class GlassPill extends StatelessWidget {
// [BackdropGroup] (a bar's row) share one read of the content behind
// them where the engine supports it (Impeller) instead of one each.
surface = BackdropFilter.grouped(
filter: ImageFilter.blur(sigmaX: blurSigma, sigmaY: blurSigma),
filter: ImageFilter.blur(
sigmaX: blurSigma * reveal,
sigmaY: blurSigma * reveal,
),
child: surface,
);
}
@@ -87,3 +109,19 @@ class GlassPill extends StatelessWidget {
);
}
}
/// Fades in every [GlassPill] below it by [opacity]: its fill, hairline,
/// blur and content together. A piece entering the screen uses it instead of
/// an opacity layer, under which the blur would read nothing behind it.
class GlassReveal extends InheritedWidget {
const GlassReveal({super.key, required this.opacity, required super.child});
final Animation<double> opacity;
static Animation<double>? maybeOf(BuildContext context) =>
context.dependOnInheritedWidgetOfExactType<GlassReveal>()?.opacity;
@override
bool updateShouldNotify(GlassReveal oldWidget) =>
opacity != oldWidget.opacity;
}
+161 -64
View File
@@ -3,6 +3,8 @@ import 'dart:math' as math;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../motion/motion.dart';
import 'segmented_bar_motion.dart';
import 'segmented_bar_pieces.dart';
export 'segmented_bar_pieces.dart' show SegmentedBackButton, SegmentedPill;
@@ -169,6 +171,21 @@ class _SegmentedBarState extends State<SegmentedBar> {
final _piecesBackdrop = BackdropKey();
final _expandedTitleBackdrop = BackdropKey();
/// Whether the bar's first frame is drawn. Islands built with the bar
/// enter one after another from the leading edge; an island that turns up
/// later (an action becoming available) enters at once.
bool _shown = false;
/// Whether the actions pill was there at the last build: actions that
/// arrive with the pill enter with it, not again inside it.
bool _hadActions = false;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) => _shown = true);
}
@override
void didUpdateWidget(covariant SegmentedBar oldWidget) {
super.didUpdateWidget(oldWidget);
@@ -186,12 +203,101 @@ class _SegmentedBarState extends State<SegmentedBar> {
setState(() => _expanded = !_expanded);
}
/// The bar's main pill: the [SegmentedBar.titleWidget] filling the row, or
/// the title pill (drawn expanded in the overlay when it is expandable).
Widget _titleIsland(
BuildContext context,
Widget Function(bool expanded) titlePill,
) {
final titleWidget = widget.titleWidget;
if (titleWidget != null) {
// The main pill fills the row, up to its cap.
return ConstrainedBox(
constraints: BoxConstraints(maxWidth: widget.titleWidgetMaxWidth),
child: SegmentedPill(
cupertino: widget.cupertino,
fill: widget.pillColor,
child: SizedBox(width: double.infinity, child: titleWidget),
),
);
}
if (!widget.expandableTitle) return titlePill(false);
return LayoutBuilder(
builder: (context, titleSpace) => OverlayPortal(
controller: _expandedTitle,
overlayChildBuilder: (context) => !(_expanded && widget.expandableTitle)
? const SizedBox.shrink()
: Positioned(
top: 0,
left: 0,
child: CompositedTransformFollower(
link: _titleLink,
showWhenUnlinked: false,
child: ConstrainedBox(
constraints: BoxConstraints(maxWidth: titleSpace.maxWidth),
child: BackdropGroup(
backdropKey: _expandedTitleBackdrop,
child: titlePill(true),
),
),
),
),
child: CompositedTransformTarget(
link: _titleLink,
child: ExcludeSemantics(
excluding: _expanded && widget.expandableTitle,
child: titlePill(false),
),
),
),
);
}
/// The actions, each keyed by its own key (or its type and place among
/// actions of that type) so one arriving or leaving leaves the others be.
/// Only the first shrinks (ellipsises); the rest keep their natural width
/// as inflexible (flex 0) children.
List<Widget> _actionIslands(
List<Widget> actions, {
required bool cupertino,
required bool play,
}) {
final seen = <Type, int>{};
return [
for (final (index, action) in actions.indexed)
Flexible(
key: ValueKey((
'segmented.action',
action.key ??
(
action.runtimeType,
seen.update(
action.runtimeType,
(n) => n + 1,
ifAbsent: () => 0,
),
),
)),
flex: index == 0 ? 1 : 0,
child: SegmentedEntrance(
play: play,
glass: false,
child: SegmentedPress(
cupertino: cupertino,
tint: widget.foregroundColor,
enabled: actions.length > 1,
child: action,
),
),
),
];
}
@override
Widget build(BuildContext context) {
final cupertino = widget.cupertino;
final title = widget.title;
final titleWidget = widget.titleWidget;
final titleWidgetMaxWidth = widget.titleWidgetMaxWidth;
final leadingWidget = widget.leading;
final automaticallyImplyLeading = widget.automaticallyImplyLeading;
final subtitle = widget.subtitle;
@@ -224,14 +330,33 @@ class _SegmentedBarState extends State<SegmentedBar> {
foreground: foregroundColor,
fill: pillColor,
);
// Each island is keyed by its role, so it keeps its entrance (and plays
// none) while the bar rebuilds around it.
var order = 0;
Duration nextDelay() =>
_shown ? Duration.zero : Motion.barEntranceStagger * order++;
final leadingDelay = leading == null ? Duration.zero : nextDelay();
final hasTitle = titleWidget != null || showTitle;
final titleDelay = hasTitle ? nextDelay() : Duration.zero;
final actionsDelay = actions.isEmpty ? Duration.zero : nextDelay();
final freshActions = !_hadActions;
_hadActions = actions.isNotEmpty;
final Widget bar = LayoutBuilder(
builder: (context, constraints) => Row(
children: [
if (leading != null) ...[
SegmentedPill(
cupertino: cupertino,
fill: pillColor,
child: leading,
SegmentedEntrance(
key: const ValueKey('segmented.leading'),
delay: leadingDelay,
child: SegmentedPress(
cupertino: cupertino,
tint: foregroundColor,
child: SegmentedPill(
cupertino: cupertino,
fill: pillColor,
child: leading,
),
),
),
const SizedBox(width: 8),
],
@@ -241,57 +366,17 @@ class _SegmentedBarState extends State<SegmentedBar> {
child: Align(
alignment: AlignmentDirectional.centerStart,
heightFactor: 1,
child: titleWidget != null
// The main pill fills the row, up to its cap.
? ConstrainedBox(
constraints: BoxConstraints(
maxWidth: titleWidgetMaxWidth,
child: !hasTitle
? const SizedBox.shrink()
: SegmentedEntrance(
key: ValueKey(
titleWidget != null
? 'segmented.field'
: 'segmented.title',
),
child: SegmentedPill(
cupertino: cupertino,
fill: pillColor,
child: SizedBox(
width: double.infinity,
child: titleWidget,
),
),
)
: showTitle && !widget.expandableTitle
? titlePill(false)
: showTitle
? LayoutBuilder(
builder: (context, titleSpace) => OverlayPortal(
controller: _expandedTitle,
overlayChildBuilder: (context) =>
!(_expanded && widget.expandableTitle)
? const SizedBox.shrink()
: Positioned(
top: 0,
left: 0,
child: CompositedTransformFollower(
link: _titleLink,
showWhenUnlinked: false,
child: ConstrainedBox(
constraints: BoxConstraints(
maxWidth: titleSpace.maxWidth,
),
child: BackdropGroup(
backdropKey: _expandedTitleBackdrop,
child: titlePill(true),
),
),
),
),
child: CompositedTransformTarget(
link: _titleLink,
child: ExcludeSemantics(
excluding: _expanded && widget.expandableTitle,
child: titlePill(false),
),
),
),
)
: const SizedBox.shrink(),
delay: titleDelay,
child: _titleIsland(context, titlePill),
),
),
),
if (actions.isNotEmpty) ...[
@@ -311,15 +396,27 @@ class _SegmentedBarState extends State<SegmentedBar> {
constraints.maxWidth * SegmentedBar.minTitleShare,
),
),
child: SegmentedPill(
cupertino: cupertino,
fill: pillColor,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Flexible(child: actions.first),
...actions.skip(1),
],
child: SegmentedEntrance(
key: const ValueKey('segmented.actions'),
delay: actionsDelay,
// A lone action is the whole pill, so the pill sinks when it
// is pressed; beside others, each action sinks on its own.
child: SegmentedPress(
cupertino: cupertino,
tint: foregroundColor,
enabled: actions.length == 1,
child: SegmentedPill(
cupertino: cupertino,
fill: pillColor,
child: Row(
mainAxisSize: MainAxisSize.min,
children: _actionIslands(
actions,
cupertino: cupertino,
play: !freshActions,
),
),
),
),
),
),
@@ -0,0 +1,245 @@
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<SegmentedEntrance> createState() => _SegmentedEntranceState();
}
class _SegmentedEntranceState extends State<SegmentedEntrance>
with SingleTickerProviderStateMixin {
AnimationController? _controller;
Animation<double> _pop = kAlwaysCompleteAnimation;
Animation<double> _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<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 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,
),
);
},
),
);
}
}
+23 -3
View File
@@ -2,6 +2,7 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'glass_pill.dart';
import 'segmented_bar_motion.dart';
/// The smallest height of every piece of a [SegmentedBar]: the platform's
/// minimum touch target, so a round Back is a circle.
@@ -216,7 +217,11 @@ class SegmentedTitlePill extends StatelessWidget {
: InkWell(key: buttonKey, onTap: onPressed, child: content),
);
}
return SegmentedPill(
final titleAction = this.titleAction;
// A pressable title sinks as a whole pill; beside a title action, the
// title and the action each give their own feedback.
final split = titleAction != null;
final pill = SegmentedPill(
cupertino: cupertino,
fill: fill,
child: Row(
@@ -224,11 +229,26 @@ class SegmentedTitlePill extends StatelessWidget {
children: [
Flexible(
fit: FlexFit.loose,
child: IntrinsicWidth(child: body),
child: IntrinsicWidth(
child: split && onPressed != null
? SegmentedPress(
cupertino: cupertino,
tint: foreground,
child: body,
)
: body,
),
),
?titleAction,
if (titleAction != null)
SegmentedPress(
cupertino: cupertino,
tint: foreground,
child: titleAction,
),
],
),
);
if (split || onPressed == null) return pill;
return SegmentedPress(cupertino: cupertino, tint: foreground, child: pill);
}
}
+33
View File
@@ -47,6 +47,39 @@ abstract final class Motion {
/// The icon's scale at the top of its bounce.
static const double tabBounceScale = 1.08;
/// Duration of each top-bar island's entrance pop (Back, title, actions).
static const Duration barEntranceDuration = Duration(milliseconds: 240);
/// Delay between neighbouring top-bar islands entering together, from the
/// leading edge.
static const Duration barEntranceStagger = Duration(milliseconds: 40);
/// The scale at which a top-bar island starts its entrance.
static const double barEntranceScaleBegin = 0.85;
/// The fraction of an island's entrance spent fading in.
static const double barEntranceOpacityPortion = 0.6;
/// The gentle overshoot of an island's entrance.
static const Curve barEntranceCurve = Curves.easeOutBack;
/// How long a pressed top-bar control takes to sink.
static const Duration barPressDuration = Duration(milliseconds: 90);
/// The scale of a top-bar control while it is held down.
static const double barPressScale = 0.94;
/// The opacity of the pressed tint over a top-bar control.
static const double barPressTintOpacity = 0.14;
/// The spring a released top-bar control returns on: quick, with a
/// barely visible overshoot.
static const SpringDescription barReleaseSpring = SpringDescription(
mass: 1,
stiffness: 520,
damping: 28,
);
static const Curve curve = Curves.easeOutCubic;
/// [duration], or zero under the system's reduce-motion setting.