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);
@@ -1,7 +1,6 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/ui/motion/motion.dart';
import 'package:pipelines_app/ui/adaptive/floating_destination_bar.dart';
import 'package:pipelines_app/ui/adaptive/glass_pill.dart';
@@ -116,7 +115,12 @@ void main() {
);
for (var index = 0; index < 3; index += 1) {
final circle = button(tester, index);
expect(circle.size, Size(diameter, diameter));
// Tab 0 is selected: its whole button is larger than the others.
final across = index == 0
? FloatingDestinationBar.selectedButtonSize(cupertino: cupertino)
: diameter;
expect(circle.width, moreOrLessEquals(across));
expect(circle.height, moreOrLessEquals(across));
expect(
tester
.widget<GlassPill>(
@@ -129,9 +133,12 @@ void main() {
isA<CircleBorder>(),
);
final icon = tester.getRect(find.byIcon(icons[index]));
// Tab 0 is selected: its icon is a little larger than the others.
final side = index == 0
? iconSize * Motion.tabSelectedIconScale
? iconSize *
FloatingDestinationBar.selectedButtonSize(
cupertino: cupertino,
) /
FloatingDestinationBar.buttonSize(cupertino: cupertino)
: iconSize;
expect(icon.width, moreOrLessEquals(side));
expect(icon.height, moreOrLessEquals(side));
@@ -156,10 +163,18 @@ void main() {
final box = row(tester);
expect(box.right, size.width - 16);
expect(box.bottom, size.height - 16);
expect(box.height, diameter);
// As tall as the selected (larger) button.
expect(
box.height,
moreOrLessEquals(
FloatingDestinationBar.selectedButtonSize(cupertino: cupertino),
),
);
expect(
box.width,
FloatingDestinationBar.rowWidth(cupertino: cupertino, tabs: count),
moreOrLessEquals(
FloatingDestinationBar.rowWidth(cupertino: cupertino, tabs: count),
),
);
expect(button(tester, 0).left, box.left);
expect(button(tester, count - 1).right, box.right);
@@ -168,7 +183,11 @@ void main() {
button(tester, index).left - button(tester, index - 1).right,
moreOrLessEquals(gap),
);
expect(button(tester, index).top, box.top);
// Every button is centred on the row, whatever its size.
expect(
button(tester, index).center.dy,
moreOrLessEquals(box.center.dy),
);
}
});
}
@@ -229,9 +248,16 @@ void main() {
final box = row(tester);
expect(box.right, 320 - 16);
expect(box.left, greaterThanOrEqualTo(16));
final narrowed = (288 - 5 * diameter) / 4;
// Tab 4 is selected and larger; the gaps narrow around it.
final selected = FloatingDestinationBar.selectedButtonSize(
cupertino: cupertino,
);
final narrowed = (288 - 4 * diameter - selected) / 4;
for (var index = 1; index < 5; index += 1) {
expect(button(tester, index).width, diameter);
expect(
button(tester, index).width,
moreOrLessEquals(index == 4 ? selected : diameter),
);
expect(
button(tester, index).left - button(tester, index - 1).right,
moreOrLessEquals(narrowed < gap ? narrowed : gap),
@@ -1,7 +1,6 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/ui/motion/motion.dart';
import 'package:pipelines_app/shell/adaptive_shell.dart';
import 'package:pipelines_app/shell/destination.dart';
import 'package:pipelines_app/ui/adaptive/floating_destination_bar.dart';
@@ -117,7 +116,10 @@ void main() {
for (var index = 0; index < 3; index += 1) {
final tab = find.byKey(tabKey(index));
expect(opacityOf(tester, tab), 0);
expect(widthOf(tester, tab), moreOrLessEquals(size * 0.6));
final rest = index == 0
? FloatingDestinationBar.selectedButtonSize(cupertino: cupertino)
: size;
expect(widthOf(tester, tab), moreOrLessEquals(rest * 0.6));
}
await tester.pump(const Duration(milliseconds: 130));
final first = widthOf(tester, find.byKey(tabKey(0)));
@@ -129,7 +131,13 @@ void main() {
for (var index = 0; index < 3; index += 1) {
final tab = find.byKey(tabKey(index));
expect(opacityOf(tester, tab), 1);
expect(widthOf(tester, tab), size);
// Tab 0 is selected, so it settles at the larger size.
expect(
widthOf(tester, tab),
index == 0
? FloatingDestinationBar.selectedButtonSize(cupertino: cupertino)
: size,
);
}
});
@@ -145,13 +153,12 @@ void main() {
for (var index = 0; index < 3; index += 1) {
final tab = find.byKey(tabKey(index));
expect(opacityOf(tester, tab), 1, reason: 'tab $index faded');
// Only the press may shrink the tapped button, and only a
// little; a replayed pop starts at 0.6.
// The selection grows and shrinks buttons between their two
// sizes and the press sinks the tapped one a little; a replayed
// pop would start at 0.6.
expect(
widthOf(tester, tab),
index == target
? greaterThan(size * 0.9)
: moreOrLessEquals(size),
greaterThan(size * 0.9),
reason: 'tab $index popped again',
);
}
@@ -222,7 +229,11 @@ void main() {
// At rest the selected icon is a little larger than the others.
expect(
iconWidth(tester, 2),
moreOrLessEquals(iconSize * Motion.tabSelectedIconScale),
moreOrLessEquals(
iconSize *
FloatingDestinationBar.selectedButtonSize(cupertino: cupertino) /
FloatingDestinationBar.buttonSize(cupertino: cupertino),
),
);
expect(iconWidth(tester, 0), moreOrLessEquals(iconSize));
expect(iconWidth(tester, 1), moreOrLessEquals(iconSize));
@@ -251,10 +262,18 @@ void main() {
await tester.pump();
await tester.pump(const Duration(milliseconds: 120));
expect(widthOf(tester, tab), lessThan(size * 0.97));
expect(widthOf(tester, find.byKey(tabKey(0))), size);
// Tab 0 is selected, so it is the larger button; the press leaves it.
expect(
widthOf(tester, find.byKey(tabKey(0))),
FloatingDestinationBar.selectedButtonSize(cupertino: cupertino),
);
await gesture.up();
await tester.pumpAndSettle();
expect(widthOf(tester, tab), size);
// The tap selected it: it springs back to the selected size.
expect(
widthOf(tester, tab),
FloatingDestinationBar.selectedButtonSize(cupertino: cupertino),
);
});
testWidgets('$host: under reduced motion the tint jumps and the buttons '
@@ -273,7 +292,11 @@ void main() {
// No bounce, and the selected icon jumps straight to its larger size.
expect(
iconWidth(tester, 2),
moreOrLessEquals(iconSize * Motion.tabSelectedIconScale),
moreOrLessEquals(
iconSize *
FloatingDestinationBar.selectedButtonSize(cupertino: cupertino) /
FloatingDestinationBar.buttonSize(cupertino: cupertino),
),
);
expect(iconWidth(tester, 0), moreOrLessEquals(iconSize));
});
@@ -313,7 +336,13 @@ void main() {
reason: '${tab.name} faded switching to ${next.name}',
);
if (tab != next) {
expect(widthOf(tester, finder), size, reason: tab.name);
// The previous selection shrinks back to its size; nothing
// pops again from 0.6.
expect(
widthOf(tester, finder),
greaterThan(size * 0.9),
reason: tab.name,
);
}
}
}
@@ -187,7 +187,7 @@ void main() {
final pill = tester.getRect(find.byKey(FloatingDestinationBar.rowKey));
expect(pill.top, 64);
// The pill is the bar's own height: leftover media padding is not added.
expect(pill.height, FloatingDestinationBar.materialButtonSize);
expect(pill.height, FloatingDestinationBar.materialSelectedButtonSize);
// Wide: sized to its tabs at the leading edge, under the title.
expect(pill.left, FloatingDestinationBar.topMargin);
expect(pill.width, lessThan(400));