009bf3d853
Each island (Back, title, actions pill, each action) scales in from 0.85 with a gentle overshoot, staggered from the leading edge on first show and at once when it newly appears; rebuilds and title changes keep it. Glass fades its fill and blur together so backdrop grouping is unchanged. Every tappable piece sinks to 0.94 under a stadium tint while pressed and springs back. Reduced motion skips the animation.
269 lines
10 KiB
Dart
269 lines
10 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/rendering.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:pipelines_app/ui/adaptive/glass_pill.dart';
|
|
import 'package:pipelines_app/ui/adaptive/segmented_bar.dart';
|
|
import 'package:pipelines_app/ui/adaptive/segmented_bar_motion.dart';
|
|
import 'package:pipelines_app/ui/adaptive/segmented_bar_pieces.dart';
|
|
import 'package:pipelines_app/ui/motion/motion.dart';
|
|
|
|
/// The top bar's islands pop in once, and its controls sink when pressed.
|
|
void main() {
|
|
Widget page(
|
|
bool cupertino, {
|
|
String title = 'Runs',
|
|
List<Widget> actions = const [],
|
|
VoidCallback? onTitle,
|
|
bool reduced = false,
|
|
VoidCallback? onBack,
|
|
}) {
|
|
final bar = SegmentedBar(
|
|
cupertino: cupertino,
|
|
title: title,
|
|
onTitlePressed: onTitle,
|
|
leading: SegmentedBackButton(
|
|
key: const Key('back'),
|
|
cupertino: cupertino,
|
|
onPressed: onBack ?? () {},
|
|
),
|
|
actions: actions,
|
|
);
|
|
final Widget home = cupertino
|
|
? CupertinoPageScaffold(
|
|
navigationBar: bar,
|
|
child: const SizedBox.expand(),
|
|
)
|
|
: Scaffold(
|
|
extendBodyBehindAppBar: true,
|
|
appBar: bar,
|
|
body: const SizedBox.expand(),
|
|
);
|
|
final app = cupertino ? CupertinoApp(home: home) : MaterialApp(home: home);
|
|
return MediaQuery(
|
|
data: MediaQueryData(disableAnimations: reduced),
|
|
child: app,
|
|
);
|
|
}
|
|
|
|
/// The entrance scale of the island holding [of]: its pill's, or with
|
|
/// [inner] its own inside the actions pill.
|
|
double entranceScale(WidgetTester tester, Finder of, {bool inner = false}) {
|
|
final entrances = find.ancestor(
|
|
of: of,
|
|
matching: find.byType(SegmentedEntrance),
|
|
);
|
|
final entrance = inner ? entrances.first : entrances.last;
|
|
final transform = tester.widget<Transform>(
|
|
find.descendant(of: entrance, matching: find.byType(Transform)).first,
|
|
);
|
|
return transform.transform.storage[0];
|
|
}
|
|
|
|
/// The press scale of the control holding [of], or with [pill] of the
|
|
/// pill holding it.
|
|
double pressScale(WidgetTester tester, Finder of, {bool pill = false}) {
|
|
final presses = find.ancestor(
|
|
of: of,
|
|
matching: find.byType(SegmentedPress),
|
|
);
|
|
final press = pill ? presses.last : presses.first;
|
|
final transform = tester.widget<Transform>(
|
|
find.descendant(of: press, matching: find.byType(Transform)).first,
|
|
);
|
|
return transform.transform.storage[0];
|
|
}
|
|
|
|
Color pressTint(WidgetTester tester, Finder of) {
|
|
final press = find
|
|
.ancestor(of: of, matching: find.byType(SegmentedPress))
|
|
.first;
|
|
final box = tester.widget<DecoratedBox>(
|
|
find.descendant(of: press, matching: find.byType(DecoratedBox)).first,
|
|
);
|
|
return (box.decoration as ShapeDecoration).color!;
|
|
}
|
|
|
|
final back = find.byKey(const Key('back'));
|
|
final title = find.text('Runs');
|
|
|
|
for (final cupertino in [false, true]) {
|
|
final host = cupertino ? 'Cupertino' : 'Material';
|
|
final icon = cupertino ? CupertinoIcons.refresh : Icons.refresh;
|
|
Widget action(String key) =>
|
|
IconButton(key: Key(key), icon: Icon(icon), onPressed: () {});
|
|
|
|
testWidgets('$host: the islands pop in left to right on first show', (
|
|
tester,
|
|
) async {
|
|
await tester.pumpWidget(page(cupertino, actions: [action('a')]));
|
|
final a = find.byKey(const Key('a'));
|
|
expect(entranceScale(tester, back), Motion.barEntranceScaleBegin);
|
|
await tester.pump(const Duration(milliseconds: 120));
|
|
final b = entranceScale(tester, back);
|
|
final t = entranceScale(tester, title);
|
|
final r = entranceScale(tester, a);
|
|
expect(b, greaterThan(t), reason: 'Back leads the title');
|
|
expect(t, greaterThan(r), reason: 'the title leads the actions');
|
|
expect(r, greaterThan(Motion.barEntranceScaleBegin));
|
|
await tester.pumpAndSettle();
|
|
expect(entranceScale(tester, back), 1);
|
|
expect(entranceScale(tester, title), 1);
|
|
expect(entranceScale(tester, a), 1);
|
|
});
|
|
|
|
testWidgets('$host: a rebuild or new title does not replay it', (
|
|
tester,
|
|
) async {
|
|
await tester.pumpWidget(page(cupertino, actions: [action('a')]));
|
|
await tester.pumpAndSettle();
|
|
await tester.pumpWidget(
|
|
page(cupertino, title: 'Other', actions: [action('a')]),
|
|
);
|
|
await tester.pump();
|
|
expect(entranceScale(tester, back), 1);
|
|
expect(entranceScale(tester, find.text('Other')), 1);
|
|
expect(entranceScale(tester, find.byKey(const Key('a'))), 1);
|
|
expect(tester.hasRunningAnimations, isFalse);
|
|
});
|
|
|
|
testWidgets('$host: an action that becomes available pops in alone', (
|
|
tester,
|
|
) async {
|
|
await tester.pumpWidget(page(cupertino, actions: [action('a')]));
|
|
await tester.pumpAndSettle();
|
|
await tester.pumpWidget(
|
|
page(cupertino, actions: [action('b'), action('a')]),
|
|
);
|
|
final newcomer = find.byKey(const Key('b'));
|
|
expect(
|
|
entranceScale(tester, newcomer, inner: true),
|
|
Motion.barEntranceScaleBegin,
|
|
);
|
|
// The pill and the action already there stay put.
|
|
expect(entranceScale(tester, find.byKey(const Key('a'))), 1);
|
|
expect(entranceScale(tester, find.byKey(const Key('a')), inner: true), 1);
|
|
expect(entranceScale(tester, back), 1);
|
|
await tester.pump(Motion.barEntranceDuration ~/ 2);
|
|
expect(entranceScale(tester, newcomer, inner: true), greaterThan(0.9));
|
|
await tester.pumpAndSettle();
|
|
expect(entranceScale(tester, newcomer, inner: true), 1);
|
|
});
|
|
|
|
testWidgets('$host: reduced motion shows the bar at once', (tester) async {
|
|
await tester.pumpWidget(
|
|
page(cupertino, actions: [action('a')], reduced: true),
|
|
);
|
|
expect(entranceScale(tester, back), 1);
|
|
expect(entranceScale(tester, title), 1);
|
|
expect(tester.hasRunningAnimations, isFalse);
|
|
});
|
|
|
|
testWidgets('$host: the glass fades in with its blur, not under a layer', (
|
|
tester,
|
|
) async {
|
|
await tester.pumpWidget(page(cupertino));
|
|
await tester.pump(const Duration(milliseconds: 40));
|
|
final backdrops = tester.layers.whereType<BackdropFilterLayer>().toList();
|
|
expect(backdrops, hasLength(2), reason: 'Back and the title');
|
|
expect(backdrops.first.backdropKey, isNotNull, reason: 'still grouped');
|
|
expect(backdrops.last.backdropKey, same(backdrops.first.backdropKey));
|
|
for (final layer in backdrops) {
|
|
// No opacity layer above a blur: it would read nothing behind it.
|
|
for (ContainerLayer? up = layer.parent; up != null; up = up.parent) {
|
|
if (up is OpacityLayer) expect(up.alpha, anyOf(isNull, 255));
|
|
}
|
|
}
|
|
final mid = tester.widget<BackdropFilter>(
|
|
find.ancestor(of: back, matching: find.byType(BackdropFilter)).first,
|
|
);
|
|
await tester.pumpAndSettle();
|
|
final rest = tester.widget<BackdropFilter>(
|
|
find.ancestor(of: back, matching: find.byType(BackdropFilter)).first,
|
|
);
|
|
expect(mid.filter, isNot(rest.filter), reason: 'the blur fades in');
|
|
});
|
|
|
|
testWidgets(
|
|
'$host: Back sinks and tints while pressed, then springs back',
|
|
(tester) async {
|
|
var backs = 0;
|
|
await tester.pumpWidget(page(cupertino, onBack: () => backs++));
|
|
await tester.pumpAndSettle();
|
|
expect(pressScale(tester, back), 1);
|
|
expect(pressTint(tester, back).a, 0);
|
|
final gesture = await tester.startGesture(tester.getCenter(back));
|
|
await tester.pump();
|
|
await tester.pump(Motion.barPressDuration);
|
|
expect(pressScale(tester, back), closeTo(Motion.barPressScale, 1e-6));
|
|
expect(pressTint(tester, back).a, greaterThan(0.1));
|
|
await gesture.up();
|
|
await tester.pumpAndSettle();
|
|
expect(pressScale(tester, back), closeTo(1, 1e-3));
|
|
expect(pressTint(tester, back).a, 0);
|
|
expect(backs, 1, reason: 'the tap still reaches Back');
|
|
},
|
|
);
|
|
|
|
testWidgets('$host: a quick tap still shows the whole press', (
|
|
tester,
|
|
) async {
|
|
await tester.pumpWidget(page(cupertino, actions: [action('a')]));
|
|
await tester.pumpAndSettle();
|
|
final a = find.byKey(const Key('a'));
|
|
await tester.tap(a);
|
|
await tester.pump();
|
|
await tester.pump(const Duration(milliseconds: 80));
|
|
// A lone action is the whole pill: the pill sinks, not the icon.
|
|
expect(pressScale(tester, a, pill: true), lessThan(0.95));
|
|
expect(pressScale(tester, a), 1);
|
|
await tester.pumpAndSettle();
|
|
expect(pressScale(tester, a, pill: true), 1);
|
|
});
|
|
|
|
testWidgets('$host: beside others each action sinks on its own', (
|
|
tester,
|
|
) async {
|
|
var taps = 0;
|
|
await tester.pumpWidget(
|
|
page(
|
|
cupertino,
|
|
onTitle: () => taps++,
|
|
actions: [action('a'), action('b')],
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
final a = find.byKey(const Key('a'));
|
|
final b = find.byKey(const Key('b'));
|
|
final gesture = await tester.startGesture(tester.getCenter(b));
|
|
await tester.pump();
|
|
await tester.pump(Motion.barPressDuration);
|
|
expect(pressScale(tester, b), closeTo(Motion.barPressScale, 1e-6));
|
|
expect(pressScale(tester, a), 1);
|
|
expect(pressScale(tester, back), 1);
|
|
await gesture.up();
|
|
await tester.pumpAndSettle();
|
|
// A pressable title sinks as a whole pill and still takes the tap.
|
|
await tester.tap(title);
|
|
await tester.pumpAndSettle();
|
|
expect(taps, 1);
|
|
});
|
|
|
|
testWidgets('$host: hit testing is unchanged at rest', (tester) async {
|
|
await tester.pumpWidget(page(cupertino, actions: [action('a')]));
|
|
await tester.pumpAndSettle();
|
|
final pill = tester.getRect(
|
|
find.ancestor(of: back, matching: find.byType(GlassPill)).first,
|
|
);
|
|
expect(pill.left, 12);
|
|
expect(pill.height, segmentedPieceHeight(cupertino));
|
|
// A tap between the pieces reaches no piece; on Back it reaches Back.
|
|
final backBox = tester.renderObject(back);
|
|
bool hitsBack(Offset at) =>
|
|
tester.hitTestOnBinding(at).path.any((e) => e.target == backBox);
|
|
expect(hitsBack(Offset(pill.right + 4, pill.center.dy)), isFalse);
|
|
expect(hitsBack(pill.center), isTrue);
|
|
});
|
|
}
|
|
}
|