Showing
3 changed files
with
100 additions
and
7 deletions
@@ -5,10 +5,9 @@ class ParseRouteTree { | @@ -5,10 +5,9 @@ class ParseRouteTree { | ||
5 | final List<_ParseRouteTreeNode> _nodes = <_ParseRouteTreeNode>[]; | 5 | final List<_ParseRouteTreeNode> _nodes = <_ParseRouteTreeNode>[]; |
6 | 6 | ||
7 | // bool _hasDefaultRoute = false; | 7 | // bool _hasDefaultRoute = false; |
8 | - | ||
9 | void addRoute(GetPage route) { | 8 | void addRoute(GetPage route) { |
10 | var path = route.name; | 9 | var path = route.name; |
11 | - | 10 | + |
12 | if (path == Navigator.defaultRouteName) { | 11 | if (path == Navigator.defaultRouteName) { |
13 | // if (_hasDefaultRoute) { | 12 | // if (_hasDefaultRoute) { |
14 | // throw ("Default route was already defined"); | 13 | // throw ("Default route was already defined"); |
@@ -46,8 +45,50 @@ class ParseRouteTree { | @@ -46,8 +45,50 @@ class ParseRouteTree { | ||
46 | } | 45 | } |
47 | parent = node; | 46 | parent = node; |
48 | } | 47 | } |
48 | + | ||
49 | + // Add Page children. | ||
50 | + for (var page in _flattenPage(route)) { | ||
51 | + addRoute(page); | ||
52 | + } | ||
49 | } | 53 | } |
50 | 54 | ||
55 | + List<GetPage> _flattenPage(GetPage route) { | ||
56 | + final result = <GetPage>[]; | ||
57 | + if (route.pages == null || route.pages.isEmpty) { | ||
58 | + return result; | ||
59 | + } | ||
60 | + | ||
61 | + final routePath = route.name; | ||
62 | + for (var page in route.pages) { | ||
63 | + result.add(_changePath(page, routePath)); | ||
64 | + final children = _flattenPage(page); | ||
65 | + for (var child in children) { | ||
66 | + result.add(_changePath(child, routePath)); | ||
67 | + } | ||
68 | + } | ||
69 | + return result; | ||
70 | + } | ||
71 | + | ||
72 | + /// Change the Path for a [GetPage] | ||
73 | + GetPage _changePath(GetPage orgin, String routePath) => GetPage( | ||
74 | + name: routePath + orgin.name, | ||
75 | + page: orgin.page, | ||
76 | + title: orgin.title, | ||
77 | + alignment: orgin.alignment, | ||
78 | + transition: orgin.transition, | ||
79 | + binding: orgin.binding, | ||
80 | + bindings: orgin.bindings, | ||
81 | + curve: orgin.curve, | ||
82 | + customTransition: orgin.customTransition, | ||
83 | + fullscreenDialog: orgin.fullscreenDialog, | ||
84 | + maintainState: orgin.maintainState, | ||
85 | + opaque: orgin.opaque, | ||
86 | + parameter: orgin.parameter, | ||
87 | + popGesture: orgin.popGesture, | ||
88 | + settings: orgin.settings, | ||
89 | + transitionDuration: orgin.transitionDuration, | ||
90 | + ); | ||
91 | + | ||
51 | _GetPageMatch matchRoute(String path) { | 92 | _GetPageMatch matchRoute(String path) { |
52 | var usePath = path; | 93 | var usePath = path; |
53 | if (usePath.startsWith("/")) { | 94 | if (usePath.startsWith("/")) { |
@@ -126,9 +167,7 @@ class ParseRouteTree { | @@ -126,9 +167,7 @@ class ParseRouteTree { | ||
126 | var match = matches.first; | 167 | var match = matches.first; |
127 | var nodeToUse = match.node; | 168 | var nodeToUse = match.node; |
128 | 169 | ||
129 | - if (nodeToUse != null && | ||
130 | - nodeToUse.routes != null && | ||
131 | - nodeToUse.routes.length > 0) { | 170 | + if (nodeToUse != null && nodeToUse.routes != null && nodeToUse.routes.length > 0) { |
132 | var routes = nodeToUse.routes; | 171 | var routes = nodeToUse.routes; |
133 | var routeMatch = _GetPageMatch(routes[0]); | 172 | var routeMatch = _GetPageMatch(routes[0]); |
134 | 173 | ||
@@ -186,8 +225,7 @@ class ParseRouteTree { | @@ -186,8 +225,7 @@ class ParseRouteTree { | ||
186 | class _ParseRouteTreeNodeMatch { | 225 | class _ParseRouteTreeNodeMatch { |
187 | _ParseRouteTreeNodeMatch(this.node); | 226 | _ParseRouteTreeNodeMatch(this.node); |
188 | 227 | ||
189 | - _ParseRouteTreeNodeMatch.fromMatch( | ||
190 | - _ParseRouteTreeNodeMatch match, this.node) { | 228 | + _ParseRouteTreeNodeMatch.fromMatch(_ParseRouteTreeNodeMatch match, this.node) { |
191 | parameters = <String, String>{}; | 229 | parameters = <String, String>{}; |
192 | if (match != null) { | 230 | if (match != null) { |
193 | parameters.addAll(match.parameters); | 231 | parameters.addAll(match.parameters); |
@@ -20,6 +20,7 @@ class GetPage { | @@ -20,6 +20,7 @@ class GetPage { | ||
20 | final Duration transitionDuration; | 20 | final Duration transitionDuration; |
21 | final bool fullscreenDialog; | 21 | final bool fullscreenDialog; |
22 | final RouteSettings settings; | 22 | final RouteSettings settings; |
23 | + final List<GetPage> pages; | ||
23 | 24 | ||
24 | const GetPage({ | 25 | const GetPage({ |
25 | @required this.name, | 26 | @required this.name, |
@@ -38,6 +39,7 @@ class GetPage { | @@ -38,6 +39,7 @@ class GetPage { | ||
38 | this.transition, | 39 | this.transition, |
39 | this.customTransition, | 40 | this.customTransition, |
40 | this.fullscreenDialog = false, | 41 | this.fullscreenDialog = false, |
42 | + this.pages, | ||
41 | }) : assert(page != null), | 43 | }) : assert(page != null), |
42 | assert(name != null), | 44 | assert(name != null), |
43 | assert(maintainState != null), | 45 | assert(maintainState != null), |
test/navigation/parse_route_test.dart
0 → 100644
1 | +import 'package:flutter/cupertino.dart'; | ||
2 | +import 'package:flutter_test/flutter_test.dart'; | ||
3 | +import 'package:get/get.dart'; | ||
4 | +import 'package:get/get_navigation/src/root/parse_route.dart'; | ||
5 | + | ||
6 | +void main() { | ||
7 | + test('Parse Page with children', () { | ||
8 | + final tree = ParseRouteTree(); | ||
9 | + final pageTree = GetPage(name: '/city', page: () => Container(), pages: [ | ||
10 | + GetPage(name: '/home', page: () => Container(), pages: [ | ||
11 | + GetPage(name: '/bed-room', page: () => Container()), | ||
12 | + GetPage(name: '/living-room', page: () => Container()), | ||
13 | + ]), | ||
14 | + GetPage(name: '/work', page: () => Container(), pages: [ | ||
15 | + GetPage(name: '/office', page: () => Container(), pages: [ | ||
16 | + GetPage(name: '/pen', page: () => Container()), | ||
17 | + GetPage(name: '/paper', page: () => Container()), | ||
18 | + ]), | ||
19 | + GetPage(name: '/meeting-room', page: () => Container()), | ||
20 | + ]), | ||
21 | + ]); | ||
22 | + | ||
23 | + tree.addRoute(pageTree); | ||
24 | + final searchRoute = '/city/work/office/pen'; | ||
25 | + final match = tree.matchRoute(searchRoute); | ||
26 | + expect(match, isNotNull); | ||
27 | + expect(match.route.name, searchRoute); | ||
28 | + }); | ||
29 | + | ||
30 | + test('Parse Page without children', () { | ||
31 | + final tree = ParseRouteTree(); | ||
32 | + final pageTree = [ | ||
33 | + GetPage(name: '/city', page: () => Container()), | ||
34 | + GetPage(name: '/city/home', page: () => Container()), | ||
35 | + GetPage(name: '/city/home/bed-room', page: () => Container()), | ||
36 | + GetPage(name: '/city/home/living-room', page: () => Container()), | ||
37 | + GetPage(name: '/city/work', page: () => Container()), | ||
38 | + GetPage(name: '/city/work/office', page: () => Container()), | ||
39 | + GetPage(name: '/city/work/office/pen', page: () => Container()), | ||
40 | + GetPage(name: '/city/work/office/paper', page: () => Container()), | ||
41 | + GetPage(name: '/city/work/meeting-room', page: () => Container()), | ||
42 | + ]; | ||
43 | + | ||
44 | + for (var p in pageTree) { | ||
45 | + tree.addRoute(p); | ||
46 | + } | ||
47 | + | ||
48 | + final searchRoute = '/city/work/office/pen'; | ||
49 | + final match = tree.matchRoute(searchRoute); | ||
50 | + expect(match, isNotNull); | ||
51 | + expect(match.route.name, searchRoute); | ||
52 | + }); | ||
53 | +} |
-
Please register or login to post a comment