b56a2eb72d
Tabs fade through when switching main destinations and page tabs. Each page keeps one widget structure whatever its state, so its element (scroll, controllers, text) survives switches; curves are computed directly instead of building a CurvedAnimation per frame; only the active and outgoing pages are on stage, including under reduced motion. Adds FadeThroughStack tests.
133 lines
4.1 KiB
Dart
133 lines
4.1 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:pipelines_app/ui/motion/fade_through_stack.dart';
|
|
import 'package:pipelines_app/ui/motion/motion.dart';
|
|
|
|
class _Page extends StatefulWidget {
|
|
const _Page(this.name);
|
|
final String name;
|
|
@override
|
|
State<_Page> createState() => _PageState();
|
|
}
|
|
|
|
class _PageState extends State<_Page> {
|
|
final controller = TextEditingController();
|
|
@override
|
|
void dispose() {
|
|
controller.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) => Column(
|
|
children: [
|
|
Text('page ${widget.name}'),
|
|
TextField(key: Key('field ${widget.name}'), controller: controller),
|
|
ElevatedButton(
|
|
key: Key('button ${widget.name}'),
|
|
onPressed: () => tapped.add(widget.name),
|
|
child: Text('tap ${widget.name}'),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
final tapped = <String>[];
|
|
|
|
Widget _host(int index, {bool reduced = false}) => MaterialApp(
|
|
home: MediaQuery(
|
|
data: MediaQueryData(disableAnimations: reduced),
|
|
child: Scaffold(
|
|
body: FadeThroughStack(
|
|
index: index,
|
|
children: const [_Page('a'), _Page('b'), _Page('c')],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
|
|
double _opacityOf(WidgetTester tester, String name) => tester
|
|
.widget<Opacity>(
|
|
find.ancestor(
|
|
of: find.text('page $name'),
|
|
matching: find.byType(Opacity),
|
|
),
|
|
)
|
|
.opacity;
|
|
|
|
bool _onstage(WidgetTester tester, String name) => !tester
|
|
.widget<Offstage>(
|
|
find
|
|
.ancestor(
|
|
of: find.text('page $name', skipOffstage: false),
|
|
matching: find.byType(Offstage, skipOffstage: false),
|
|
)
|
|
.first,
|
|
)
|
|
.offstage;
|
|
|
|
void main() {
|
|
setUp(tapped.clear);
|
|
|
|
testWidgets('fades the old page out, then the new page in', (tester) async {
|
|
await tester.pumpWidget(_host(0));
|
|
await tester.pumpWidget(_host(1));
|
|
final total = Motion.tabFadeThrough;
|
|
await tester.pump(total * 0.2);
|
|
expect(_opacityOf(tester, 'a'), inExclusiveRange(0.0, 1.0));
|
|
expect(_opacityOf(tester, 'b'), 0.0);
|
|
await tester.pump(total * 0.5);
|
|
expect(_opacityOf(tester, 'a'), 0.0);
|
|
expect(_opacityOf(tester, 'b'), inExclusiveRange(0.0, 1.0));
|
|
await tester.pump(total);
|
|
expect(_opacityOf(tester, 'b'), 1.0);
|
|
expect(_onstage(tester, 'a'), isFalse);
|
|
});
|
|
|
|
testWidgets('a page keeps its state across switches', (tester) async {
|
|
await tester.pumpWidget(_host(0));
|
|
await tester.enterText(find.byKey(const Key('field a')), 'draft');
|
|
final before = tester.state(find.byType(_Page).first);
|
|
await tester.pumpWidget(_host(1));
|
|
await tester.pump(Motion.tabFadeThrough * 2);
|
|
await tester.pumpWidget(_host(0));
|
|
await tester.pump(Motion.tabFadeThrough * 2);
|
|
expect(tester.state(find.byType(_Page).first), same(before));
|
|
expect(find.text('draft'), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('only the active page takes taps', (tester) async {
|
|
await tester.pumpWidget(_host(0));
|
|
await tester.pumpWidget(_host(1));
|
|
await tester.pump(Motion.tabFadeThrough * 0.2);
|
|
// The outgoing page is still visible but must not take the tap; it
|
|
// reaches the active page's button underneath instead.
|
|
await tester.tap(find.byKey(const Key('button a')), warnIfMissed: false);
|
|
expect(tapped, isNot(contains('a')));
|
|
expect(tapped, ['b']);
|
|
});
|
|
|
|
testWidgets('rapid switching ends with only the last page shown', (
|
|
tester,
|
|
) async {
|
|
await tester.pumpWidget(_host(0));
|
|
await tester.pumpWidget(_host(1));
|
|
await tester.pump(const Duration(milliseconds: 30));
|
|
await tester.pumpWidget(_host(2));
|
|
await tester.pump(const Duration(milliseconds: 30));
|
|
await tester.pumpWidget(_host(0));
|
|
await tester.pump(Motion.tabFadeThrough * 2);
|
|
expect(_onstage(tester, 'a'), isTrue);
|
|
expect(_onstage(tester, 'b'), isFalse);
|
|
expect(_onstage(tester, 'c'), isFalse);
|
|
expect(_opacityOf(tester, 'a'), 1.0);
|
|
});
|
|
|
|
testWidgets('reduced motion switches instantly', (tester) async {
|
|
await tester.pumpWidget(_host(0, reduced: true));
|
|
await tester.pumpWidget(_host(1, reduced: true));
|
|
await tester.pump();
|
|
expect(_opacityOf(tester, 'b'), 1.0);
|
|
});
|
|
}
|