Index: sky/sdk/lib/widgets/navigator.dart |
diff --git a/sky/sdk/lib/widgets/navigator.dart b/sky/sdk/lib/widgets/navigator.dart |
index 4bbded3b71cc069c261c6a85a462d0ce1f4076c6..ea31c8bafed632a8bacc77c0896042ab36f1351e 100644 |
--- a/sky/sdk/lib/widgets/navigator.dart |
+++ b/sky/sdk/lib/widgets/navigator.dart |
@@ -7,51 +7,79 @@ import 'basic.dart'; |
typedef Widget Builder(Navigator navigator); |
abstract class RouteBase { |
- RouteBase({this.name}); |
+ RouteBase({ this.name }); |
final String name; |
Widget build(Navigator navigator); |
} |
class Route extends RouteBase { |
- Route({String name, this.builder}) : super(name: name); |
+ Route({ String name, this.builder }) : super(name: name); |
final Builder builder; |
Widget build(Navigator navigator) => builder(navigator); |
} |
class Navigator extends Component { |
- Navigator({Object key, this.currentRoute, this.routes}) |
- : super(key: key, stateful: true); |
+ Navigator({ Object key, RouteBase defaultRoute, List<RouteBase> routes }) |
+ : super(key: key, stateful: true) { |
+ if (routes != null) { |
+ if (defaultRoute == null) |
+ defaultRoute = routes[0]; |
+ for (Route route in routes) { |
+ if (route.name != null) |
+ namedRoutes[route.name] = route; |
+ } |
+ } |
+ assert(defaultRoute != null); |
+ _history.add(defaultRoute); |
+ } |
+ |
+ List<RouteBase> _history = new List<RouteBase>(); |
+ int _historyIndex = 0; |
+ Map<String, RouteBase> namedRoutes = new Map<String, RouteBase>(); |
- RouteBase currentRoute; |
- List<RouteBase> routes; |
- |
void syncFields(Navigator source) { |
- currentRoute = source.currentRoute; |
- routes = source.routes; |
- } |
- |
- void pushNamedRoute(String name) { |
- assert(routes != null); |
- for (RouteBase route in routes) { |
- if (route.name == name) { |
- setState(() { |
- currentRoute = route; |
- }); |
- return; |
+ namedRoutes = source.namedRoutes; |
+ } |
+ |
+ void pushNamed(String name) { |
+ Route route = namedRoutes[name]; |
+ assert(route != null); |
+ push(route); |
+ } |
+ |
+ void push(RouteBase route) { |
+ setState(() { |
+ // Discard future history |
+ _history.removeRange(_historyIndex + 1, _history.length); |
+ _historyIndex = _history.length; |
+ _history.add(route); |
+ }); |
+ } |
+ |
+ void pop() { |
+ setState(() { |
+ if (_historyIndex > 0) { |
+ _history.removeLast(); |
+ _historyIndex--; |
} |
- } |
- assert(false); // route not found |
+ }); |
} |
- void pushRoute(RouteBase route) { |
+ void back() { |
setState(() { |
- currentRoute = route; |
+ if (_historyIndex > 0) |
+ _historyIndex--; |
+ }); |
+ } |
+ |
+ void forward() { |
+ setState(() { |
+ _historyIndex++; |
+ assert(_historyIndex < _history.length); |
}); |
} |
Widget build() { |
- Route route = currentRoute == null ? routes[0] : currentRoute; |
- assert(route != null); |
- return route.build(this); |
+ return _history[_historyIndex].build(this); |
} |
} |