Merge: tabs as separate round glass buttons

This commit is contained in:
2026-11-02 18:44:27 +00:00
24 changed files with 916 additions and 1003 deletions
@@ -1,368 +0,0 @@
import 'dart:math' as math;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/ui/adaptive/floating_destination_bar.dart';
import 'package:pipelines_app/ui/adaptive/glass_pill.dart';
/// The floating tab bar is sized to its tabs at the bottom right on every
/// width, and its selection circle keeps one gap everywhere: the first and
/// last circles sit as far from the pill's sides as from its top and bottom,
/// and neighbouring circles are that far apart too, on both hosts.
void main() {
const icons = [
Icons.account_tree_outlined,
Icons.play_circle_outline,
Icons.chat_bubble_outline,
Icons.search,
Icons.settings_outlined,
];
Key tabKey(int index) => ValueKey('tab-$index');
Widget bar({
required bool cupertino,
required int count,
required int selected,
ValueChanged<int>? onTap,
}) => FloatingDestinationBar(
cupertino: cupertino,
currentIndex: selected,
onTap: onTap ?? (_) {},
items: [
for (var index = 0; index < count; index += 1)
FloatingDestinationItem(
key: tabKey(index),
icon: Icon(icons[index]),
label: 'Tab $index',
),
],
);
Future<void> pump(
WidgetTester tester, {
required bool cupertino,
required Size size,
required Widget child,
double textScale = 1,
}) async {
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final body = MediaQuery(
data: MediaQueryData(
size: size,
textScaler: TextScaler.linear(textScale),
),
child: child,
);
await tester.pumpWidget(
cupertino
? CupertinoApp(home: CupertinoPageScaffold(child: body))
: MaterialApp(home: Scaffold(body: body)),
);
await tester.pumpAndSettle();
}
/// The bar's one selection highlight, at rest on tab [index].
Rect circle(WidgetTester tester, int index) {
final rect = tester.getRect(
find.descendant(
of: find.byKey(FloatingDestinationBar.highlightKey),
matching: find.byType(DecoratedBox),
),
);
final icon = tester.getCenter(
find.descendant(
of: find.byKey(tabKey(index)),
matching: find.byIcon(icons[index]),
),
);
expect(rect.center.dx, moreOrLessEquals(icon.dx, epsilon: 0.5));
expect(rect.center.dy, moreOrLessEquals(icon.dy, epsilon: 0.5));
return rect;
}
Rect pill(WidgetTester tester) => tester.getRect(
find.descendant(
of: find.byType(FloatingDestinationBar),
matching: find.byType(GlassPill),
),
);
final placements = [
(name: 'wide 1200, 4 tabs', size: const Size(1200, 800), count: 4),
(name: 'phone 390, 3 tabs', size: const Size(390, 844), count: 3),
(name: 'phone 390, 4 tabs', size: const Size(390, 844), count: 4),
(name: 'phone 560, 5 tabs', size: const Size(560, 844), count: 5),
(name: 'phone 320, 5 tabs', size: const Size(320, 640), count: 5),
];
for (final cupertino in [false, true]) {
final host = cupertino ? 'Cupertino' : 'Material';
testWidgets('$host: icon and selection circle sizes grow together', (
tester,
) async {
await pump(
tester,
cupertino: cupertino,
size: const Size(390, 844),
child: Align(
alignment: Alignment.bottomCenter,
child: bar(cupertino: cupertino, count: 2, selected: 0),
),
);
final icon = tester.getRect(find.byIcon(icons.first));
final mark = circle(tester, 0);
final iconSize = cupertino
? FloatingDestinationBar.cupertinoIconSize
: FloatingDestinationBar.materialIconSize;
final highlightSize = cupertino
? FloatingDestinationBar.cupertinoHighlightSize
: FloatingDestinationBar.materialHighlightSize;
expect(icon.width, iconSize);
expect(icon.height, iconSize);
expect(mark.width, highlightSize);
expect(mark.height, highlightSize);
});
testWidgets(
'$host: tab entrance animates once and respects reduced motion',
(tester) async {
tester.platformDispatcher.accessibilityFeaturesTestValue =
const FakeAccessibilityFeatures(disableAnimations: false);
addTearDown(() {
tester.platformDispatcher.accessibilityFeaturesTestValue =
const FakeAccessibilityFeatures(disableAnimations: true);
});
tester.view.physicalSize = const Size(390, 844);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
var selected = 0;
await tester.pumpWidget(
cupertino
? CupertinoApp(
home: CupertinoPageScaffold(
child: MediaQuery(
data: const MediaQueryData(disableAnimations: false),
child: StatefulBuilder(
builder: (context, setState) => bar(
cupertino: cupertino,
count: 3,
selected: selected,
onTap: (index) => setState(() => selected = index),
),
),
),
),
)
: MaterialApp(
home: Scaffold(
body: MediaQuery(
data: const MediaQueryData(disableAnimations: false),
child: StatefulBuilder(
builder: (context, setState) => bar(
cupertino: cupertino,
count: 3,
selected: selected,
onTap: (index) => setState(() => selected = index),
),
),
),
),
),
);
Finder entranceOf(int index) => find
.descendant(
of: find.byKey(tabKey(index)),
matching: find.byWidgetPredicate(
(widget) =>
widget is FadeTransition && widget.child is AnimatedBuilder,
),
)
.last;
final entrance = entranceOf(0);
double firstOpacity() =>
tester.widget<FadeTransition>(entranceOf(0)).opacity.value;
double lastOpacity() =>
tester.widget<FadeTransition>(entranceOf(2)).opacity.value;
expect(firstOpacity(), 0);
expect(lastOpacity(), 0);
await tester.pump(const Duration(milliseconds: 130));
expect(firstOpacity(), greaterThan(0.9));
expect(lastOpacity(), lessThan(0.2));
await tester.pump(const Duration(milliseconds: 270));
expect(firstOpacity(), 1);
expect(lastOpacity(), 1);
await tester.tap(find.byKey(tabKey(1)));
await tester.pump();
await tester.pumpWidget(
MediaQuery(
data: const MediaQueryData(disableAnimations: true),
child: cupertino
? CupertinoApp(
home: CupertinoPageScaffold(
child: bar(cupertino: cupertino, count: 3, selected: 0),
),
)
: MaterialApp(
home: Scaffold(
body: bar(cupertino: cupertino, count: 3, selected: 0),
),
),
),
);
expect(tester.widget<FadeTransition>(entrance.first).opacity.value, 1);
},
);
for (final placement in placements) {
testWidgets('$host ${placement.name}: compact at the bottom right; the '
'outer circles keep the top gap from the sides and neighbours', (
tester,
) async {
final count = placement.count;
final size = placement.size;
final centres = <double>[];
for (final selected in [0, count - 1]) {
await pump(
tester,
cupertino: cupertino,
size: size,
child: Align(
alignment: Alignment.bottomCenter,
child: bar(
cupertino: cupertino,
count: count,
selected: selected,
),
),
);
final box = pill(tester);
expect(box.right, size.width - 16);
expect(box.bottom, size.height - 16);
final mark = circle(tester, selected);
expect(mark.width, mark.height);
final top = mark.top - box.top;
expect(box.bottom - mark.bottom, moreOrLessEquals(top, epsilon: 1));
final side = selected == 0
? mark.left - box.left
: box.right - mark.right;
expect(
side,
moreOrLessEquals(top, epsilon: 1),
reason: 'side gap $side vs top gap $top',
);
// The pill's round end is concentric with the outer circle.
expect(box.height / 2, moreOrLessEquals(top + mark.width / 2));
}
// Unselected icons sit where their circle would: neighbours are
// evenly spaced, with the larger named neighbour gap.
for (var index = 0; index < count; index += 1) {
centres.add(
tester
.getCenter(
find.descendant(
of: find.byKey(tabKey(index)),
matching: find.byIcon(icons[index]),
),
)
.dx,
);
}
final last = circle(tester, count - 1);
final box = pill(tester);
final edgeGap = last.top - box.top;
final requestedNeighbourGap = cupertino
? FloatingDestinationBar.cupertinoNeighbourGap
: FloatingDestinationBar.materialNeighbourGap;
final neighbourGap = math.min(
requestedNeighbourGap,
(box.width -
2 *
(cupertino
? FloatingDestinationBar.cupertinoEdgeGap
: FloatingDestinationBar.materialEdgeGap) -
count * last.width) /
(count - 1),
);
for (var index = 1; index < count; index += 1) {
expect(
centres[index] - centres[index - 1] - last.width,
moreOrLessEquals(neighbourGap, epsilon: 1),
);
}
expect(
neighbourGap - edgeGap,
moreOrLessEquals(
neighbourGap -
(cupertino
? FloatingDestinationBar.cupertinoEdgeGap
: FloatingDestinationBar.materialEdgeGap),
epsilon: 1,
),
);
expect(last.center.dx, moreOrLessEquals(centres.last, epsilon: 1));
});
}
testWidgets('$host phone: taps beside the compact bar reach the content, '
'taps on it the tabs', (tester) async {
final tapped = <int>[];
var content = 0;
await pump(
tester,
cupertino: cupertino,
size: const Size(390, 844),
child: Stack(
children: [
Positioned.fill(
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () => content++,
),
),
Align(
alignment: Alignment.bottomCenter,
child: bar(
cupertino: cupertino,
count: 3,
selected: 1,
onTap: tapped.add,
),
),
],
),
);
final box = pill(tester);
expect(box.width, lessThan(390 / 2));
await tester.tapAt(Offset(box.left - 40, box.center.dy));
await tester.tapAt(box.centerLeft + const Offset(12, 0));
await tester.tapAt(box.centerRight - const Offset(12, 0));
await tester.pump();
expect(content, 1);
expect(tapped, [0, 2]);
});
testWidgets('$host: 5 tabs at 320 with 2x text do not overflow', (
tester,
) async {
await pump(
tester,
cupertino: cupertino,
size: const Size(320, 640),
textScale: 2,
child: Align(
alignment: Alignment.bottomCenter,
child: bar(cupertino: cupertino, count: 5, selected: 4),
),
);
expect(tester.takeException(), isNull);
expect(pill(tester).right, 320 - 16);
expect(pill(tester).left, greaterThanOrEqualTo(16));
});
}
}
@@ -0,0 +1,254 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/ui/adaptive/floating_destination_bar.dart';
import 'package:pipelines_app/ui/adaptive/glass_pill.dart';
/// The floating tab bar is a row of separate round glass buttons at the
/// bottom right on every width: each tab its own circle of the named size,
/// evenly spaced, the row keeping the margins from the edges, on both hosts.
void main() {
const icons = [
Icons.account_tree_outlined,
Icons.play_circle_outline,
Icons.chat_bubble_outline,
Icons.search,
Icons.settings_outlined,
];
Key tabKey(int index) => ValueKey('tab-$index');
Widget bar({
required bool cupertino,
required int count,
required int selected,
ValueChanged<int>? onTap,
}) => FloatingDestinationBar(
cupertino: cupertino,
currentIndex: selected,
onTap: onTap ?? (_) {},
items: [
for (var index = 0; index < count; index += 1)
FloatingDestinationItem(
key: tabKey(index),
icon: Icon(icons[index]),
label: 'Tab $index',
),
],
);
Future<void> pump(
WidgetTester tester, {
required bool cupertino,
required Size size,
required Widget child,
double textScale = 1,
}) async {
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final body = MediaQuery(
data: MediaQueryData(
size: size,
textScaler: TextScaler.linear(textScale),
),
child: child,
);
await tester.pumpWidget(
cupertino
? CupertinoApp(home: CupertinoPageScaffold(child: body))
: MaterialApp(home: Scaffold(body: body)),
);
await tester.pumpAndSettle();
}
/// Tab [index]'s glass circle.
Rect button(WidgetTester tester, int index) => tester.getRect(
find.descendant(
of: find.byKey(tabKey(index)),
matching: find.byType(GlassPill),
),
);
Rect row(WidgetTester tester) =>
tester.getRect(find.byKey(FloatingDestinationBar.rowKey));
final placements = [
(name: 'wide 1200, 4 tabs', size: const Size(1200, 800), count: 4),
(name: 'phone 390, 3 tabs', size: const Size(390, 844), count: 3),
(name: 'phone 390, 4 tabs', size: const Size(390, 844), count: 4),
(name: 'phone 560, 5 tabs', size: const Size(560, 844), count: 5),
];
for (final cupertino in [false, true]) {
final host = cupertino ? 'Cupertino' : 'Material';
final diameter = FloatingDestinationBar.buttonSize(cupertino: cupertino);
final gap = FloatingDestinationBar.buttonGap(cupertino: cupertino);
final iconSize = cupertino
? FloatingDestinationBar.cupertinoIconSize
: FloatingDestinationBar.materialIconSize;
test('$host: buttons are larger than the old 44/42 circle, hit targets '
'meet the platform minimum', () {
expect(diameter, cupertino ? 50 : 52);
expect(diameter, greaterThanOrEqualTo(cupertino ? 44 : 48));
expect(gap, inInclusiveRange(10, 12));
});
testWidgets('$host: each tab is its own glass circle with the icon '
'centred in it', (tester) async {
await pump(
tester,
cupertino: cupertino,
size: const Size(390, 844),
child: Align(
alignment: Alignment.bottomCenter,
child: bar(cupertino: cupertino, count: 3, selected: 0),
),
);
expect(
find.descendant(
of: find.byType(FloatingDestinationBar),
matching: find.byType(GlassPill),
),
findsNWidgets(3),
);
for (var index = 0; index < 3; index += 1) {
final circle = button(tester, index);
expect(circle.size, Size(diameter, diameter));
expect(
tester
.widget<GlassPill>(
find.descendant(
of: find.byKey(tabKey(index)),
matching: find.byType(GlassPill),
),
)
.shape,
isA<CircleBorder>(),
);
final icon = tester.getRect(find.byIcon(icons[index]));
expect(icon.size, Size(iconSize, iconSize));
expect(icon.center, circle.center);
}
});
for (final placement in placements) {
testWidgets('$host ${placement.name}: a row at the bottom right, '
'evenly spaced', (tester) async {
final count = placement.count;
final size = placement.size;
await pump(
tester,
cupertino: cupertino,
size: size,
child: Align(
alignment: Alignment.bottomCenter,
child: bar(cupertino: cupertino, count: count, selected: 0),
),
);
final box = row(tester);
expect(box.right, size.width - 16);
expect(box.bottom, size.height - 16);
expect(box.height, diameter);
expect(
box.width,
FloatingDestinationBar.rowWidth(cupertino: cupertino, tabs: count),
);
expect(button(tester, 0).left, box.left);
expect(button(tester, count - 1).right, box.right);
for (var index = 1; index < count; index += 1) {
expect(
button(tester, index).left - button(tester, index - 1).right,
moreOrLessEquals(gap),
);
expect(button(tester, index).top, box.top);
}
});
}
testWidgets('$host: taps between and beside the buttons reach the '
'content, taps on them the tabs', (tester) async {
final tapped = <int>[];
var content = 0;
await pump(
tester,
cupertino: cupertino,
size: const Size(390, 844),
child: Stack(
children: [
Positioned.fill(
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () => content++,
),
),
Align(
alignment: Alignment.bottomCenter,
child: bar(
cupertino: cupertino,
count: 3,
selected: 1,
onTap: tapped.add,
),
),
],
),
);
final box = row(tester);
expect(box.width, lessThan(390 / 2));
await tester.tapAt(Offset(box.left - 40, box.center.dy));
final between = (button(tester, 0).right + button(tester, 1).left) / 2;
await tester.tapAt(Offset(between, box.center.dy));
await tester.tapAt(button(tester, 0).center);
await tester.tapAt(button(tester, 2).center);
await tester.pumpAndSettle();
expect(content, 2);
expect(tapped, [0, 2]);
});
testWidgets('$host: 5 tabs at 320 with 2x text narrow their gaps and '
'stay within the margins', (tester) async {
await pump(
tester,
cupertino: cupertino,
size: const Size(320, 640),
textScale: 2,
child: Align(
alignment: Alignment.bottomCenter,
child: bar(cupertino: cupertino, count: 5, selected: 4),
),
);
expect(tester.takeException(), isNull);
final box = row(tester);
expect(box.right, 320 - 16);
expect(box.left, greaterThanOrEqualTo(16));
final narrowed = (288 - 5 * diameter) / 4;
for (var index = 1; index < 5; index += 1) {
expect(button(tester, index).width, diameter);
expect(
button(tester, index).left - button(tester, index - 1).right,
moreOrLessEquals(narrowed < gap ? narrowed : gap),
);
}
});
testWidgets('$host: right to left, the row sits at the bottom left and '
'reads from the right', (tester) async {
await pump(
tester,
cupertino: cupertino,
size: const Size(390, 844),
child: Directionality(
textDirection: TextDirection.rtl,
child: Align(
alignment: Alignment.bottomCenter,
child: bar(cupertino: cupertino, count: 3, selected: 0),
),
),
);
expect(row(tester).left, 16);
expect(button(tester, 0).right, row(tester).right);
});
}
}
@@ -49,7 +49,7 @@ void main() {
),
),
);
final pill = tester.getRect(find.byType(GlassPill));
final pill = tester.getRect(find.byKey(FloatingDestinationBar.rowKey));
// The list fills the area, behind the bar.
expect(tester.getRect(find.byType(ListView)).bottom, 800);
await tester.drag(find.byType(ListView), const Offset(0, -5000));
@@ -67,11 +67,11 @@ void main() {
await tester.pumpWidget(
page(list: (_) => const SizedBox.expand(), onTap: () => taps++),
);
final pill = tester.getRect(find.byType(GlassPill));
final pill = tester.getRect(find.byKey(FloatingDestinationBar.rowKey));
expect(pill.right, 1200 - kFloatingBarMargin);
await tester.tapAt(Offset(pill.left - 24, pill.center.dy));
expect(taps, 1);
await tester.tapAt(pill.center);
await tester.tapAt(tester.getCenter(find.byType(GlassPill).first));
expect(taps, 1, reason: 'the bar takes its own taps');
});
}
+162 -137
View File
@@ -4,10 +4,13 @@ import 'package:flutter_test/flutter_test.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';
import 'package:pipelines_app/ui/adaptive/floating_tab_button.dart';
import 'package:pipelines_app/ui/adaptive/glass_pill.dart';
/// The floating tab bar's motion: the tabs pop in only when the bar appears
/// or its set of tabs changes, and one selection highlight glides (and
/// stretches on the way) to a newly selected tab, whose icon bounces.
/// The floating tab bar's motion: each button pops in on its own, staggered,
/// only when the bar appears or its set of tabs changes; the selection tint
/// cross-fades to a newly selected button, whose icon bounces; a press sinks
/// the button and it springs back.
void main() {
const icons = [
Icons.account_tree_outlined,
@@ -30,15 +33,16 @@ void main() {
void animate(WidgetTester tester, {required bool on}) {
tester.platformDispatcher.accessibilityFeaturesTestValue =
FakeAccessibilityFeatures(disableAnimations: !on);
addTearDown(() {
tester.platformDispatcher.accessibilityFeaturesTestValue =
const FakeAccessibilityFeatures(disableAnimations: true);
});
addTearDown(tester.platformDispatcher.clearAccessibilityFeaturesTestValue);
tester.view.physicalSize = const Size(560, 900);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
}
Widget app(bool cupertino, Widget body) => cupertino
? CupertinoApp(home: CupertinoPageScaffold(child: body))
: MaterialApp(home: Scaffold(body: body));
/// A bar of [count] tabs whose tap selects.
Future<void> pumpBar(
WidgetTester tester, {
@@ -46,74 +50,92 @@ void main() {
int count = 3,
}) async {
var selected = 0;
final body = StatefulBuilder(
builder: (context, setState) {
return Align(
alignment: Alignment.bottomCenter,
child: FloatingDestinationBar(
cupertino: cupertino,
currentIndex: selected,
onTap: (index) => setState(() => selected = index),
items: items(count),
),
);
},
);
await tester.pumpWidget(
cupertino
? CupertinoApp(home: CupertinoPageScaffold(child: body))
: MaterialApp(home: Scaffold(body: body)),
app(
cupertino,
StatefulBuilder(
builder: (context, setState) => Align(
alignment: Alignment.bottomCenter,
child: FloatingDestinationBar(
cupertino: cupertino,
currentIndex: selected,
onTap: (index) => setState(() => selected = index),
items: items(count),
),
),
),
),
);
}
/// The opacity of [tab]'s entrance fade.
double opacityOf(WidgetTester tester, Finder tab) => tester
.widget<FadeTransition>(
find
.descendant(
of: tab,
matching: find.byWidgetPredicate(
(widget) =>
widget is FadeTransition && widget.child is AnimatedBuilder,
),
)
.last,
)
.opacity
.value;
Finder glass(Finder tab) =>
find.descendant(of: tab, matching: find.byType(GlassPill));
double iconWidth(WidgetTester tester, Finder tab, IconData icon) => tester
.getRect(find.descendant(of: tab, matching: find.byIcon(icon)))
/// The opacity of [tab]'s entrance fade (its glass reveal).
double opacityOf(WidgetTester tester, Finder tab) =>
GlassReveal.maybeOf(tester.element(glass(tab)))?.value ?? 1;
/// [tab]'s drawn width (its entrance and press scale it).
double widthOf(WidgetTester tester, Finder tab) =>
tester.getRect(glass(tab)).width;
double iconWidth(WidgetTester tester, int index) => tester
.getRect(
find.descendant(
of: find.byKey(tabKey(index)),
matching: find.byIcon(icons[index]),
),
)
.width;
Rect highlight(WidgetTester tester) => tester.getRect(
find.descendant(
of: find.byKey(FloatingDestinationBar.highlightKey),
matching: find.byType(DecoratedBox),
),
);
Offset iconCentre(WidgetTester tester, int index) => tester.getCenter(
find.descendant(
of: find.byKey(tabKey(index)),
matching: find.byIcon(icons[index]),
),
);
/// The alpha of tab [index]'s selection fill.
double fillAlpha(WidgetTester tester, int index) {
final fill = tester.widget(
find.descendant(
of: find.byKey(tabKey(index)),
matching: find.byKey(FloatingTabButton.fillKey),
),
);
final decoration = fill is Ink
? fill.decoration
: (fill as DecoratedBox).decoration;
return (decoration! as ShapeDecoration).color!.a;
}
for (final cupertino in [false, true]) {
final host = cupertino ? 'Cupertino' : 'Material';
final size = FloatingDestinationBar.buttonSize(cupertino: cupertino);
final iconSize = cupertino
? FloatingDestinationBar.cupertinoIconSize
: FloatingDestinationBar.materialIconSize;
final size = cupertino
? FloatingDestinationBar.cupertinoHighlightSize
: FloatingDestinationBar.materialHighlightSize;
testWidgets('$host: the entrance does not replay when the selection '
'changes, and plays again for a new set of tabs', (tester) async {
testWidgets('$host: each button pops in on its own, staggered, from '
'0.6 to full size', (tester) async {
animate(tester, on: true);
await pumpBar(tester, cupertino: cupertino);
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));
}
await tester.pump(const Duration(milliseconds: 130));
final first = widthOf(tester, find.byKey(tabKey(0)));
final last = widthOf(tester, find.byKey(tabKey(2)));
expect(first, greaterThan(last + 4), reason: 'staggered');
expect(opacityOf(tester, find.byKey(tabKey(0))), greaterThan(0.9));
expect(opacityOf(tester, find.byKey(tabKey(2))), lessThan(0.2));
await tester.pumpAndSettle();
for (var index = 0; index < 3; index += 1) {
final tab = find.byKey(tabKey(index));
expect(opacityOf(tester, tab), 1);
expect(widthOf(tester, tab), size);
}
});
testWidgets('$host: the entrance does not replay when the selection '
'changes', (tester) async {
animate(tester, on: true);
await pumpBar(tester, cupertino: cupertino);
expect(opacityOf(tester, find.byKey(tabKey(0))), 0);
await tester.pumpAndSettle();
for (final target in [1, 2, 0]) {
await tester.tap(find.byKey(tabKey(target)));
@@ -122,14 +144,18 @@ 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 bounce may grow an icon; a replayed pop shrinks it.
// Only the press may shrink the tapped button, and only a
// little; a replayed pop starts at 0.6.
expect(
iconWidth(tester, tab, icons[index]),
greaterThanOrEqualTo(iconSize - 0.01),
widthOf(tester, tab),
index == target
? greaterThan(size * 0.9)
: moreOrLessEquals(size),
reason: 'tab $index popped again',
);
}
}
await tester.pumpAndSettle();
}
});
@@ -137,24 +163,24 @@ void main() {
animate(tester, on: true);
var count = 3;
late StateSetter update;
Widget build() => StatefulBuilder(
builder: (context, setState) {
update = setState;
return Align(
alignment: Alignment.bottomCenter,
child: FloatingDestinationBar(
cupertino: cupertino,
currentIndex: 0,
onTap: (_) {},
items: items(count),
),
);
},
);
await tester.pumpWidget(
cupertino
? CupertinoApp(home: CupertinoPageScaffold(child: build()))
: MaterialApp(home: Scaffold(body: build())),
app(
cupertino,
StatefulBuilder(
builder: (context, setState) {
update = setState;
return Align(
alignment: Alignment.bottomCenter,
child: FloatingDestinationBar(
cupertino: cupertino,
currentIndex: 0,
onTap: (_) {},
items: items(count),
),
);
},
),
),
);
await tester.pumpAndSettle();
expect(opacityOf(tester, find.byKey(tabKey(2))), 1);
@@ -164,84 +190,80 @@ void main() {
expect(opacityOf(tester, find.byKey(tabKey(2))), 0);
await tester.pumpAndSettle();
expect(opacityOf(tester, find.byKey(tabKey(3))), 1);
expect(widthOf(tester, find.byKey(tabKey(3))), size);
});
testWidgets('$host: the highlight rests on the selected tab as the old '
'circle did, glides and stretches between tabs, and the new icon '
'bounces', (tester) async {
testWidgets('$host: the selection tint cross-fades to the new button, '
'whose icon takes the selected colour and bounces', (tester) async {
animate(tester, on: true);
await pumpBar(tester, cupertino: cupertino);
await tester.pumpAndSettle();
expect(
highlight(tester),
rectMoreOrLessEquals(
Rect.fromCenter(
center: iconCentre(tester, 0),
width: size,
height: size,
),
epsilon: 0.01,
),
);
expect(find.byKey(FloatingDestinationBar.highlightKey), findsOneWidget);
final tint = fillAlpha(tester, 0);
expect(tint, greaterThan(0.1));
expect(fillAlpha(tester, 1), 0);
expect(fillAlpha(tester, 2), 0);
await tester.tap(find.byKey(tabKey(2)));
await tester.pump();
await tester.pump(const Duration(milliseconds: 60));
final from = iconCentre(tester, 0).dx;
final to = iconCentre(tester, 2).dx;
final moving = highlight(tester);
expect(moving.center.dx, greaterThan(from + 1));
expect(moving.center.dx, lessThan(to - 1));
expect(moving.height, moreOrLessEquals(size));
expect(moving.width, greaterThan(size * 1.1));
expect(moving.width, lessThanOrEqualTo(size * 1.25 + 0.01));
await tester.pump(const Duration(milliseconds: 80));
expect(fillAlpha(tester, 2), inExclusiveRange(0, tint));
expect(fillAlpha(tester, 0), inExclusiveRange(0, tint));
// The icon grows within its button (which the tap's press sinks).
expect(
iconWidth(tester, find.byKey(tabKey(2)), icons[2]),
greaterThan(iconSize * 1.02),
iconWidth(tester, 2) * size / widthOf(tester, find.byKey(tabKey(2))),
greaterThan(iconSize * 1.04),
);
expect(iconWidth(tester, 1), moreOrLessEquals(iconSize));
await tester.pumpAndSettle();
expect(fillAlpha(tester, 2), tint);
expect(fillAlpha(tester, 0), 0);
expect(iconWidth(tester, 2), moreOrLessEquals(iconSize));
final context = tester.element(find.byKey(tabKey(2)));
final selectedColour = cupertino
? CupertinoTheme.of(context).primaryColor
: Theme.of(context).colorScheme.onSecondaryContainer;
expect(
highlight(tester),
rectMoreOrLessEquals(
Rect.fromCenter(
center: iconCentre(tester, 2),
width: size,
height: size,
),
epsilon: 0.01,
),
IconTheme.of(tester.element(find.byIcon(icons[2]))).color,
isSameColorAs(selectedColour),
);
expect(
iconWidth(tester, find.byKey(tabKey(2)), icons[2]),
moreOrLessEquals(iconSize),
IconTheme.of(tester.element(find.byIcon(icons[0]))).color,
isNot(isSameColorAs(selectedColour)),
);
});
testWidgets('$host: under reduced motion the highlight jumps and the '
'icons neither pop nor bounce', (tester) async {
testWidgets('$host: a press sinks the button and it springs back', (
tester,
) async {
animate(tester, on: true);
await pumpBar(tester, cupertino: cupertino);
await tester.pumpAndSettle();
final tab = find.byKey(tabKey(1));
final gesture = await tester.startGesture(tester.getCenter(tab));
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);
await gesture.up();
await tester.pumpAndSettle();
expect(widthOf(tester, tab), size);
});
testWidgets('$host: under reduced motion the tint jumps and the buttons '
'neither pop nor bounce', (tester) async {
animate(tester, on: false);
await pumpBar(tester, cupertino: cupertino);
await tester.pump();
expect(opacityOf(tester, find.byKey(tabKey(1))), 1);
expect(widthOf(tester, find.byKey(tabKey(1))), size);
final tint = fillAlpha(tester, 0);
await tester.tap(find.byKey(tabKey(2)));
await tester.pump();
expect(
highlight(tester),
rectMoreOrLessEquals(
Rect.fromCenter(
center: iconCentre(tester, 2),
width: size,
height: size,
),
epsilon: 0.01,
),
);
expect(
iconWidth(tester, find.byKey(tabKey(2)), icons[2]),
moreOrLessEquals(iconSize),
);
await tester.pump();
expect(fillAlpha(tester, 2), tint);
expect(fillAlpha(tester, 0), 0);
expect(iconWidth(tester, 2), moreOrLessEquals(iconSize));
});
testWidgets('$host: switching the shell tabs never replays the main '
@@ -270,14 +292,17 @@ void main() {
for (var frame = 0; frame < 12; frame += 1) {
await tester.pump(const Duration(milliseconds: 25));
for (final tab in shellTabDestinations) {
final finder = find.byKey(
ValueKey('shell.destination.${tab.name}'),
);
expect(
opacityOf(
tester,
find.byKey(ValueKey('shell.destination.${tab.name}')),
),
opacityOf(tester, finder),
1,
reason: '${tab.name} faded switching to ${next.name}',
);
if (tab != next) {
expect(widthOf(tester, finder), size, reason: tab.name);
}
}
}
await tester.pumpAndSettle();
@@ -1,3 +1,5 @@
import 'dart:ui' show SemanticsRole;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
@@ -63,32 +65,58 @@ void main() {
await tester.pumpAndSettle();
final floating = find.byType(FloatingDestinationBar);
if (cupertino) {
// No label is drawn at all.
expect(
find.descendant(of: floating, matching: find.byType(Text)),
findsNothing,
);
} else {
// NavigationBar keeps its label hidden (for semantics only).
expect(
NavigationBarTheme.of(
tester.element(find.byType(NavigationDestination).first),
).labelBehavior,
NavigationDestinationLabelBehavior.alwaysHide,
);
}
// No label is drawn at all.
expect(
find.descendant(of: floating, matching: find.byType(Text)),
findsNothing,
);
for (final (index, (key, tabLabel)) in tabs.indexed) {
final node = tester.getSemantics(find.byKey(ValueKey(key)));
final finder = find.byKey(ValueKey(key));
final node = tester.getSemantics(finder);
expect(node.label, startsWith(tabLabel), reason: key);
expect(
node,
index == 0
? isSemantics(isSelected: true)
: isNot(isSemantics(isSelected: true)),
isSemantics(
isSelected: index == 0,
hasSelectedState: true,
isButton: true,
hasTapAction: true,
),
reason: key,
);
expect(node.role, SemanticsRole.tab, reason: key);
// "Tab n of m", as the host navigation controls announce it: in the
// Material label, the Cupertino hint.
final position = 'Tab ${index + 1} of ${tabs.length}';
expect(
cupertino ? node.hint : node.label,
contains(position),
reason: key,
);
// Material names the tab in its tooltip too.
if (!cupertino) {
expect(
find.descendant(
of: finder,
matching: find.byWidgetPredicate(
(widget) => widget is Tooltip && widget.message == tabLabel,
),
),
findsOneWidget,
reason: key,
);
}
// A hit target at least the platform minimum.
expect(
tester.getSize(finder).shortestSide,
greaterThanOrEqualTo(cupertino ? 44 : 48),
);
}
// The tabs make up one tab bar.
final tabBar = tester.getSemantics(
find.byKey(FloatingDestinationBar.rowKey),
);
expect(tabBar.role, SemanticsRole.tabBar);
// Distinct glyphs within the bar.
final icons = tester
.widgetList<Icon>(
@@ -184,10 +184,10 @@ void main() {
),
),
);
final pill = tester.getRect(find.byType(GlassPill));
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.materialHeight);
expect(pill.height, FloatingDestinationBar.materialButtonSize);
// Wide: sized to its tabs at the leading edge, under the title.
expect(pill.left, FloatingDestinationBar.topMargin);
expect(pill.width, lessThan(400));