Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: sky/sdk/lib/widgets/navigator.dart

Issue 1181773006: Add back/forward history to navigation (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: rebase Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sky/examples/widgets/navigation.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 Navigator extends Component {
22 Navigator({Object key, this.currentRoute, this.routes}) 22 Navigator({ Object key, RouteBase defaultRoute, List<RouteBase> routes })
23 : super(key: key, stateful: true); 23 : super(key: key, stateful: true) {
24 24 if (routes != null) {
25 RouteBase currentRoute; 25 if (defaultRoute == null)
26 List<RouteBase> routes; 26 defaultRoute = routes[0];
27 27 for (Route route in routes) {
28 void syncFields(Navigator source) { 28 if (route.name != null)
29 currentRoute = source.currentRoute; 29 namedRoutes[route.name] = route;
30 routes = source.routes; 30 }
31 }
32 assert(defaultRoute != null);
33 _history.add(defaultRoute);
31 } 34 }
32 35
33 void pushNamedRoute(String name) { 36 List<RouteBase> _history = new List<RouteBase>();
34 assert(routes != null); 37 int _historyIndex = 0;
35 for (RouteBase route in routes) { 38 Map<String, RouteBase> namedRoutes = new Map<String, RouteBase>();
36 if (route.name == name) { 39
37 setState(() { 40 void syncFields(Navigator source) {
38 currentRoute = route; 41 namedRoutes = source.namedRoutes;
39 });
40 return;
41 }
42 }
43 assert(false); // route not found
44 } 42 }
45 43
46 void pushRoute(RouteBase route) { 44 void pushNamed(String name) {
45 Route route = namedRoutes[name];
46 assert(route != null);
47 push(route);
48 }
49
50 void push(RouteBase route) {
47 setState(() { 51 setState(() {
48 currentRoute = route; 52 // Discard future history
53 _history.removeRange(_historyIndex + 1, _history.length);
54 _historyIndex = _history.length;
55 _history.add(route);
56 });
57 }
58
59 void pop() {
60 setState(() {
61 if (_historyIndex > 0) {
62 _history.removeLast();
63 _historyIndex--;
64 }
65 });
66 }
67
68 void back() {
69 setState(() {
70 if (_historyIndex > 0)
71 _historyIndex--;
72 });
73 }
74
75 void forward() {
76 setState(() {
77 _historyIndex++;
78 assert(_historyIndex < _history.length);
49 }); 79 });
50 } 80 }
51 81
52 Widget build() { 82 Widget build() {
53 Route route = currentRoute == null ? routes[0] : currentRoute; 83 return _history[_historyIndex].build(this);
54 assert(route != null);
55 return route.build(this);
56 } 84 }
57 } 85 }
OLDNEW
« no previous file with comments | « sky/examples/widgets/navigation.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698