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, RouteBase route); |
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, RouteBase route); |
| 13 void popState() { } |
13 } | 14 } |
14 | 15 |
15 class Route extends RouteBase { | 16 class Route extends RouteBase { |
16 Route({ String name, this.builder }) : super(name: name); | 17 Route({ String name, this.builder }) : super(name: name); |
17 final Builder builder; | 18 final Builder builder; |
18 Widget build(Navigator navigator) => builder(navigator); | 19 Widget build(Navigator navigator, RouteBase route) => builder(navigator, route
); |
| 20 } |
| 21 |
| 22 class RouteState extends RouteBase { |
| 23 |
| 24 RouteState({this.callback, this.route, String name}) : super(name: name); |
| 25 |
| 26 RouteBase route; |
| 27 Function callback; |
| 28 |
| 29 Widget build(Navigator navigator, _) => route.build(navigator, this); |
| 30 |
| 31 void popState() { |
| 32 if (callback != null) |
| 33 callback(this); |
| 34 } |
19 } | 35 } |
20 | 36 |
21 class NavigationState { | 37 class NavigationState { |
22 | 38 |
23 NavigationState(List<Route> routes) { | 39 NavigationState(List<Route> routes) { |
24 for (Route route in routes) { | 40 for (Route route in routes) { |
25 if (route.name != null) | 41 if (route.name != null) |
26 namedRoutes[route.name] = route; | 42 namedRoutes[route.name] = route; |
27 } | 43 } |
28 history.add(routes[0]); | 44 history.add(routes[0]); |
(...skipping 15 matching lines...) Expand all Loading... |
44 | 60 |
45 void push(RouteBase route) { | 61 void push(RouteBase route) { |
46 // Discard future history | 62 // Discard future history |
47 history.removeRange(historyIndex + 1, history.length); | 63 history.removeRange(historyIndex + 1, history.length); |
48 historyIndex = history.length; | 64 historyIndex = history.length; |
49 history.add(route); | 65 history.add(route); |
50 } | 66 } |
51 | 67 |
52 void pop() { | 68 void pop() { |
53 if (historyIndex > 0) { | 69 if (historyIndex > 0) { |
| 70 history[historyIndex].popState(); |
54 history.removeLast(); | 71 history.removeLast(); |
55 historyIndex--; | 72 historyIndex--; |
56 } | 73 } |
57 } | 74 } |
58 | |
59 void back() { | |
60 if (historyIndex > 0) | |
61 historyIndex--; | |
62 } | |
63 | |
64 void forward() { | |
65 historyIndex++; | |
66 assert(historyIndex < history.length); | |
67 } | |
68 } | 75 } |
69 | 76 |
70 class Navigator extends Component { | 77 class Navigator extends Component { |
71 | 78 |
72 Navigator(this.state, { String key }) : super(key: key); | 79 Navigator(this.state, { String key }) : super(key: key, stateful: true); |
73 | 80 |
74 NavigationState state; | 81 NavigationState state; |
75 | 82 |
76 void syncFields(Navigator source) { | 83 void syncFields(Navigator source) { |
77 state = source.state; | 84 state = source.state; |
78 } | 85 } |
79 | 86 |
| 87 RouteBase get currentRoute => state.currentRoute; |
| 88 |
| 89 void pushState(String name, Function callback) { |
| 90 RouteBase route = new RouteState( |
| 91 name: name, |
| 92 callback: callback, |
| 93 route: state.currentRoute |
| 94 ); |
| 95 push(route); |
| 96 } |
| 97 |
80 void pushNamed(String name) { | 98 void pushNamed(String name) { |
81 setState(() { | 99 setState(() { |
82 state.pushNamed(name); | 100 state.pushNamed(name); |
83 }); | 101 }); |
84 } | 102 } |
85 | 103 |
86 void push(RouteBase route) { | 104 void push(RouteBase route) { |
87 setState(() { | 105 setState(() { |
88 state.push(route); | 106 state.push(route); |
89 }); | 107 }); |
90 } | 108 } |
91 | 109 |
92 void pop() { | 110 void pop() { |
93 setState(() { | 111 setState(() { |
94 state.pop(); | 112 state.pop(); |
95 }); | 113 }); |
96 } | 114 } |
97 | 115 |
98 void back() { | |
99 setState(() { | |
100 state.back(); | |
101 }); | |
102 } | |
103 | |
104 void forward() { | |
105 setState(() { | |
106 state.forward(); | |
107 }); | |
108 } | |
109 | |
110 Widget build() { | 116 Widget build() { |
111 return state.currentRoute.build(this); | 117 return state.currentRoute.build(this, state.currentRoute); |
112 } | 118 } |
113 } | 119 } |
OLD | NEW |