提交 fd396d02 编写于 作者: C Collin Jackson

Simple support for routing in Sky

R=abarth@chromium.org, abarth, hixie

Review URL: https://codereview.chromium.org/1186273002.
上级 fc5d7261
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:sky/widgets/basic.dart';
import 'package:sky/widgets/navigator.dart';
import 'package:sky/widgets/raised_button.dart';
List<Route> routes = [
new Route(
name: 'safety',
builder: (navigator) => new RaisedButton(
child: new Text('PRESS FORWARD'),
onPressed: () => navigator.pushNamedRoute('adventure')
)
),
new Route(
name: 'adventure',
builder: (navigator) => new RaisedButton(
child: new Text('NO WAIT! GO BACK!'),
onPressed: () => navigator.pushRoute(routes[0])
)
)
];
class NavigationExampleApp extends App {
UINode build() {
return new Navigator(routes: routes);
}
}
void main() {
App app = new NavigationExampleApp();
}
......@@ -114,6 +114,7 @@ dart_pkg("sdk") {
"lib/widgets/menu_divider.dart",
"lib/widgets/menu_item.dart",
"lib/widgets/modal_overlay.dart",
"lib/widgets/navigator.dart",
"lib/widgets/popup_menu.dart",
"lib/widgets/popup_menu_item.dart",
"lib/widgets/radio.dart",
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'basic.dart';
typedef UINode Builder(Navigator navigator);
abstract class RouteBase {
RouteBase({this.name});
final String name;
UINode build(Navigator navigator);
}
class Route extends RouteBase {
Route({String name, this.builder}) : super(name: name);
final Builder builder;
UINode build(Navigator navigator) => builder(navigator);
}
class Navigator extends Component {
Navigator({Object key, this.currentRoute, this.routes})
: super(key: key, stateful: true);
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;
}
}
assert(false); // route not found
}
void pushRoute(RouteBase route) {
setState(() {
currentRoute = route;
});
}
UINode build() {
Route route = currentRoute == null ? routes[0] : currentRoute;
assert(route != null);
return route.build(this);
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册