Line data Source code
1 : part of 'main.dart';
2 :
3 : /// A page to put in [Navigator] pages
4 : ///
5 : /// This is a normal page except that it allows for
6 : /// custom transitions easily.
7 : abstract class VBasePage<T> extends Page<T> {
8 : /// The child of this page
9 : final Widget child;
10 :
11 : /// The name of this page
12 : @override
13 : final String? name;
14 :
15 : /// The key of this page
16 : @override
17 : final LocalKey key;
18 :
19 : /// The duration of the transition which happens when this page
20 : /// is put in the widget tree
21 : final Duration? transitionDuration;
22 :
23 : /// The duration of the transition which happens when this page
24 : /// is removed from the widget tree
25 : final Duration? reverseTransitionDuration;
26 :
27 : /// A function to build the transition to or from this route
28 : ///
29 : /// [child] is the child of the page
30 : ///
31 : /// Example of a fade transition:
32 : /// buildTransition: (animation, _, child) {
33 : /// return FadeTransition(opacity: animation, child: child);
34 : /// }
35 : ///
36 : /// If this is null, the default transition is the one of the [VRouter]
37 : /// If the one of the [VRouter] is also null, the default transition is
38 : /// the one of a [MaterialPage]
39 : final Widget Function(Animation<double> animation,
40 : Animation<double> secondaryAnimation, Widget child)? buildTransition;
41 :
42 : /// {@macro flutter.widgets.ModalRoute.maintainState}
43 : final bool maintainState;
44 :
45 : /// {@macro flutter.widgets.PageRoute.fullscreenDialog}
46 : final bool fullscreenDialog;
47 :
48 0 : VBasePage({
49 : required this.key,
50 : required this.child,
51 : this.maintainState = true,
52 : this.fullscreenDialog = false,
53 : this.name,
54 : this.buildTransition,
55 : this.transitionDuration,
56 : this.reverseTransitionDuration,
57 0 : }) : super(key: key);
58 :
59 11 : factory VBasePage.fromPlatform({
60 : required LocalKey key,
61 : required Widget child,
62 : String? name,
63 : Widget Function(Animation<double> animation,
64 : Animation<double> secondaryAnimation, Widget child)?
65 : buildTransition,
66 : Duration? transitionDuration,
67 : Duration? reverseTransitionDuration,
68 : }) =>
69 11 : (!kIsWeb && Platform.isIOS)
70 0 : ? VCupertinoPage(
71 : key: key,
72 : child: child,
73 : name: name,
74 : buildTransition: buildTransition,
75 : transitionDuration: transitionDuration,
76 : reverseTransitionDuration: reverseTransitionDuration,
77 : )
78 11 : : VMaterialPage(
79 : key: key,
80 : child: child,
81 : name: name,
82 : buildTransition: buildTransition,
83 : transitionDuration: transitionDuration,
84 : reverseTransitionDuration: reverseTransitionDuration,
85 : );
86 : }
87 :
88 : /// A page to put in [Navigator] pages
89 : ///
90 : /// This is a normal material page except that it allows for
91 : /// custom transitions easily.
92 : class VMaterialPage<T> extends MaterialPage<T> implements VBasePage<T> {
93 : /// The child of this page
94 : @override
95 : final Widget child;
96 :
97 : /// The name of this page
98 : @override
99 : final String? name;
100 :
101 : /// The key of this page
102 : @override
103 : final LocalKey key;
104 :
105 : /// The duration of the transition which happens when this page
106 : /// is put in the widget tree
107 : final Duration? transitionDuration;
108 :
109 : /// The duration of the transition which happens when this page
110 : /// is removed from the widget tree
111 : final Duration? reverseTransitionDuration;
112 :
113 : /// A function to build the transition to or from this route
114 : ///
115 : /// [child] is the child of the page
116 : ///
117 : /// Example of a fade transition:
118 : /// buildTransition: (animation, _, child) {
119 : /// return FadeTransition(opacity: animation, child: child);
120 : /// }
121 : ///
122 : /// If this is null, the default transition is the one of the [VRouter]
123 : /// If the one of the [VRouter] is also null, the default transition is
124 : /// the one of a [MaterialPage]
125 : final Widget Function(Animation<double> animation,
126 : Animation<double> secondaryAnimation, Widget child)? buildTransition;
127 :
128 11 : VMaterialPage({
129 : required this.key,
130 : required this.child,
131 : this.name,
132 : this.buildTransition,
133 : this.transitionDuration,
134 : this.reverseTransitionDuration,
135 11 : }) : super(key: key, child: child);
136 :
137 11 : @override
138 : Route<T> createRoute(BuildContext context) {
139 : // If any transition was given, use it
140 11 : if (buildTransition != null) {
141 0 : return VPageRoute<T>(
142 : page: this,
143 0 : customTransition: (_, Animation<double> animation,
144 : Animation<double> secondaryAnimation, Widget child) =>
145 0 : buildTransition!(
146 : animation,
147 : secondaryAnimation,
148 : child,
149 : ),
150 0 : transitionDuration: transitionDuration,
151 0 : reverseTransitionDuration: reverseTransitionDuration,
152 : );
153 22 : } else if (RootVRouterData.of(context)._defaultPageBuildTransition !=
154 : null) {
155 : // Else try to use the router transition
156 0 : return VPageRoute<T>(
157 : page: this,
158 0 : customTransition: (_, Animation<double> animation,
159 : Animation<double> secondaryAnimation, Widget child) =>
160 0 : RootVRouterData.of(context)._defaultPageBuildTransition!(
161 : animation,
162 : secondaryAnimation,
163 : child,
164 : ),
165 : transitionDuration:
166 0 : RootVRouterData.of(context)._defaultPageTransitionDuration,
167 : reverseTransitionDuration:
168 0 : RootVRouterData.of(context)._defaultPageReverseTransitionDuration,
169 : );
170 : }
171 :
172 : // Default is parent animation (ie MaterialPageRoute animation)
173 11 : return super.createRoute(context);
174 : }
175 : }
176 :
177 : /// A page to put in [Navigator] pages
178 : ///
179 : /// This is a normal cupertino page except that it allows for
180 : /// custom transitions easily.
181 : class VCupertinoPage<T> extends CupertinoPage<T> implements VBasePage<T> {
182 : /// The child of this page
183 : @override
184 : final Widget child;
185 :
186 : /// The name of this page
187 : @override
188 : final String? name;
189 :
190 : /// The key of this page
191 : @override
192 : final LocalKey key;
193 :
194 : /// The duration of the transition which happens when this page
195 : /// is put in the widget tree
196 : final Duration? transitionDuration;
197 :
198 : /// The duration of the transition which happens when this page
199 : /// is removed from the widget tree
200 : final Duration? reverseTransitionDuration;
201 :
202 : /// A function to build the transition to or from this route
203 : ///
204 : /// [child] is the child of the page
205 : ///
206 : /// Example of a fade transition:
207 : /// buildTransition: (animation, _, child) {
208 : /// return FadeTransition(opacity: animation, child: child);
209 : /// }
210 : ///
211 : /// If this is null, the default transition is the one of the [VRouter]
212 : /// If the one of the [VRouter] is also null, the default transition is
213 : /// the one of a [MaterialPage]
214 : final Widget Function(Animation<double> animation,
215 : Animation<double> secondaryAnimation, Widget child)? buildTransition;
216 :
217 0 : VCupertinoPage({
218 : required this.key,
219 : required this.child,
220 : this.name,
221 : this.buildTransition,
222 : this.transitionDuration,
223 : this.reverseTransitionDuration,
224 0 : }) : super(key: key, child: child);
225 :
226 0 : @override
227 : Route<T> createRoute(BuildContext context) {
228 : // If any transition was given, use it
229 0 : if (buildTransition != null) {
230 0 : return VPageRoute<T>(
231 : page: this,
232 0 : customTransition: (_, Animation<double> animation,
233 : Animation<double> secondaryAnimation, Widget child) =>
234 0 : buildTransition!(
235 : animation,
236 : secondaryAnimation,
237 : child,
238 : ),
239 0 : transitionDuration: transitionDuration,
240 0 : reverseTransitionDuration: reverseTransitionDuration,
241 : );
242 0 : } else if (RootVRouterData.of(context)._defaultPageBuildTransition !=
243 : null) {
244 : // Else try to use the router transition
245 0 : return VPageRoute<T>(
246 : page: this,
247 0 : customTransition: (_, Animation<double> animation,
248 : Animation<double> secondaryAnimation, Widget child) =>
249 0 : RootVRouterData.of(context)._defaultPageBuildTransition!(
250 : animation,
251 : secondaryAnimation,
252 : child,
253 : ),
254 : transitionDuration:
255 0 : RootVRouterData.of(context)._defaultPageTransitionDuration,
256 : reverseTransitionDuration:
257 0 : RootVRouterData.of(context)._defaultPageReverseTransitionDuration,
258 : );
259 : }
260 :
261 : // Default is parent animation (ie MaterialPageRoute animation)
262 0 : return super.createRoute(context);
263 : }
264 : }
265 :
266 : /// Helper to create a PageRoute which displays the desired animation
267 : class VPageRoute<T> extends PageRoute<T> {
268 : @override
269 : final Duration transitionDuration;
270 : @override
271 : final Duration reverseTransitionDuration;
272 : final Widget Function(BuildContext context, Animation<double> animation,
273 : Animation<double> secondaryAnimation, Widget child) customTransition;
274 :
275 0 : VPageRoute({
276 : required VBasePage<T> page,
277 : required this.customTransition,
278 : Duration? transitionDuration,
279 : Duration? reverseTransitionDuration,
280 0 : }) : transitionDuration = transitionDuration ?? Duration(milliseconds: 300),
281 : reverseTransitionDuration = reverseTransitionDuration ??
282 0 : (transitionDuration ?? Duration(milliseconds: 300)),
283 0 : super(settings: page) {
284 0 : assert(opaque);
285 : }
286 :
287 0 : VBasePage<T> get _page => settings as VBasePage<T>;
288 :
289 0 : @override
290 0 : bool get maintainState => _page.maintainState;
291 :
292 0 : @override
293 0 : bool get fullscreenDialog => _page.fullscreenDialog;
294 :
295 0 : @override
296 0 : String get debugLabel => '${super.debugLabel}(${_page.name})';
297 :
298 0 : @override
299 : Color get barrierColor => Colors.transparent;
300 :
301 0 : @override
302 0 : String get barrierLabel => settings.name ?? '';
303 :
304 0 : @override
305 : bool canTransitionTo(TransitionRoute<dynamic> nextRoute) {
306 : // Don't perform outgoing animation if the next route is a fullscreen dialog.
307 0 : return (nextRoute is MaterialRouteTransitionMixin &&
308 0 : !nextRoute.fullscreenDialog) ||
309 0 : (nextRoute is CupertinoRouteTransitionMixin &&
310 0 : !nextRoute.fullscreenDialog);
311 : }
312 :
313 0 : @override
314 : Widget buildPage(
315 : BuildContext context,
316 : Animation<double> animation,
317 : Animation<double> secondaryAnimation,
318 : ) {
319 0 : return _page.child;
320 : }
321 :
322 0 : @override
323 : Widget buildTransitions(BuildContext context, Animation<double> animation,
324 : Animation<double> secondaryAnimation, Widget child) {
325 0 : return customTransition(context, animation, secondaryAnimation, child);
326 : }
327 : }
|