Animate and space floating tab bars

This commit is contained in:
2026-11-01 13:13:15 +00:00
parent fea3d770b4
commit 4007599c99
4 changed files with 252 additions and 36 deletions
+139 -24
View File
@@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
import 'floating_bar_layout.dart';
import 'glass_pill.dart';
import '../motion/motion.dart';
/// A tab bar which floats above the bottom edge in a frosted [GlassPill]
/// instead of becoming a full-width system bar. Used for the app's
@@ -79,12 +80,15 @@ class FloatingDestinationBar extends StatelessWidget {
static const double materialEdgeGap = _materialEdgeGap;
static const double cupertinoEdgeGap = _cupertinoEdgeGap;
/// Width of one tab: its circle and one edge gap. The pill is one more edge
/// gap wider than its tabs.
/// The additional breathing room between neighbouring selection circles.
static const double materialNeighbourGap = 15;
static const double cupertinoNeighbourGap = 13;
/// Width of one equal native tab slot at the requested neighbour spacing.
static const double materialTabWidth =
materialHighlightSize + materialEdgeGap;
materialHighlightSize + materialNeighbourGap;
static const double cupertinoTabWidth =
cupertinoHighlightSize + cupertinoEdgeGap;
cupertinoHighlightSize + cupertinoNeighbourGap;
/// The side margin at [FloatingBarEdge.top], matching the top bar's own.
static const double topMargin = 12;
@@ -148,6 +152,8 @@ class FloatingDestinationBar extends StatelessWidget {
double get _highlight =>
cupertino ? cupertinoHighlightSize : materialHighlightSize;
double get _gap => cupertino ? cupertinoEdgeGap : materialEdgeGap;
double get _neighbourGap =>
cupertino ? cupertinoNeighbourGap : materialNeighbourGap;
/// The width the icon-only tabs need side by side. Icons do not scale with
/// text, so the pill keeps its size at any text scale.
@@ -156,8 +162,12 @@ class FloatingDestinationBar extends StatelessWidget {
/// The width of a pill holding [tabs] tabs, when the space allows it.
static double pillWidth({required bool cupertino, required int tabs}) =>
cupertino
? cupertinoTabWidth * tabs + cupertinoEdgeGap
: materialTabWidth * tabs + materialEdgeGap;
? cupertinoHighlightSize * tabs +
2 * cupertinoEdgeGap +
math.max(0, tabs - 1) * cupertinoNeighbourGap
: materialHighlightSize * tabs +
2 * materialEdgeGap +
math.max(0, tabs - 1) * materialNeighbourGap;
/// Insets the host bar's equal tab slots so the outer circles sit the edge
/// gap from the pill's sides. A pill squeezed below its tabs' width keeps
@@ -165,14 +175,31 @@ class FloatingDestinationBar extends StatelessWidget {
/// the circles still fit side by side.
Widget _edgeAligned(double width, Widget bar) {
final count = items.length;
var inset = _gap / 2;
if (count > 1) {
final slot = (width - _highlight - 2 * _gap) / (count - 1);
inset = slot >= _highlight ? (width - slot * count) / 2 : 0;
}
return Padding(
padding: EdgeInsets.symmetric(horizontal: math.max(0, inset)),
child: bar,
if (count < 2) return bar;
// The native bars use equal-width slots. Let their slot centers be the
// desired circle centers, and translate the slots by half the difference
// between the outer and neighbour gaps. The pill clips the harmless
// overflow of the native slot backgrounds, while the circles remain
// exactly edge-aligned.
final available = math.max(0.0, width - 2 * _gap - count * _highlight);
final neighbour = math.min(_neighbourGap, available / (count - 1));
final slot = _highlight + math.max(0, neighbour);
final barWidth = slot * count;
final offset = _gap - neighbour / 2;
return ClipRect(
child: SizedBox(
width: width,
height: cupertino ? cupertinoHeight : materialHeight,
child: OverflowBox(
alignment: AlignmentDirectional.topStart,
minWidth: 0,
maxWidth: double.infinity,
child: Transform.translate(
offset: Offset(offset, 0),
child: SizedBox(width: barWidth, child: bar),
),
),
),
);
}
@@ -199,16 +226,19 @@ class FloatingDestinationBar extends StatelessWidget {
selectedIndex: currentIndex,
onDestinationSelected: onTap,
destinations: [
for (final item in items)
for (var index = 0; index < items.length; index += 1)
NavigationDestination(
key: item.key,
icon: item.icon,
key: items[index].key,
icon: _entrance(items[index].icon, index),
selectedIcon: _SelectionCircle(
color: scheme.secondaryContainer,
ring: _materialRing,
child: item.selectedIcon ?? item.icon,
child: _entrance(
items[index].selectedIcon ?? items[index].icon,
index,
),
),
label: item.label,
label: items[index].label,
),
],
),
@@ -221,14 +251,18 @@ class FloatingDestinationBar extends StatelessWidget {
/// the selection reads at a glance, not by colour alone.
Widget _cupertinoBar(BuildContext context) {
final active = CupertinoTheme.of(context).primaryColor;
Widget tab(FloatingDestinationItem item, {required bool selected}) {
Widget tab(
FloatingDestinationItem item, {
required bool selected,
required int index,
}) {
final icon = selected
? _SelectionCircle(
color: active.withValues(alpha: 0.16),
ring: _cupertinoRing,
child: item.selectedIcon ?? item.icon,
child: _entrance(item.selectedIcon ?? item.icon, index),
)
: item.icon;
: _entrance(item.icon, index);
return KeyedSubtree(
key: item.key,
child: Semantics(
@@ -256,13 +290,94 @@ class FloatingDestinationBar extends StatelessWidget {
items: [
for (var index = 0; index < items.length; index += 1)
BottomNavigationBarItem(
icon: tab(items[index], selected: false),
activeIcon: tab(items[index], selected: true),
icon: tab(items[index], selected: false, index: index),
activeIcon: tab(items[index], selected: true, index: index),
),
],
),
);
}
Widget _entrance(Widget child, int index) => _TabEntrance(
key: ValueKey<Object?>('${_tabSetIdentity}_$index'),
index: index,
count: items.length,
child: child,
);
String get _tabSetIdentity =>
items.map((item) => '${item.key ?? item.label}:${item.label}').join('|');
}
class _TabEntrance extends StatefulWidget {
const _TabEntrance({
super.key,
required this.index,
required this.count,
required this.child,
});
final int index;
final int count;
final Widget child;
@override
State<_TabEntrance> createState() => _TabEntranceState();
}
class _TabEntranceState extends State<_TabEntrance>
with SingleTickerProviderStateMixin {
late final AnimationController _controller;
bool _started = false;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration:
Motion.standard + Motion.tabEntranceStagger * (widget.count - 1),
);
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
if (_started) return;
_started = true;
final duration = Motion.of(context, Motion.standard);
_controller.duration =
duration + Motion.tabEntranceStagger * (widget.count - 1);
if (duration == Duration.zero) {
_controller.value = 1;
} else {
_controller.forward();
}
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final total = _controller.duration!.inMicroseconds;
final start = Motion.tabEntranceStagger.inMicroseconds * widget.index;
final begin = total == 0 ? 0.0 : start / total;
final animation = CurvedAnimation(
parent: _controller,
curve: Interval(begin, 1, curve: Motion.curve),
);
return FadeTransition(
opacity: animation,
child: ScaleTransition(
scale: Tween<double>(begin: 0.88, end: 1).animate(animation),
child: widget.child,
),
);
}
}
/// The selected tab's icon (and any badge on it) in a tinted circle, [ring]
+3
View File
@@ -10,6 +10,9 @@ abstract final class Motion {
/// A section opening or closing, a bar changing its actions.
static const Duration standard = Duration(milliseconds: 200);
/// Delay between neighbouring controls entering as one group.
static const Duration tabEntranceStagger = Duration(milliseconds: 40);
static const Curve curve = Curves.easeOutCubic;
/// [duration], or zero under the system's reduce-motion setting.