-
Notifications
You must be signed in to change notification settings - Fork 106
/
go_router_push_animation.dart
297 lines (258 loc) · 8.51 KB
/
go_router_push_animation.dart
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright 2013 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:animation_metadata/animation_metadata.dart';
import 'package:diagram_capture/diagram_capture.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'diagram_step.dart';
import 'utils.dart';
const Duration _pauseDuration = Duration(seconds: 1);
const Duration _openDuration = Duration(milliseconds: 300);
const Duration _closeDuration = Duration(milliseconds: 300);
final Duration _totalDuration = _pauseDuration +
_pauseDuration +
_openDuration +
_pauseDuration +
_closeDuration +
_pauseDuration;
final GlobalKey _pushShell1 = GlobalKey();
final GlobalKey _pushShell2 = GlobalKey();
final GlobalKey _pushRegularRoute = GlobalKey();
final GlobalKey<NavigatorState> _innerNavigator = GlobalKey<NavigatorState>();
final GlobalKey<NavigatorState> _outerNavigator = GlobalKey<NavigatorState>();
class PushRegularRouteDiagram extends StatefulWidget with DiagramMetadata {
const PushRegularRouteDiagram({super.key});
@override
String get name => 'push_regular_route';
@override
VideoFormat get videoFormat => VideoFormat.gif;
@override
State<PushRegularRouteDiagram> createState() =>
_PushRegularRouteDiagramState();
@override
Duration? get duration => _totalDuration;
}
class _PushRegularRouteDiagramState extends State<PushRegularRouteDiagram>
with TickerProviderStateMixin, LockstepStateMixin {
Future<void> _tap(GlobalKey key) async {
final RenderBox target =
key.currentContext!.findRenderObject()! as RenderBox;
final Offset targetOffset =
target.localToGlobal(target.size.center(Offset.zero));
final WidgetController controller = DiagramWidgetController.of(context);
final TestGesture gesture = await controller.startGesture(targetOffset);
await waitLockstep(_pauseDuration);
await gesture.up();
await waitLockstep(_openDuration);
}
Future<void> _pause() async {
await waitLockstep(_pauseDuration);
}
Future<void> startAnimation() async {
await _pause();
await _tap(_pushRegularRoute);
await _pause();
_outerNavigator.currentState!.pop();
await waitLockstep(_closeDuration);
await _pause();
}
@override
void initState() {
super.initState();
startAnimation();
}
@override
Widget build(BuildContext context) {
return _MainApp();
}
}
class PushSameShellDiagram extends StatefulWidget with DiagramMetadata {
const PushSameShellDiagram({super.key});
@override
String get name => 'push_same_shell';
@override
VideoFormat get videoFormat => VideoFormat.gif;
@override
State<PushSameShellDiagram> createState() => _PushSameShellDiagramState();
@override
Duration? get duration => _totalDuration;
}
class _PushSameShellDiagramState extends State<PushSameShellDiagram>
with TickerProviderStateMixin, LockstepStateMixin {
Future<void> _tap(GlobalKey key) async {
final RenderBox target =
key.currentContext!.findRenderObject()! as RenderBox;
final Offset targetOffset =
target.localToGlobal(target.size.center(Offset.zero));
final WidgetController controller = DiagramWidgetController.of(context);
final TestGesture gesture = await controller.startGesture(targetOffset);
await waitLockstep(_pauseDuration);
await gesture.up();
await waitLockstep(_openDuration);
}
Future<void> _pause() async {
await waitLockstep(_pauseDuration);
}
Future<void> startAnimation() async {
await _pause();
await _tap(_pushShell1);
await _pause();
_innerNavigator.currentState!.pop();
await waitLockstep(_closeDuration);
await _pause();
}
@override
void initState() {
super.initState();
startAnimation();
}
@override
Widget build(BuildContext context) {
return _MainApp();
}
}
class PushDifferentShellDiagram extends StatefulWidget with DiagramMetadata {
const PushDifferentShellDiagram({super.key});
@override
String get name => 'push_different_shell';
@override
VideoFormat get videoFormat => VideoFormat.gif;
@override
State<PushDifferentShellDiagram> createState() =>
_PushDifferentShellDiagramState();
@override
Duration? get duration => _totalDuration;
}
class _PushDifferentShellDiagramState extends State<PushDifferentShellDiagram>
with TickerProviderStateMixin, LockstepStateMixin {
Future<void> _tap(GlobalKey key) async {
final RenderBox target =
key.currentContext!.findRenderObject()! as RenderBox;
final Offset targetOffset =
target.localToGlobal(target.size.center(Offset.zero));
final WidgetController controller = DiagramWidgetController.of(context);
final TestGesture gesture = await controller.startGesture(targetOffset);
await waitLockstep(_pauseDuration);
await gesture.up();
await waitLockstep(_openDuration);
}
Future<void> _pause() async {
await waitLockstep(_pauseDuration);
}
Future<void> startAnimation() async {
await _pause();
await _tap(_pushShell2);
await _pause();
_outerNavigator.currentState!.pop();
await waitLockstep(_closeDuration);
await _pause();
}
@override
void initState() {
super.initState();
startAnimation();
}
@override
Widget build(BuildContext context) {
return _MainApp();
}
}
class _MainApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ConstrainedBox(
key: UniqueKey(),
constraints: BoxConstraints.tight(const Size(350, 622)),
child: Navigator(
key: _outerNavigator,
initialRoute: '/',
onGenerateRoute: (RouteSettings settings) {
return PageRouteBuilder<void>(
pageBuilder: (
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
) {
return Scaffold(
appBar: AppBar(
title: const Text('Shell1'),
),
body: _Shell1(),
);
},
);
},
),
);
}
}
class _Shell1 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Navigator(
key: _innerNavigator,
initialRoute: '/',
onGenerateRoute: (RouteSettings settings) {
return PageRouteBuilder<void>(
pageBuilder: (
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
) {
return Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
TextButton(
key: _pushShell1,
onPressed: () {
_innerNavigator.currentState!
.push(MaterialPageRoute<void>(builder: (_) {
return const Center(child: Text('shell1 body'));
}));
},
child: const Text('push the same shell route /shell1'),
),
TextButton(
key: _pushShell2,
onPressed: () {
_outerNavigator.currentState!
.push(MaterialPageRoute<void>(builder: (_) {
return Scaffold(
appBar: AppBar(
title: const Text('shell2'),
),
body: const Center(child: Text('shell2 body')));
}));
},
child: const Text('push the different shell route /shell2'),
),
TextButton(
key: _pushRegularRoute,
onPressed: () {
_outerNavigator.currentState!
.push(MaterialPageRoute<void>(builder: (_) {
return const Scaffold(
body: Center(child: Text('Regular Route')));
}));
},
child: const Text('push the regular route /regular-route'),
),
],
);
},
);
},
);
}
}
class GoRouterDiagramStep extends DiagramStep {
@override
final String category = 'go_router';
@override
Future<List<DiagramMetadata>> get diagrams async => <DiagramMetadata>[
const PushRegularRouteDiagram(),
const PushSameShellDiagram(),
const PushDifferentShellDiagram(),
];
}