6fcdbcaec2
The selected icon grows to 1.15x with the selection tint (220 ms, same curve) and shrinks back when another tab is selected, on every floating tab bar; reduced motion jumps straight to the size.
261 lines
8.3 KiB
Dart
261 lines
8.3 KiB
Dart
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';
|
|
|
|
/// 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]));
|
|
// Tab 0 is selected: its icon is a little larger than the others.
|
|
final side = index == 0
|
|
? iconSize * Motion.tabSelectedIconScale
|
|
: iconSize;
|
|
expect(icon.width, moreOrLessEquals(side));
|
|
expect(icon.height, moreOrLessEquals(side));
|
|
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);
|
|
});
|
|
}
|
|
}
|