Add Navigator.of

Now you don't need to pass the navigator around everywhere.
This commit is contained in:
Adam Barth
2015-10-22 11:17:20 -07:00
parent 35bd448bdd
commit de395582d5
21 changed files with 139 additions and 146 deletions

View File

@@ -153,15 +153,15 @@ class _DialogRoute extends PerformanceRoute {
}
}
Future showDialog(NavigatorState navigator, DialogBuilder builder) {
Future showDialog({ BuildContext context, Widget child }) {
Completer completer = new Completer();
navigator.push(new _DialogRoute(
Navigator.of(context).push(new _DialogRoute(
completer: completer,
builder: (RouteArguments args) {
return new Focus(
key: new GlobalObjectKey(completer),
autofocus: true,
child: builder(args.navigator)
child: child
);
}
));

View File

@@ -169,7 +169,6 @@ class _DrawerRoute extends Route {
}
}
void showDrawer({ NavigatorState navigator, Widget child, int level: 3 }) {
assert(navigator != null);
navigator.push(new _DrawerRoute(child: child, level: level));
void showDrawer({ BuildContext context, Widget child, int level: 3 }) {
Navigator.of(context).push(new _DrawerRoute(child: child, level: level));
}

View File

@@ -22,14 +22,11 @@ const double _kMenuMaxWidth = 5.0 * _kMenuWidthStep;
const double _kMenuHorizontalPadding = 16.0;
const double _kMenuVerticalPadding = 8.0;
typedef List<PopupMenuItem> PopupMenuItemsBuilder(NavigatorState navigator);
class PopupMenu extends StatelessComponent {
PopupMenu({
Key key,
this.items,
this.level: 4,
this.navigator,
this.performance
}) : super(key: key) {
assert(items != null);
@@ -38,7 +35,6 @@ class PopupMenu extends StatelessComponent {
final List<PopupMenuItem> items;
final int level;
final NavigatorState navigator;
final PerformanceView performance;
Widget build(BuildContext context) {
@@ -58,7 +54,7 @@ class PopupMenu extends StatelessComponent {
performance: performance,
opacity: new AnimatedValue<double>(0.0, end: 1.0, curve: new Interval(start, end)),
child: new InkWell(
onTap: () { navigator.pop(items[i].value); },
onTap: () { Navigator.of(context).pop(items[i].value); },
child: items[i]
))
);
@@ -114,11 +110,11 @@ class MenuPosition {
}
class _MenuRoute extends PerformanceRoute {
_MenuRoute({ this.completer, this.position, this.builder, this.level });
_MenuRoute({ this.completer, this.position, this.items, this.level });
final Completer completer;
final MenuPosition position;
final PopupMenuItemsBuilder builder;
final List<PopupMenuItem> items;
final int level;
Performance createPerformance() {
@@ -144,9 +140,8 @@ class _MenuRoute extends PerformanceRoute {
key: new GlobalObjectKey(this),
autofocus: true,
child: new PopupMenu(
items: builder != null ? builder(navigator) : const <PopupMenuItem>[],
items: items,
level: level,
navigator: navigator,
performance: performance
)
)
@@ -159,12 +154,12 @@ class _MenuRoute extends PerformanceRoute {
}
}
Future showMenu({ NavigatorState navigator, MenuPosition position, PopupMenuItemsBuilder builder, int level: 4 }) {
Future showMenu({ BuildContext context, MenuPosition position, List<PopupMenuItem> items, int level: 4 }) {
Completer completer = new Completer();
navigator.push(new _MenuRoute(
Navigator.of(context).push(new _MenuRoute(
completer: completer,
position: position,
builder: builder,
items: items,
level: level
));
return completer.future;

View File

@@ -105,7 +105,7 @@ class _SnackBarRoute extends PerformanceRoute {
Widget build(RouteArguments args) => null;
}
void showSnackBar({ NavigatorState navigator, GlobalKey<PlaceholderState> placeholderKey, Widget content, List<SnackBarAction> actions }) {
void showSnackBar({ BuildContext context, GlobalKey<PlaceholderState> placeholderKey, Widget content, List<SnackBarAction> actions }) {
Route route = new _SnackBarRoute();
SnackBar snackBar = new SnackBar(
content: content,
@@ -113,5 +113,5 @@ void showSnackBar({ NavigatorState navigator, GlobalKey<PlaceholderState> placeh
performance: route.performance
);
placeholderKey.currentState.child = snackBar;
navigator.push(route);
Navigator.of(context).push(route);
}

View File

@@ -7,7 +7,6 @@ import 'package:flutter/rendering.dart';
import 'basic.dart';
import 'framework.dart';
import 'navigator.dart';
import 'transitions.dart';
// Heroes are the parts of an application's screen-to-screen transitions where a
@@ -51,7 +50,7 @@ import 'transitions.dart';
// TODO(ianh): If the widgets use Inherited properties, they are taken from the
// Navigator's position in the widget hierarchy, not the source or target. We
// should interpolate the inherited properties from their value at the source to
// their value at the target. See: https://github.com/flutter/engine/issues/1698
// their value at the target. See: https://github.com/flutter/engine/issues/1698
final Object centerOfAttentionHeroTag = new Object();
@@ -77,7 +76,6 @@ abstract class HeroHandle {
class Hero extends StatefulComponent {
Hero({
Key key,
this.navigator,
this.tag,
this.child,
this.turns: 1
@@ -85,7 +83,6 @@ class Hero extends StatefulComponent {
assert(tag != null);
}
final NavigatorState navigator;
final Object tag;
final Widget child;
final int turns;
@@ -205,7 +202,7 @@ class HeroState extends State<Hero> implements HeroHandle {
child: config.child
)
);
case _HeroMode.taken:
case _HeroMode.taken:
return new SizedBox(width: _size.width, height: _size.height);
}
}

View File

@@ -47,6 +47,22 @@ class Navigator extends StatefulComponent {
final RouteGenerator onGenerateRoute;
final RouteBuilder onUnknownRoute;
static NavigatorState of(BuildContext context) {
NavigatorState result;
bool visitor(Element element) {
if (element is StatefulComponentElement) {
if (element.state is NavigatorState) {
result = element.state;
return false;
}
}
return true;
}
if (visitor(context))
context.visitAncestorElements(visitor);
return result;
}
NavigatorState createState() => new NavigatorState();
}
@@ -54,7 +70,7 @@ class Navigator extends StatefulComponent {
// It also animates between these pages.
// Pages can have "heroes", which are UI elements that animate from point to point.
// These animations are called journeys.
//
//
// Journeys can start in two conditions:
// - Everything is calm, and we have no heroes in flight. In this case, we will
// have to collect the heroes from the route we're starting at and the route

View File

@@ -21,7 +21,7 @@ void main() {
);
tester.pump(); // no effect
expect(tester.findText('drawer'), isNull);
showDrawer(navigator: navigator, child: new Text('drawer'));
showDrawer(context: navigator.context, child: new Text('drawer'));
tester.pump(); // drawer should be starting to animate in
expect(tester.findText('drawer'), isNotNull);
tester.pump(new Duration(seconds: 1)); // animation done
@@ -50,7 +50,7 @@ void main() {
);
tester.pump(); // no effect
expect(tester.findText('drawer'), isNull);
showDrawer(navigator: navigator, child: new Text('drawer'));
showDrawer(context: navigator.context, child: new Text('drawer'));
tester.pump(); // drawer should be starting to animate in
expect(tester.findText('drawer'), isNotNull);
tester.pump(new Duration(seconds: 1)); // animation done

View File

@@ -16,7 +16,7 @@ void main() {
return new GestureDetector(
onTap: () {
showSnackBar(
navigator: args.navigator,
context: args.navigator.context,
placeholderKey: placeholderKey,
content: new Text(helloSnackBar)
);