OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 import 'basic.dart'; | 5 import 'basic.dart'; |
6 | 6 |
7 typedef Widget Builder(Navigator navigator); | 7 typedef Widget Builder(Navigator navigator); |
8 | 8 |
9 abstract class RouteBase { | 9 abstract class RouteBase { |
10 RouteBase({ this.name }); | 10 RouteBase({ this.name }); |
11 final String name; | 11 final String name; |
12 Widget build(Navigator navigator); | 12 Widget build(Navigator navigator); |
13 } | 13 } |
14 | 14 |
15 class Route extends RouteBase { | 15 class Route extends RouteBase { |
16 Route({ String name, this.builder }) : super(name: name); | 16 Route({ String name, this.builder }) : super(name: name); |
17 final Builder builder; | 17 final Builder builder; |
18 Widget build(Navigator navigator) => builder(navigator); | 18 Widget build(Navigator navigator) => builder(navigator); |
19 } | 19 } |
20 | 20 |
21 class Navigator extends Component { | 21 class NavigationState { |
22 Navigator({ Object key, RouteBase defaultRoute, List<RouteBase> routes }) | 22 |
23 : super(key: key, stateful: true) { | 23 NavigationState(List<Route> routes) { |
24 if (routes != null) { | 24 for (Route route in routes) { |
25 if (defaultRoute == null) | 25 if (route.name != null) |
26 defaultRoute = routes[0]; | 26 namedRoutes[route.name] = route; |
27 for (Route route in routes) { | |
28 if (route.name != null) | |
29 namedRoutes[route.name] = route; | |
30 } | |
31 } | 27 } |
32 assert(defaultRoute != null); | 28 history.add(routes[0]); |
33 _history.add(defaultRoute); | |
34 } | 29 } |
35 | 30 |
36 List<RouteBase> _history = new List<RouteBase>(); | 31 List<RouteBase> history = new List<RouteBase>(); |
37 int _historyIndex = 0; | 32 int historyIndex = 0; |
38 Map<String, RouteBase> namedRoutes = new Map<String, RouteBase>(); | 33 Map<String, RouteBase> namedRoutes = new Map<String, RouteBase>(); |
39 | 34 |
40 void syncFields(Navigator source) { | 35 RouteBase get currentRoute => history[historyIndex]; |
41 namedRoutes = source.namedRoutes; | 36 bool hasPrevious() => historyIndex > 0; |
42 } | 37 bool hasNext() => history.length > historyIndex + 1; |
43 | 38 |
44 void pushNamed(String name) { | 39 void pushNamed(String name) { |
45 Route route = namedRoutes[name]; | 40 Route route = namedRoutes[name]; |
46 assert(route != null); | 41 assert(route != null); |
47 push(route); | 42 push(route); |
48 } | 43 } |
49 | 44 |
50 void push(RouteBase route) { | 45 void push(RouteBase route) { |
| 46 // Discard future history |
| 47 history.removeRange(historyIndex + 1, history.length); |
| 48 historyIndex = history.length; |
| 49 history.add(route); |
| 50 } |
| 51 |
| 52 void pop() { |
| 53 if (historyIndex > 0) { |
| 54 history.removeLast(); |
| 55 historyIndex--; |
| 56 } |
| 57 } |
| 58 |
| 59 void back() { |
| 60 if (historyIndex > 0) |
| 61 historyIndex--; |
| 62 } |
| 63 |
| 64 void forward() { |
| 65 historyIndex++; |
| 66 assert(historyIndex < history.length); |
| 67 } |
| 68 } |
| 69 |
| 70 class Navigator extends Component { |
| 71 |
| 72 Navigator(this.state, { String key }) : super(key: key); |
| 73 |
| 74 NavigationState state; |
| 75 |
| 76 void pushNamed(String name) { |
| 77 setState(() { |
| 78 state.pushNamed(name); |
| 79 }); |
| 80 } |
| 81 |
| 82 void push(RouteBase route) { |
51 setState(() { | 83 setState(() { |
52 // Discard future history | 84 state.push(route); |
53 _history.removeRange(_historyIndex + 1, _history.length); | |
54 _historyIndex = _history.length; | |
55 _history.add(route); | |
56 }); | 85 }); |
57 } | 86 } |
58 | 87 |
59 void pop() { | 88 void pop() { |
60 setState(() { | 89 setState(() { |
61 if (_historyIndex > 0) { | 90 state.pop(); |
62 _history.removeLast(); | |
63 _historyIndex--; | |
64 } | |
65 }); | 91 }); |
66 } | 92 } |
67 | 93 |
68 void back() { | 94 void back() { |
69 setState(() { | 95 setState(() { |
70 if (_historyIndex > 0) | 96 state.back(); |
71 _historyIndex--; | |
72 }); | 97 }); |
73 } | 98 } |
74 | 99 |
75 void forward() { | 100 void forward() { |
76 setState(() { | 101 setState(() { |
77 _historyIndex++; | 102 state.forward(); |
78 assert(_historyIndex < _history.length); | |
79 }); | 103 }); |
80 } | 104 } |
81 | 105 |
82 Widget build() { | 106 Widget build() { |
83 return _history[_historyIndex].build(this); | 107 return state.currentRoute.build(this); |
84 } | 108 } |
85 } | 109 } |
OLD | NEW |