Tabs: the selected button grows about 20%, neighbours keep their gaps

The selected tab's whole button (62 px Material, 60 px Cupertino, from 52/50)
and its icon grow with the selection in the layout, so the other buttons move
over and every gap stays the same; the previous selection shrinks back. The
row is as tall as the selected button with the others centred on it.
This commit is contained in:
2026-11-02 22:30:25 +00:00
parent 6fcdbcaec2
commit 2176b396ac
7 changed files with 137 additions and 52 deletions
@@ -3,6 +3,7 @@ import 'dart:ui' show SemanticsRole;
import 'package:flutter/widgets.dart';
import '../motion/motion.dart';
import 'floating_bar_layout.dart';
import 'floating_tab_button.dart';
import 'floating_tab_motion.dart';
@@ -71,10 +72,22 @@ class FloatingDestinationBar extends StatefulWidget {
static double buttonGap({required bool cupertino}) =>
cupertino ? cupertinoButtonGap : materialButtonGap;
/// The width of a row of [tabs] buttons, when the space allows it.
/// The selected button's size, about [Motion.tabSelectedScale] times the
/// others (whole pixels, so layout stays exact).
static const double materialSelectedButtonSize = 62;
static const double cupertinoSelectedButtonSize = 60;
static double selectedButtonSize({required bool cupertino}) =>
cupertino ? cupertinoSelectedButtonSize : materialSelectedButtonSize;
/// The width of a row of [tabs] buttons (one of them selected), when the
/// space allows it.
static double rowWidth({required bool cupertino, required int tabs}) =>
buttonSize(cupertino: cupertino) * tabs +
buttonGap(cupertino: cupertino) * math.max(0, tabs - 1);
tabs == 0
? 0
: buttonSize(cupertino: cupertino) * (tabs - 1) +
selectedButtonSize(cupertino: cupertino) +
buttonGap(cupertino: cupertino) * math.max(0, tabs - 1);
@override
State<FloatingDestinationBar> createState() => _FloatingDestinationBarState();
@@ -119,6 +132,12 @@ class _FloatingDestinationBarState extends State<FloatingDestinationBar>
items.map((item) => '${item.key ?? item.label}:${item.label}').join('|');
double get _size => FloatingDestinationBar.buttonSize(cupertino: cupertino);
double get _selectedSize =>
FloatingDestinationBar.selectedButtonSize(cupertino: cupertino);
/// The buttons' natural width side by side, the selected one larger.
double get _buttonsWidth =>
items.isEmpty ? 0 : _size * (items.length - 1) + _selectedSize;
@override
Widget build(BuildContext context) {
@@ -165,14 +184,17 @@ class _FloatingDestinationBarState extends State<FloatingDestinationBar>
final gap = FloatingDestinationBar.buttonGap(cupertino: cupertino);
final count = items.length;
if (count < 2 || !width.isFinite) return gap;
return (width - count * _size).clamp(0.0, gap * (count - 1)) / (count - 1);
return (width - _buttonsWidth).clamp(0.0, gap * (count - 1)) / (count - 1);
}
Widget _row(BuildContext context, double width) {
final gap = _gap(width);
// As tall as the selected (largest) button; the others are centred on
// it, and every gap stays the same while a button grows or shrinks.
Widget row = Row(
key: FloatingDestinationBar.rowKey,
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
for (var index = 0; index < items.length; index += 1) ...[
if (index > 0) SizedBox(width: gap),
@@ -181,10 +203,10 @@ class _FloatingDestinationBarState extends State<FloatingDestinationBar>
],
);
// Narrower than the buttons themselves: the whole row shrinks.
if (width.isFinite && width < items.length * _size) {
if (width.isFinite && width < _buttonsWidth) {
row = SizedBox(
width: math.max(0.0, width),
height: _size,
height: _selectedSize,
child: FittedBox(fit: BoxFit.scaleDown, child: row),
);
}
@@ -204,23 +226,31 @@ class _FloatingDestinationBarState extends State<FloatingDestinationBar>
Widget _button(BuildContext context, int index) {
final item = items[index];
final selected = index == currentIndex;
final iconSize = cupertino
? FloatingDestinationBar.cupertinoIconSize
: FloatingDestinationBar.materialIconSize;
// The selected button grows (and a deselected one shrinks back) in the
// layout, so its neighbours move over and keep their gaps.
return FloatingTabEntrance(
motion: _motion,
index: index,
child: FloatingTabButton(
key: item.key,
cupertino: cupertino,
motion: _motion,
index: index,
count: items.length,
selected: selected,
size: _size,
iconSize: cupertino
? FloatingDestinationBar.cupertinoIconSize
: FloatingDestinationBar.materialIconSize,
label: item.label,
icon: selected ? item.selectedIcon ?? item.icon : item.icon,
onTap: () => widget.onTap(index),
child: TweenAnimationBuilder<double>(
tween: Tween(end: selected ? _selectedSize : _size),
duration: Motion.of(context, Motion.tabSelectDuration),
curve: Motion.curve,
builder: (context, size, _) => FloatingTabButton(
key: item.key,
cupertino: cupertino,
motion: _motion,
index: index,
count: items.length,
selected: selected,
size: size,
iconSize: iconSize * size / _size,
label: item.label,
icon: selected ? item.selectedIcon ?? item.icon : item.icon,
onTap: () => widget.onTap(index),
),
),
);
}
@@ -156,11 +156,6 @@ class FloatingTabButton extends StatelessWidget {
),
child: FloatingTabBounce(motion: motion, index: index, child: icon),
);
// The selected icon is a little larger, growing with the selection.
glyph = Transform.scale(
scale: 1 + (Motion.tabSelectedIconScale - 1) * t,
child: glyph,
);
final reveal = GlassReveal.maybeOf(context);
if (reveal != null) {
// The icon sits above the glass, so it fades with it here.
+5 -1
View File
@@ -8,7 +8,11 @@ const double kContentMaxWidth = 1100;
/// Centres [child] and caps its width, keeping the incoming height constraints.
class ContentWidth extends StatelessWidget {
const ContentWidth({super.key, required this.child, this.maxWidth = kContentMaxWidth});
const ContentWidth({
super.key,
required this.child,
this.maxWidth = kContentMaxWidth,
});
final Widget child;
final double maxWidth;
+4 -3
View File
@@ -43,9 +43,10 @@ abstract final class Motion {
/// The icon's scale at the top of its bounce.
static const double tabBounceScale = 1.08;
/// How much larger the selected tab's icon is than the others; it grows
/// and shrinks with the selection tint.
static const double tabSelectedIconScale = 1.15;
/// How much larger the selected tab's button (and its icon) is than the
/// others; it grows and shrinks with the selection tint, and its
/// neighbours keep their gaps to it.
static const double tabSelectedScale = 1.2;
/// Duration of each top-bar island's entrance pop (Back, title, actions).
static const Duration barEntranceDuration = Duration(milliseconds: 240);