Fade-through tab transitions, kept state and tests (finishes run #228)
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.
This commit is contained in:
@@ -94,55 +94,45 @@ class _FadeThroughStackState extends State<FadeThroughStack>
|
|||||||
Widget _page(int index, bool reduced) {
|
Widget _page(int index, bool reduced) {
|
||||||
final outgoing = index == _outgoingIndex;
|
final outgoing = index == _outgoingIndex;
|
||||||
final active = index == _visibleIndex;
|
final active = index == _visibleIndex;
|
||||||
final shown = reduced || active || outgoing;
|
final shown = active || outgoing;
|
||||||
final progress = _controller.value;
|
final progress = _controller.value;
|
||||||
final opacity = outgoing
|
const split = Motion.tabFadeThroughIncomingAt;
|
||||||
? 1 -
|
// Curves evaluated directly: a CurvedAnimation built per frame would
|
||||||
CurvedAnimation(
|
// register a listener on the controller every frame.
|
||||||
parent: _controller,
|
final out = const Interval(
|
||||||
curve: const Interval(
|
0,
|
||||||
0,
|
split,
|
||||||
Motion.tabFadeThroughIncomingAt,
|
curve: Motion.curve,
|
||||||
curve: Motion.curve,
|
).transform(progress);
|
||||||
),
|
final into = const Interval(
|
||||||
).value
|
split,
|
||||||
: active
|
1,
|
||||||
? CurvedAnimation(
|
curve: Motion.curve,
|
||||||
parent: _controller,
|
).transform(progress);
|
||||||
curve: const Interval(
|
const shrink = 1 - Motion.tabFadeThroughScale;
|
||||||
Motion.tabFadeThroughIncomingAt,
|
final opacity = outgoing ? 1 - out : (active ? into : 0.0);
|
||||||
1,
|
|
||||||
curve: Motion.curve,
|
|
||||||
),
|
|
||||||
).value
|
|
||||||
: 0.0;
|
|
||||||
final scale = outgoing
|
final scale = outgoing
|
||||||
? 1 -
|
? 1 - shrink * out
|
||||||
(Motion.tabFadeThroughScale - 1).abs() *
|
: (active ? Motion.tabFadeThroughScale + shrink * into : 1.0);
|
||||||
(progress / Motion.tabFadeThroughIncomingAt).clamp(0.0, 1.0)
|
// The same widget structure for every page whatever its state, so a
|
||||||
: active
|
// page's element (and its scroll position, controllers and text) is kept
|
||||||
? Motion.tabFadeThroughScale +
|
// across switches.
|
||||||
(1 - Motion.tabFadeThroughScale) *
|
|
||||||
((progress - Motion.tabFadeThroughIncomingAt) /
|
|
||||||
(1 - Motion.tabFadeThroughIncomingAt))
|
|
||||||
.clamp(0.0, 1.0)
|
|
||||||
: 1.0;
|
|
||||||
final transformed = Transform.scale(
|
|
||||||
scale: scale,
|
|
||||||
child: Opacity(opacity: opacity, child: widget.children[index]),
|
|
||||||
);
|
|
||||||
final page = active
|
|
||||||
? Semantics(container: true, explicitChildNodes: true, child: transformed)
|
|
||||||
: ExcludeFocus(
|
|
||||||
child: ExcludeSemantics(child: transformed),
|
|
||||||
);
|
|
||||||
return Offstage(
|
return Offstage(
|
||||||
offstage: !shown,
|
offstage: !shown,
|
||||||
child: TickerMode(
|
child: TickerMode(
|
||||||
enabled: active,
|
enabled: active,
|
||||||
child: IgnorePointer(
|
child: IgnorePointer(
|
||||||
ignoring: !active,
|
ignoring: !active,
|
||||||
child: page,
|
child: ExcludeFocus(
|
||||||
|
excluding: !active,
|
||||||
|
child: ExcludeSemantics(
|
||||||
|
excluding: !active,
|
||||||
|
child: Transform.scale(
|
||||||
|
scale: scale,
|
||||||
|
child: Opacity(opacity: opacity, child: widget.children[index]),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -58,7 +58,9 @@ void main() {
|
|||||||
await tester.pump();
|
await tester.pump();
|
||||||
expect(provider.value.uri.path, '/pipelines');
|
expect(provider.value.uri.path, '/pipelines');
|
||||||
await waitForRequest(tester, channel, 'list_pipelines');
|
await waitForRequest(tester, channel, 'list_pipelines');
|
||||||
expect(find.text('Nightly build'), findsNothing);
|
// The Runs page may still be fading out, but it is no longer the
|
||||||
|
// active page: nothing on it takes a tap.
|
||||||
|
expect(find.text('Nightly build').hitTestable(), findsNothing);
|
||||||
|
|
||||||
channel.incoming.add(
|
channel.incoming.add(
|
||||||
jsonEncode({'type': 'run_event', 'run_id': 1, 'status': 'waiting'}),
|
jsonEncode({'type': 'run_event', 'run_id': 1, 'status': 'waiting'}),
|
||||||
|
|||||||
@@ -0,0 +1,132 @@
|
|||||||
|
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);
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user