Showing
2 changed files
with
25 additions
and
11 deletions
1 | import 'package:flutter/widgets.dart'; | 1 | import 'package:flutter/widgets.dart'; |
2 | +import 'package:get/get.dart'; | ||
2 | import '../routes/get_route.dart'; | 3 | import '../routes/get_route.dart'; |
3 | 4 | ||
4 | class ParseRouteTree { | 5 | class ParseRouteTree { |
@@ -7,7 +8,7 @@ class ParseRouteTree { | @@ -7,7 +8,7 @@ class ParseRouteTree { | ||
7 | // bool _hasDefaultRoute = false; | 8 | // bool _hasDefaultRoute = false; |
8 | void addRoute(GetPage route) { | 9 | void addRoute(GetPage route) { |
9 | var path = route.name; | 10 | var path = route.name; |
10 | - | 11 | + |
11 | if (path == Navigator.defaultRouteName) { | 12 | if (path == Navigator.defaultRouteName) { |
12 | // if (_hasDefaultRoute) { | 13 | // if (_hasDefaultRoute) { |
13 | // throw ("Default route was already defined"); | 14 | // throw ("Default route was already defined"); |
@@ -58,20 +59,26 @@ class ParseRouteTree { | @@ -58,20 +59,26 @@ class ParseRouteTree { | ||
58 | return result; | 59 | return result; |
59 | } | 60 | } |
60 | 61 | ||
61 | - final routePath = route.name; | 62 | + final parentPath = route.name; |
62 | for (var page in route.children) { | 63 | for (var page in route.children) { |
63 | - result.add(_changePath(page, routePath)); | 64 | + // Add Parent middlewares to children |
65 | + final pageMiddlewares = page.middlewares ?? <GetMiddleware>[]; | ||
66 | + pageMiddlewares.addAll(route.middlewares ?? <GetMiddleware>[]); | ||
67 | + result.add(_addChild(page, parentPath, pageMiddlewares)); | ||
64 | final children = _flattenPage(page); | 68 | final children = _flattenPage(page); |
65 | for (var child in children) { | 69 | for (var child in children) { |
66 | - result.add(_changePath(child, routePath)); | 70 | + pageMiddlewares.addAll(child.middlewares ?? <GetMiddleware>[]); |
71 | + result.add(_addChild(child, parentPath, pageMiddlewares)); | ||
67 | } | 72 | } |
68 | } | 73 | } |
69 | return result; | 74 | return result; |
70 | } | 75 | } |
71 | 76 | ||
72 | /// Change the Path for a [GetPage] | 77 | /// Change the Path for a [GetPage] |
73 | - GetPage _changePath(GetPage origin, String routePath) => GetPage( | ||
74 | - name: routePath + origin.name, | 78 | + GetPage _addChild( |
79 | + GetPage origin, String parentPath, List<GetMiddleware> middlewares) => | ||
80 | + GetPage( | ||
81 | + name: parentPath + origin.name, | ||
75 | page: origin.page, | 82 | page: origin.page, |
76 | title: origin.title, | 83 | title: origin.title, |
77 | alignment: origin.alignment, | 84 | alignment: origin.alignment, |
@@ -87,7 +94,7 @@ class ParseRouteTree { | @@ -87,7 +94,7 @@ class ParseRouteTree { | ||
87 | popGesture: origin.popGesture, | 94 | popGesture: origin.popGesture, |
88 | settings: origin.settings, | 95 | settings: origin.settings, |
89 | transitionDuration: origin.transitionDuration, | 96 | transitionDuration: origin.transitionDuration, |
90 | - middlewares: origin.middlewares, | 97 | + middlewares: middlewares, |
91 | ); | 98 | ); |
92 | 99 | ||
93 | _GetPageMatch matchRoute(String path) { | 100 | _GetPageMatch matchRoute(String path) { |
@@ -168,7 +175,9 @@ class ParseRouteTree { | @@ -168,7 +175,9 @@ class ParseRouteTree { | ||
168 | var match = matches.first; | 175 | var match = matches.first; |
169 | var nodeToUse = match.node; | 176 | var nodeToUse = match.node; |
170 | 177 | ||
171 | - if (nodeToUse != null && nodeToUse.routes != null && nodeToUse.routes.length > 0) { | 178 | + if (nodeToUse != null && |
179 | + nodeToUse.routes != null && | ||
180 | + nodeToUse.routes.length > 0) { | ||
172 | var routes = nodeToUse.routes; | 181 | var routes = nodeToUse.routes; |
173 | var routeMatch = _GetPageMatch(routes[0]); | 182 | var routeMatch = _GetPageMatch(routes[0]); |
174 | 183 | ||
@@ -226,7 +235,8 @@ class ParseRouteTree { | @@ -226,7 +235,8 @@ class ParseRouteTree { | ||
226 | class _ParseRouteTreeNodeMatch { | 235 | class _ParseRouteTreeNodeMatch { |
227 | _ParseRouteTreeNodeMatch(this.node); | 236 | _ParseRouteTreeNodeMatch(this.node); |
228 | 237 | ||
229 | - _ParseRouteTreeNodeMatch.fromMatch(_ParseRouteTreeNodeMatch match, this.node) { | 238 | + _ParseRouteTreeNodeMatch.fromMatch( |
239 | + _ParseRouteTreeNodeMatch match, this.node) { | ||
230 | parameters = <String, String>{}; | 240 | parameters = <String, String>{}; |
231 | if (match != null) { | 241 | if (match != null) { |
232 | parameters.addAll(match.parameters); | 242 | parameters.addAll(match.parameters); |
@@ -208,7 +208,11 @@ class PageRedirect { | @@ -208,7 +208,11 @@ class PageRedirect { | ||
208 | if (match.route.middlewares == null || match.route.middlewares.isEmpty) { | 208 | if (match.route.middlewares == null || match.route.middlewares.isEmpty) { |
209 | return false; | 209 | return false; |
210 | } | 210 | } |
211 | - | ||
212 | - return runner.runRedirect() != null; | 211 | + final newSettings = runner.runRedirect(); |
212 | + if (newSettings == null) { | ||
213 | + return false; | ||
214 | + } | ||
215 | + settings = newSettings; | ||
216 | + return true; | ||
213 | } | 217 | } |
214 | } | 218 | } |
-
Please register or login to post a comment