Showing
4 changed files
with
77 additions
and
21 deletions
@@ -11,8 +11,8 @@ import 'default_transitions.dart'; | @@ -11,8 +11,8 @@ import 'default_transitions.dart'; | ||
11 | import 'transitions_type.dart'; | 11 | import 'transitions_type.dart'; |
12 | 12 | ||
13 | class GetPageRoute<T> extends PageRoute<T> { | 13 | class GetPageRoute<T> extends PageRoute<T> { |
14 | - GetPageRoute({ | ||
15 | - RouteSettings settings, | 14 | + GetPageRoute( |
15 | + {RouteSettings settings, | ||
16 | this.transitionDuration = const Duration(milliseconds: 300), | 16 | this.transitionDuration = const Duration(milliseconds: 300), |
17 | this.opaque = true, | 17 | this.opaque = true, |
18 | this.parameter, | 18 | this.parameter, |
@@ -30,8 +30,8 @@ class GetPageRoute<T> extends PageRoute<T> { | @@ -30,8 +30,8 @@ class GetPageRoute<T> extends PageRoute<T> { | ||
30 | this.barrierLabel, | 30 | this.barrierLabel, |
31 | this.maintainState = true, | 31 | this.maintainState = true, |
32 | bool fullscreenDialog = false, | 32 | bool fullscreenDialog = false, |
33 | - this.middlewares | ||
34 | - }) : assert(opaque != null), | 33 | + this.middlewares}) |
34 | + : assert(opaque != null), | ||
35 | assert(barrierDismissible != null), | 35 | assert(barrierDismissible != null), |
36 | assert(maintainState != null), | 36 | assert(maintainState != null), |
37 | assert(fullscreenDialog != null), | 37 | assert(fullscreenDialog != null), |
@@ -383,6 +383,8 @@ class GetPageRoute<T> extends PageRoute<T> { | @@ -383,6 +383,8 @@ class GetPageRoute<T> extends PageRoute<T> { | ||
383 | WidgetsBinding.instance.addPostFrameCallback((_) => GetInstance() | 383 | WidgetsBinding.instance.addPostFrameCallback((_) => GetInstance() |
384 | .removeDependencyByRoute("${settings?.name ?? routeName}")); | 384 | .removeDependencyByRoute("${settings?.name ?? routeName}")); |
385 | } | 385 | } |
386 | + final middlewareRunner = MiddlewareRunner(middlewares); | ||
387 | + middlewareRunner.runOnPageDispose(); | ||
386 | super.dispose(); | 388 | super.dispose(); |
387 | } | 389 | } |
388 | } | 390 | } |
@@ -48,4 +48,46 @@ class GetPage { | @@ -48,4 +48,46 @@ class GetPage { | ||
48 | assert(name != null), | 48 | assert(name != null), |
49 | assert(maintainState != null), | 49 | assert(maintainState != null), |
50 | assert(fullscreenDialog != null); | 50 | assert(fullscreenDialog != null); |
51 | + | ||
52 | + GetPage copyWith({ | ||
53 | + String name, | ||
54 | + GetPageBuilder page, | ||
55 | + bool popGesture, | ||
56 | + Map<String, String> parameter, | ||
57 | + String title, | ||
58 | + Transition transition, | ||
59 | + Curve curve, | ||
60 | + Alignment alignment, | ||
61 | + bool maintainState, | ||
62 | + bool opaque, | ||
63 | + Bindings binding, | ||
64 | + List<Bindings> bindings, | ||
65 | + CustomTransition customTransition, | ||
66 | + Duration transitionDuration, | ||
67 | + bool fullscreenDialog, | ||
68 | + RouteSettings settings, | ||
69 | + List<GetPage> children, | ||
70 | + List<GetMiddleware> middlewares, | ||
71 | + }) { | ||
72 | + return GetPage( | ||
73 | + name: name ?? this.name, | ||
74 | + page: page ?? this.page, | ||
75 | + popGesture: popGesture ?? this.popGesture, | ||
76 | + parameter: parameter ?? this.parameter, | ||
77 | + title: title ?? this.title, | ||
78 | + transition: transition ?? this.transition, | ||
79 | + curve: curve ?? this.curve, | ||
80 | + alignment: alignment ?? this.alignment, | ||
81 | + maintainState: maintainState ?? this.maintainState, | ||
82 | + opaque: opaque ?? this.opaque, | ||
83 | + binding: binding ?? this.binding, | ||
84 | + bindings: bindings ?? this.bindings, | ||
85 | + customTransition: customTransition ?? this.customTransition, | ||
86 | + transitionDuration: transitionDuration ?? this.transitionDuration, | ||
87 | + fullscreenDialog: fullscreenDialog ?? this.fullscreenDialog, | ||
88 | + settings: settings ?? this.settings, | ||
89 | + children: children ?? this.children, | ||
90 | + middlewares: middlewares ?? this.middlewares, | ||
91 | + ); | ||
92 | + } | ||
51 | } | 93 | } |
@@ -18,17 +18,19 @@ abstract class _RouteMiddleware { | @@ -18,17 +18,19 @@ abstract class _RouteMiddleware { | ||
18 | /// {@end-tool} | 18 | /// {@end-tool} |
19 | int priority; | 19 | int priority; |
20 | 20 | ||
21 | - /// This function will be the first thing to call when this Page is called | ||
22 | - /// you can use it to redirect befor anything in this page happend. | 21 | + /// This function will be called when the page of |
22 | + /// the called route is being searched for. | ||
23 | + /// It take RouteSettings as a result an redirect to the new settings or | ||
24 | + /// give it null and there will be no redirecting. | ||
23 | /// {@tool snippet} | 25 | /// {@tool snippet} |
24 | /// ```dart | 26 | /// ```dart |
25 | - /// GetPage redirect( ) { | 27 | + /// GetPage redirect(String route) { |
26 | /// final authService = Get.find<AuthService>(); | 28 | /// final authService = Get.find<AuthService>(); |
27 | - /// return authService.isAuthed ? null : '/login'; | 29 | + /// return authService.authed.value ? null : RouteSettings(name: '/login'); |
28 | /// } | 30 | /// } |
29 | /// ``` | 31 | /// ``` |
30 | /// {@end-tool} | 32 | /// {@end-tool} |
31 | - RouteSettings redirect(); | 33 | + RouteSettings redirect(String route); |
32 | 34 | ||
33 | /// This function will be called when this Page is called | 35 | /// This function will be called when this Page is called |
34 | /// you can use it to change something about the page or give it new page | 36 | /// you can use it to change something about the page or give it new page |
@@ -36,8 +38,7 @@ abstract class _RouteMiddleware { | @@ -36,8 +38,7 @@ abstract class _RouteMiddleware { | ||
36 | /// ```dart | 38 | /// ```dart |
37 | /// GetPage onPageCalled(GetPage page) { | 39 | /// GetPage onPageCalled(GetPage page) { |
38 | /// final authService = Get.find<AuthService>(); | 40 | /// final authService = Get.find<AuthService>(); |
39 | - /// page.title = 'Wellcome ${authService.UserName}'; | ||
40 | - /// return page; | 41 | + /// return page.copyWith(title: 'Wellcome ${authService.UserName}'); |
41 | /// } | 42 | /// } |
42 | /// ``` | 43 | /// ``` |
43 | /// {@end-tool} | 44 | /// {@end-tool} |
@@ -45,13 +46,25 @@ abstract class _RouteMiddleware { | @@ -45,13 +46,25 @@ abstract class _RouteMiddleware { | ||
45 | 46 | ||
46 | /// This function will be called right before the [Bindings] are initialize. | 47 | /// This function will be called right before the [Bindings] are initialize. |
47 | /// Here you can change [Bindings] for this page | 48 | /// Here you can change [Bindings] for this page |
49 | + /// {@tool snippet} | ||
50 | + /// ```dart | ||
51 | + /// List<Bindings> onBindingsStart(List<Bindings> bindings) { | ||
52 | + /// final authService = Get.find<AuthService>(); | ||
53 | + /// if (authService.isAdmin) { | ||
54 | + /// bindings.add(AdminBinding()); | ||
55 | + /// } | ||
56 | + /// return bindings; | ||
57 | + /// } | ||
58 | + /// ``` | ||
59 | + /// {@end-tool} | ||
48 | List<Bindings> onBindingsStart(List<Bindings> bindings); | 60 | List<Bindings> onBindingsStart(List<Bindings> bindings); |
49 | 61 | ||
50 | /// This function will be called right after the [Bindings] are initialize. | 62 | /// This function will be called right after the [Bindings] are initialize. |
51 | - /// Here you can change the Page to build | ||
52 | GetPageBuilder onPageBuildStart(GetPageBuilder page); | 63 | GetPageBuilder onPageBuildStart(GetPageBuilder page); |
53 | 64 | ||
54 | - // Get the built page | 65 | + /// This function will be called right after the |
66 | + /// GetPage.page function is called and will give you the result | ||
67 | + /// of the function. and take the widget that will be showed. | ||
55 | Widget onPageBuilt(Widget page); | 68 | Widget onPageBuilt(Widget page); |
56 | 69 | ||
57 | void onPageDispose(); | 70 | void onPageDispose(); |
@@ -59,8 +72,8 @@ abstract class _RouteMiddleware { | @@ -59,8 +72,8 @@ abstract class _RouteMiddleware { | ||
59 | 72 | ||
60 | /// The Page Middlewares. | 73 | /// The Page Middlewares. |
61 | /// The Functions will be called in this order | 74 | /// The Functions will be called in this order |
62 | -/// (( [redirect] -> [onBindingsStart] -> [onPageBuildStart] -> | ||
63 | -/// [onPageBuilt] -> [onPageDispose] )) | 75 | +/// (( [redirect] -> [onPageCalled] -> [onBindingsStart] -> |
76 | +/// [onPageBuildStart] -> [onPageBuilt] -> [onPageDispose] )) | ||
64 | class GetMiddleware implements _RouteMiddleware { | 77 | class GetMiddleware implements _RouteMiddleware { |
65 | @override | 78 | @override |
66 | int priority = 0; | 79 | int priority = 0; |
@@ -68,7 +81,7 @@ class GetMiddleware implements _RouteMiddleware { | @@ -68,7 +81,7 @@ class GetMiddleware implements _RouteMiddleware { | ||
68 | GetMiddleware({this.priority}); | 81 | GetMiddleware({this.priority}); |
69 | 82 | ||
70 | @override | 83 | @override |
71 | - RouteSettings redirect() => null; | 84 | + RouteSettings redirect(String route) => null; |
72 | 85 | ||
73 | @override | 86 | @override |
74 | GetPage onPageCalled(GetPage page) => page; | 87 | GetPage onPageCalled(GetPage page) => page; |
@@ -106,10 +119,10 @@ class MiddlewareRunner { | @@ -106,10 +119,10 @@ class MiddlewareRunner { | ||
106 | return page; | 119 | return page; |
107 | } | 120 | } |
108 | 121 | ||
109 | - RouteSettings runRedirect() { | 122 | + RouteSettings runRedirect(String route) { |
110 | RouteSettings to; | 123 | RouteSettings to; |
111 | _getMiddlewares().forEach((element) { | 124 | _getMiddlewares().forEach((element) { |
112 | - to = element.redirect(); | 125 | + to = element.redirect(route); |
113 | }); | 126 | }); |
114 | if (to != null) { | 127 | if (to != null) { |
115 | Get.log('Redirect to $to'); | 128 | Get.log('Redirect to $to'); |
@@ -208,7 +221,7 @@ class PageRedirect { | @@ -208,7 +221,7 @@ class PageRedirect { | ||
208 | if (match.route.middlewares == null || match.route.middlewares.isEmpty) { | 221 | if (match.route.middlewares == null || match.route.middlewares.isEmpty) { |
209 | return false; | 222 | return false; |
210 | } | 223 | } |
211 | - final newSettings = runner.runRedirect(); | 224 | + final newSettings = runner.runRedirect(settings.name); |
212 | if (newSettings == null) { | 225 | if (newSettings == null) { |
213 | return false; | 226 | return false; |
214 | } | 227 | } |
@@ -6,7 +6,7 @@ import 'get_main_test.dart'; | @@ -6,7 +6,7 @@ import 'get_main_test.dart'; | ||
6 | 6 | ||
7 | class RedirectMiddleware extends GetMiddleware { | 7 | class RedirectMiddleware extends GetMiddleware { |
8 | @override | 8 | @override |
9 | - RouteSettings redirect() => RouteSettings(name: '/second'); | 9 | + RouteSettings redirect(String route) => RouteSettings(name: '/second'); |
10 | } | 10 | } |
11 | 11 | ||
12 | main() { | 12 | main() { |
@@ -33,4 +33,3 @@ main() { | @@ -33,4 +33,3 @@ main() { | ||
33 | expect(find.byType(SecondScreen), findsOneWidget); | 33 | expect(find.byType(SecondScreen), findsOneWidget); |
34 | }); | 34 | }); |
35 | } | 35 | } |
36 | - |
-
Please register or login to post a comment