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