提交 761ab683 编写于 作者: H Hixie

Rationalise usage of keys in navigator.dart.

Route (named routes) no longer have a key, and have their own storage for their names.
RouseState no longer has a key, and uses an owner field pointing to a StatefulComponent instead.
As such, RouteBase no longer has a key.

HistoryEntry no longer uses a global int to ensure uniqueness.

Propagated this to stocks app.
上级 24c88827
......@@ -69,7 +69,8 @@ class StockHome extends AnimatedComponent {
}
void _handleSearchEnd() {
assert(navigator.currentRoute.key == this);
assert(navigator.currentRoute is RouteState);
assert((navigator.currentRoute as RouteState).owner == this); // TODO(ianh): remove cast once analyzer is cleverer
navigator.pop();
setState(() {
_isSearching = false;
......
......@@ -61,7 +61,7 @@ class StockSettings extends StatefulComponent {
break;
case StockMode.pessimistic:
showModeDialog = true;
navigator.pushState("/settings/confirm", (_) {
navigator.pushState(this, (_) {
showModeDialog = false;
});
break;
......
......@@ -143,7 +143,8 @@ class Drawer extends AnimatedComponent {
if (_lastStatus != null && status != _lastStatus) {
if (status == DrawerStatus.inactive &&
navigator != null &&
navigator.currentRoute.key == this)
navigator.currentRoute is RouteState &&
(navigator.currentRoute as RouteState).owner == this) // TODO(ianh): remove cast once analyzer is cleverer
navigator.pop();
if (onStatusChanged != null)
onStatusChanged(status);
......
......@@ -12,25 +12,24 @@ import 'package:vector_math/vector_math.dart';
typedef Widget Builder(Navigator navigator, RouteBase route);
abstract class RouteBase {
RouteBase({ this.key });
final Object key;
Widget build(Navigator navigator, RouteBase route);
void popState() { }
}
class Route extends RouteBase {
Route({ String name, this.builder }) : super(key: name);
String get name => key;
Route({ this.name, this.builder });
final String name;
final Builder builder;
Widget build(Navigator navigator, RouteBase route) => builder(navigator, route);
}
class RouteState extends RouteBase {
RouteState({ this.callback, this.route, Object key }) : super(key: key);
RouteState({ this.callback, this.route, this.owner });
RouteBase route;
Function callback;
RouteBase route;
StatefulComponent owner;
Widget build(Navigator navigator, RouteBase route) => null;
......@@ -144,9 +143,8 @@ class Transition extends AnimatedComponent {
}
class HistoryEntry {
HistoryEntry({ this.route, this.key });
HistoryEntry({ this.route });
final RouteBase route;
final int key;
bool transitionFinished = false;
// TODO(jackson): Keep track of the requested transition
}
......@@ -158,12 +156,11 @@ class NavigationState {
if (route.name != null)
namedRoutes[route.name] = route;
}
history.add(new HistoryEntry(route: routes[0], key: _lastKey++));
history.add(new HistoryEntry(route: routes[0]));
}
List<HistoryEntry> history = new List<HistoryEntry>();
int historyIndex = 0;
int _lastKey = 0;
Map<String, RouteBase> namedRoutes = new Map<String, RouteBase>();
RouteBase get currentRoute => history[historyIndex].route;
......@@ -176,7 +173,7 @@ class NavigationState {
}
void push(RouteBase route) {
HistoryEntry historyEntry = new HistoryEntry(route: route, key: _lastKey++);
HistoryEntry historyEntry = new HistoryEntry(route: route);
history.insert(historyIndex + 1, historyEntry);
historyIndex++;
}
......@@ -203,9 +200,9 @@ class Navigator extends StatefulComponent {
RouteBase get currentRoute => state.currentRoute;
void pushState(Object key, Function callback) {
void pushState(StatefulComponent owner, Function callback) {
RouteBase route = new RouteState(
key: key,
owner: owner,
callback: callback,
route: state.currentRoute
);
......@@ -245,7 +242,7 @@ class Navigator extends StatefulComponent {
if (content == null)
continue;
Transition transition = new Transition(
key: historyEntry.key.toString(),
key: historyEntry.hashCode.toString(), // TODO(ianh): make it not collide
content: content,
direction: (i <= state.historyIndex) ? TransitionDirection.forward : TransitionDirection.reverse,
interactive: (i == state.historyIndex),
......
......@@ -122,8 +122,9 @@ class PopupMenu extends AnimatedComponent {
PopupMenuStatus status = _status;
if (_lastStatus != null && status != _lastStatus) {
if (status == PopupMenuStatus.inactive &&
navigator != null &&
navigator.currentRoute.key == this)
navigator != null &&
navigator.currentRoute is RouteState &&
(navigator.currentRoute as RouteState).owner == this) // TODO(ianh): remove cast once analyzer is cleverer
navigator.pop();
if (onStatusChanged != null)
onStatusChanged(status);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册