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 9 : 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 9 : (!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 9 : : 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 9 : VMaterialPage({
129 : required this.key,
130 : required this.child,
131 : this.name,
132 : this.buildTransition,
133 : this.transitionDuration,
134 : this.reverseTransitionDuration,
135 9 : }) : super(key: key, child: child);
136 :
137 9 : @override
138 : Route<T> createRoute(BuildContext context) {
139 : // If any transition was given, use it
140 9 : 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 : }
154 :
155 : // Default is parent animation (ie MaterialPageRoute animation)
156 9 : return super.createRoute(context);
157 : }
158 : }
159 :
160 : /// A page to put in [Navigator] pages
161 : ///
162 : /// This is a normal cupertino page except that it allows for
163 : /// custom transitions easily.
164 : class VCupertinoPage<T> extends CupertinoPage<T> implements VBasePage<T> {
165 : /// The child of this page
166 : @override
167 : final Widget child;
168 :
169 : /// The name of this page
170 : @override
171 : final String? name;
172 :
173 : /// The key of this page
174 : @override
175 : final LocalKey key;
176 :
177 : /// The duration of the transition which happens when this page
178 : /// is put in the widget tree
179 : final Duration? transitionDuration;
180 :
181 : /// The duration of the transition which happens when this page
182 : /// is removed from the widget tree
183 : final Duration? reverseTransitionDuration;
184 :
185 : /// A function to build the transition to or from this route
186 : ///
187 : /// [child] is the child of the page
188 : ///
189 : /// Example of a fade transition:
190 : /// buildTransition: (animation, _, child) {
191 : /// return FadeTransition(opacity: animation, child: child);
192 : /// }
193 : ///
194 : /// If this is null, the default transition is the one of the [VRouter]
195 : /// If the one of the [VRouter] is also null, the default transition is
196 : /// the one of a [MaterialPage]
197 : final Widget Function(Animation<double> animation,
198 : Animation<double> secondaryAnimation, Widget child)? buildTransition;
199 :
200 0 : VCupertinoPage({
201 : required this.key,
202 : required this.child,
203 : this.name,
204 : this.buildTransition,
205 : this.transitionDuration,
206 : this.reverseTransitionDuration,
207 0 : }) : super(key: key, child: child);
208 :
209 0 : @override
210 : Route<T> createRoute(BuildContext context) {
211 : // If any transition was given, use it
212 0 : if (buildTransition != null) {
213 0 : return VPageRoute<T>(
214 : page: this,
215 0 : customTransition: (_, Animation<double> animation,
216 : Animation<double> secondaryAnimation, Widget child) =>
217 0 : buildTransition!(
218 : animation,
219 : secondaryAnimation,
220 : child,
221 : ),
222 0 : transitionDuration: transitionDuration,
223 0 : reverseTransitionDuration: reverseTransitionDuration,
224 : );
225 : }
226 :
227 : // Default is parent animation (ie MaterialPageRoute animation)
228 0 : return super.createRoute(context);
229 : }
230 : }
231 :
232 : /// Helper to create a PageRoute which displays the desired animation
233 : class VPageRoute<T> extends PageRoute<T> {
234 : @override
235 : final Duration transitionDuration;
236 : @override
237 : final Duration reverseTransitionDuration;
238 : final Widget Function(BuildContext context, Animation<double> animation,
239 : Animation<double> secondaryAnimation, Widget child) customTransition;
240 :
241 0 : VPageRoute({
242 : required VBasePage<T> page,
243 : required this.customTransition,
244 : Duration? transitionDuration,
245 : Duration? reverseTransitionDuration,
246 0 : }) : transitionDuration = transitionDuration ?? Duration(milliseconds: 300),
247 : reverseTransitionDuration = reverseTransitionDuration ??
248 0 : (transitionDuration ?? Duration(milliseconds: 300)),
249 0 : super(settings: page) {
250 0 : assert(opaque);
251 : }
252 :
253 0 : VBasePage<T> get _page => settings as VBasePage<T>;
254 :
255 0 : @override
256 0 : bool get maintainState => _page.maintainState;
257 :
258 0 : @override
259 0 : bool get fullscreenDialog => _page.fullscreenDialog;
260 :
261 0 : @override
262 0 : String get debugLabel => '${super.debugLabel}(${_page.name})';
263 :
264 0 : @override
265 : Color get barrierColor => Colors.transparent;
266 :
267 0 : @override
268 0 : String get barrierLabel => settings.name ?? '';
269 :
270 0 : @override
271 : bool canTransitionTo(TransitionRoute<dynamic> nextRoute) {
272 : // Don't perform outgoing animation if the next route is a fullscreen dialog.
273 0 : return (nextRoute is MaterialRouteTransitionMixin &&
274 0 : !nextRoute.fullscreenDialog) ||
275 0 : (nextRoute is CupertinoRouteTransitionMixin &&
276 0 : !nextRoute.fullscreenDialog);
277 : }
278 :
279 0 : @override
280 : Widget buildPage(
281 : BuildContext context,
282 : Animation<double> animation,
283 : Animation<double> secondaryAnimation,
284 : ) {
285 0 : return _page.child;
286 : }
287 :
288 0 : @override
289 : Widget buildTransitions(BuildContext context, Animation<double> animation,
290 : Animation<double> secondaryAnimation, Widget child) {
291 0 : return customTransition(context, animation, secondaryAnimation, child);
292 : }
293 : }
|