94a41ff563
The per-tab entrance pop lived in each icon, but the hosts swap a tab's icon subtree when it is selected or deselected (Material wrapped the selected icon in its own circle; Cupertino did the same in activeIcon), so every tab switch remounted those icons and replayed their pop. The bar now owns the entrance, highlight slide and bounce animations and restarts the entrance only when its set of tabs changes. One highlight behind the icons glides to the new tab (250 ms, stretching to 1.25x wide mid-move) and the new icon bounces (1.08x, 180 ms); reduced motion jumps. Cupertino paints it from tab 0 above the host's backdrop blur.
252 lines
7.8 KiB
Dart
252 lines
7.8 KiB
Dart
import 'dart:math' as math;
|
|
|
|
import 'package:flutter/widgets.dart';
|
|
|
|
import '../motion/motion.dart';
|
|
|
|
/// The animations of one floating tab bar: its tabs popping in, the
|
|
/// selection highlight gliding between tabs and the newly selected icon's
|
|
/// bounce.
|
|
///
|
|
/// They live with the bar, not with each tab's icon, because the host
|
|
/// navigation controls rebuild a tab's icon subtree whenever it is selected
|
|
/// or deselected: state kept in the icons would restart with every tap.
|
|
class FloatingTabMotion {
|
|
FloatingTabMotion(TickerProvider vsync)
|
|
: entrance = AnimationController(vsync: vsync),
|
|
slide = AnimationController(
|
|
vsync: vsync,
|
|
duration: Motion.tabHighlightDuration,
|
|
value: 1,
|
|
),
|
|
bounce = AnimationController(
|
|
vsync: vsync,
|
|
duration: Motion.tabBounceDuration,
|
|
value: 1,
|
|
);
|
|
|
|
/// Drives every tab's entrance; tab n plays its pop in its own window.
|
|
final AnimationController entrance;
|
|
final AnimationController slide;
|
|
final AnimationController bounce;
|
|
|
|
int _count = 0;
|
|
final List<({Animation<double> pop, Animation<double> opacity})> _entrances =
|
|
[];
|
|
double _from = 0;
|
|
int _to = 0;
|
|
|
|
/// Plays the entrance of a bar of [count] tabs, from the start, and places
|
|
/// the highlight on [selected] without a move.
|
|
void enter(
|
|
BuildContext context, {
|
|
required int count,
|
|
required int selected,
|
|
}) {
|
|
_count = count;
|
|
final duration = Motion.of(context, Motion.tabEntranceDuration);
|
|
entrance.duration =
|
|
duration + Motion.tabEntranceStagger * math.max(0, count - 1);
|
|
if (duration == Duration.zero) {
|
|
entrance.value = 1;
|
|
} else {
|
|
entrance.forward(from: 0);
|
|
}
|
|
_splitEntrance();
|
|
_from = selected.toDouble();
|
|
_to = selected;
|
|
slide.value = 1;
|
|
bounce.value = 1;
|
|
}
|
|
|
|
/// Glides the highlight from where it is now to [selected] and bounces
|
|
/// that tab's icon. Under reduced motion the highlight jumps.
|
|
void select(BuildContext context, int selected) {
|
|
if (selected == _to) return;
|
|
_from = position;
|
|
_to = selected;
|
|
if (Motion.of(context, Motion.tabHighlightDuration) == Duration.zero) {
|
|
slide.value = 1;
|
|
bounce.value = 1;
|
|
return;
|
|
}
|
|
slide.forward(from: 0);
|
|
bounce.forward(from: 0);
|
|
}
|
|
|
|
double get _travel => Motion.curve.transform(slide.value);
|
|
|
|
/// The highlight's position in tab slots: 1.5 is halfway from tab 1 to 2.
|
|
double get position => _from + (_to - _from) * _travel;
|
|
|
|
/// The highlight's width over its height: 1 at rest, at most
|
|
/// [Motion.tabHighlightStretch] halfway through a move.
|
|
double get stretch => _from == _to
|
|
? 1
|
|
: 1 + (Motion.tabHighlightStretch - 1) * math.sin(math.pi * _travel);
|
|
|
|
/// The scale of tab [index]'s icon: only the newly selected one bounces.
|
|
double scaleOf(int index) {
|
|
if (index != _to || bounce.isCompleted) return 1;
|
|
final t = bounce.value;
|
|
final rise = t < 0.5 ? t * 2 : (1 - t) * 2;
|
|
return 1 + (Motion.tabBounceScale - 1) * Curves.easeOut.transform(rise);
|
|
}
|
|
|
|
/// Tab [index]'s share of [entrance]: its pop, then its fade.
|
|
({Animation<double> pop, Animation<double> opacity}) entranceOf(int index) =>
|
|
index >= 0 && index < _entrances.length
|
|
? _entrances[index]
|
|
: (pop: kAlwaysCompleteAnimation, opacity: kAlwaysCompleteAnimation);
|
|
|
|
/// Splits [entrance] into each tab's window, once per entrance.
|
|
void _splitEntrance() {
|
|
_disposeEntrances();
|
|
final total = entrance.duration!.inMicroseconds;
|
|
if (total == 0) return;
|
|
final window = Motion.tabEntranceDuration.inMicroseconds;
|
|
for (var index = 0; index < _count; index += 1) {
|
|
final start = Motion.tabEntranceStagger.inMicroseconds * index;
|
|
final begin = (start / total).clamp(0.0, 1.0);
|
|
final end = ((start + window) / total).clamp(begin, 1.0);
|
|
final faded =
|
|
((start + window * Motion.tabEntranceOpacityPortion) / total).clamp(
|
|
begin,
|
|
1.0,
|
|
);
|
|
_entrances.add((
|
|
pop: CurvedAnimation(
|
|
parent: entrance,
|
|
curve: Interval(begin, end, curve: Curves.easeOutBack),
|
|
),
|
|
opacity: CurvedAnimation(
|
|
parent: entrance,
|
|
curve: Interval(begin, faded, curve: Curves.easeOut),
|
|
),
|
|
));
|
|
}
|
|
}
|
|
|
|
void _disposeEntrances() {
|
|
for (final animations in _entrances) {
|
|
(animations.pop as CurvedAnimation).dispose();
|
|
(animations.opacity as CurvedAnimation).dispose();
|
|
}
|
|
_entrances.clear();
|
|
}
|
|
|
|
void dispose() {
|
|
_disposeEntrances();
|
|
entrance.dispose();
|
|
slide.dispose();
|
|
bounce.dispose();
|
|
}
|
|
}
|
|
|
|
/// Tab [index]'s icon (or the highlight behind it) popping in with the bar,
|
|
/// and bouncing when it becomes selected.
|
|
class FloatingTabEntrance extends StatelessWidget {
|
|
const FloatingTabEntrance({
|
|
super.key,
|
|
required this.motion,
|
|
required this.index,
|
|
required this.child,
|
|
this.bounces = true,
|
|
});
|
|
|
|
final FloatingTabMotion motion;
|
|
final int index;
|
|
final bool bounces;
|
|
final Widget child;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final entrance = motion.entranceOf(index);
|
|
return FadeTransition(
|
|
opacity: entrance.opacity,
|
|
// Tabs (and their badge counts) stay announced while they fade in.
|
|
alwaysIncludeSemantics: true,
|
|
child: AnimatedBuilder(
|
|
animation: Listenable.merge([entrance.pop, motion.bounce]),
|
|
child: child,
|
|
builder: (context, child) {
|
|
final pop = entrance.pop.value;
|
|
final scale =
|
|
Motion.tabEntranceScaleBegin +
|
|
(1 - Motion.tabEntranceScaleBegin) * pop;
|
|
return Transform.translate(
|
|
offset: Offset(0, (1 - pop) * Motion.tabEntranceSlideDistance),
|
|
child: Transform.scale(
|
|
scale: scale * (bounces ? motion.scaleOf(index) : 1),
|
|
child: child,
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// The one selection highlight of a bar, behind its icons: a circle of
|
|
/// [size] centred on the selected tab, gliding (and stretching along the way)
|
|
/// to a newly selected one. It takes no taps and says nothing; the host's
|
|
/// tabs carry the selected state.
|
|
///
|
|
/// It is laid out over tab 0 (its slot, or its icon) and paints itself
|
|
/// [slot] widths along the reading direction per tab, unclipped.
|
|
class FloatingTabHighlight extends StatelessWidget {
|
|
const FloatingTabHighlight({
|
|
super.key,
|
|
required this.motion,
|
|
required this.slot,
|
|
required this.size,
|
|
required this.color,
|
|
});
|
|
|
|
final FloatingTabMotion motion;
|
|
|
|
/// The distance between neighbouring tabs' centres.
|
|
final double slot;
|
|
final double size;
|
|
final Color color;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final direction = Directionality.of(context) == TextDirection.rtl ? -1 : 1;
|
|
return IgnorePointer(
|
|
child: ExcludeSemantics(
|
|
child: AnimatedBuilder(
|
|
animation: motion.slide,
|
|
builder: (context, _) {
|
|
final width = size * motion.stretch;
|
|
return OverflowBox(
|
|
minWidth: 0,
|
|
minHeight: 0,
|
|
maxWidth: double.infinity,
|
|
maxHeight: double.infinity,
|
|
child: Transform.translate(
|
|
offset: Offset(direction * slot * motion.position, 0),
|
|
child: FloatingTabEntrance(
|
|
motion: motion,
|
|
index: motion.position.round(),
|
|
bounces: false,
|
|
child: SizedBox(
|
|
width: width,
|
|
height: size,
|
|
child: DecoratedBox(
|
|
decoration: ShapeDecoration(
|
|
color: color,
|
|
shape: const StadiumBorder(),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|