mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Simple support for routing in Sky
R=abarth@chromium.org, abarth, hixie Review URL: https://codereview.chromium.org/1186273002.
This commit is contained in:
parent
fc5d7261ec
commit
fd396d024d
34
examples/raw/navigation.dart
Normal file
34
examples/raw/navigation.dart
Normal file
@ -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<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",
|
||||
|
||||
57
sdk/lib/widgets/navigator.dart
Normal file
57
sdk/lib/widgets/navigator.dart
Normal file
@ -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<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);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user