diff --git a/examples/raw/navigation.dart b/examples/raw/navigation.dart new file mode 100644 index 00000000000..86b1dfe4708 --- /dev/null +++ b/examples/raw/navigation.dart @@ -0,0 +1,34 @@ +// 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 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(); +} diff --git a/sdk/BUILD.gn b/sdk/BUILD.gn index da3579ebbc7..6e73249c8d7 100644 --- a/sdk/BUILD.gn +++ b/sdk/BUILD.gn @@ -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", diff --git a/sdk/lib/widgets/navigator.dart b/sdk/lib/widgets/navigator.dart new file mode 100644 index 00000000000..dc4c0bc9ab2 --- /dev/null +++ b/sdk/lib/widgets/navigator.dart @@ -0,0 +1,57 @@ +// 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 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