1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/material.dart';
class TestTransition extends AnimatedWidget {
const TestTransition({
Key? key,
required this.childFirstHalf,
required this.childSecondHalf,
required Animation<double> animation,
}) : super(key: key, listenable: animation);
final Widget childFirstHalf;
final Widget childSecondHalf;
@override
Widget build(BuildContext context) {
final Animation<double> animation = listenable as Animation<double>;
if (animation.value >= 0.5)
return childSecondHalf;
return childFirstHalf;
}
}
class TestRoute<T> extends PageRoute<T> {
TestRoute({
required this.child,
required RouteSettings settings,
this.barrierColor,
}) : super(settings: settings);
final Widget child;
@override
Duration get transitionDuration => const Duration(milliseconds: 150);
@override
final Color? barrierColor;
@override
String? get barrierLabel => null;
@override
bool get maintainState => false;
@override
Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
return child;
}
}
void main() {
const Duration kTwoTenthsOfTheTransitionDuration = Duration(milliseconds: 30);
const Duration kFourTenthsOfTheTransitionDuration = Duration(milliseconds: 60);
testWidgets('Check onstage/offstage handling around transitions', (WidgetTester tester) async {
final GlobalKey insideKey = GlobalKey();
String state({ bool skipOffstage = true }) {
String result = '';
if (tester.any(find.text('A', skipOffstage: skipOffstage)))
result += 'A';
if (tester.any(find.text('B', skipOffstage: skipOffstage)))
result += 'B';
if (tester.any(find.text('C', skipOffstage: skipOffstage)))
result += 'C';
if (tester.any(find.text('D', skipOffstage: skipOffstage)))
result += 'D';
if (tester.any(find.text('E', skipOffstage: skipOffstage)))
result += 'E';
if (tester.any(find.text('F', skipOffstage: skipOffstage)))
result += 'F';
if (tester.any(find.text('G', skipOffstage: skipOffstage)))
result += 'G';
return result;
}
await tester.pumpWidget(
MaterialApp(
onGenerateRoute: (RouteSettings settings) {
switch (settings.name) {
case '/':
return TestRoute<void>(
settings: settings,
child: Builder(
key: insideKey,
builder: (BuildContext context) {
final PageRoute<void> route = ModalRoute.of(context)! as PageRoute<void>;
return Column(
children: <Widget>[
TestTransition(
childFirstHalf: const Text('A'),
childSecondHalf: const Text('B'),
animation: route.animation!,
),
TestTransition(
childFirstHalf: const Text('C'),
childSecondHalf: const Text('D'),
animation: route.secondaryAnimation!,
),
],
);
},
),
);
case '/2': return TestRoute<void>(settings: settings, child: const Text('E'));
case '/3': return TestRoute<void>(settings: settings, child: const Text('F'));
case '/4': return TestRoute<void>(settings: settings, child: const Text('G'));
}
return null;
}
)
);
final NavigatorState navigator = insideKey.currentContext!.findAncestorStateOfType<NavigatorState>()!;
expect(state(), equals('BC')); // transition ->1 is at 1.0
navigator.pushNamed('/2');
expect(state(), equals('BC')); // transition 1->2 is not yet built
await tester.pump();
expect(state(), equals('BC')); // transition 1->2 is at 0.0
expect(state(skipOffstage: false), equals('BCE')); // E is offstage
await tester.pump(kFourTenthsOfTheTransitionDuration);
expect(state(), equals('BCE')); // transition 1->2 is at 0.4
await tester.pump(kFourTenthsOfTheTransitionDuration);
expect(state(), equals('BDE')); // transition 1->2 is at 0.8
await tester.pump(kFourTenthsOfTheTransitionDuration);
expect(state(), equals('E')); // transition 1->2 is at 1.0
expect(state(skipOffstage: false), equals('E')); // B and C are gone, the route is inactive with maintainState=false
navigator.pop();
expect(state(), equals('E')); // transition 1<-2 is at 1.0, just reversed
await tester.pump();
await tester.pump();
expect(state(), equals('BDE')); // transition 1<-2 is at 1.0
await tester.pump(kFourTenthsOfTheTransitionDuration);
expect(state(), equals('BDE')); // transition 1<-2 is at 0.6
navigator.pushNamed('/3');
expect(state(), equals('BDE')); // transition 1<-2 is at 0.6
await tester.pump();
expect(state(), equals('BDE')); // transition 1<-2 is at 0.6, 1->3 is at 0.0
expect(state(skipOffstage: false), equals('BDEF')); // F is offstage since we're at 0.0
await tester.pump(kFourTenthsOfTheTransitionDuration);
expect(state(), equals('BCEF')); // transition 1<-2 is at 0.2, 1->3 is at 0.4
expect(state(skipOffstage: false), equals('BCEF')); // nothing secret going on here
await tester.pump(kFourTenthsOfTheTransitionDuration);
expect(state(), equals('BDF')); // transition 1<-2 is done, 1->3 is at 0.8
navigator.pop();
expect(state(), equals('BDF')); // transition 1<-3 is at 0.8, just reversed
await tester.pump();
expect(state(), equals('BDF')); // transition 1<-3 is at 0.8
await tester.pump(kTwoTenthsOfTheTransitionDuration); // notice that dT=0.2 here, not 0.4
expect(state(), equals('BDF')); // transition 1<-3 is at 0.6
await tester.pump(kFourTenthsOfTheTransitionDuration);
expect(state(), equals('BCF')); // transition 1<-3 is at 0.2
navigator.pushNamed('/4');
expect(state(), equals('BCF')); // transition 1<-3 is at 0.2, 1->4 is not yet built
await tester.pump();
expect(state(), equals('BCF')); // transition 1<-3 is at 0.2, 1->4 is at 0.0
expect(state(skipOffstage: false), equals('BCFG')); // G is offstage
await tester.pump(kFourTenthsOfTheTransitionDuration);
expect(state(), equals('BCG')); // transition 1<-3 is done, 1->4 is at 0.4
await tester.pump(kFourTenthsOfTheTransitionDuration);
expect(state(), equals('BDG')); // transition 1->4 is at 0.8
await tester.pump(kFourTenthsOfTheTransitionDuration);
expect(state(), equals('G')); // transition 1->4 is done
expect(state(skipOffstage: false), equals('G')); // route 1 is not around any more
});
testWidgets('Check onstage/offstage handling of barriers around transitions', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
onGenerateRoute: (RouteSettings settings) {
switch (settings.name) {
case '/': return TestRoute<void>(settings: settings, child: const Text('A'));
case '/1': return TestRoute<void>(settings: settings, barrierColor: const Color(0xFFFFFF00), child: const Text('B'));
}
return null;
}
)
);
expect(find.byType(ModalBarrier), findsOneWidget);
tester.state<NavigatorState>(find.byType(Navigator)).pushNamed('/1');
expect(find.byType(ModalBarrier), findsOneWidget);
await tester.pump();
expect(find.byType(ModalBarrier), findsNWidgets(2));
expect(tester.widget<ModalBarrier>(find.byType(ModalBarrier).first).color, isNull);
expect(tester.widget<ModalBarrier>(find.byType(ModalBarrier).last).color, isNull);
await tester.pump(const Duration(seconds: 1));
expect(find.byType(ModalBarrier), findsOneWidget);
expect(tester.widget<ModalBarrier>(find.byType(ModalBarrier)).color, const Color(0xFFFFFF00));
});
}