Commit cf7a3b04 authored by Hixie's avatar Hixie

Navigator.canPop(context)

parent 93b1f100
......@@ -37,6 +37,9 @@ abstract class Route<T> {
/// responsibility of the Route to later call dispose().
bool didPop(T result) => true;
/// Whether calling didPop() would return false.
bool get willHandlePopInternally => false;
/// The given route has been pushed onto the navigator after this route.
void didPushNext(Route nextRoute) { }
......@@ -129,6 +132,11 @@ class Navigator extends StatefulComponent {
return returnValue;
}
static bool canPop(BuildContext context) {
NavigatorState navigator = context.ancestorStateOfType(NavigatorState);
return navigator.canPop();
}
static void popAndPushNamed(BuildContext context, String routeName, { Set<Key> mostValuableKeys }) {
openTransaction(context, (NavigatorTransaction transaction) {
transaction.pop();
......@@ -280,7 +288,10 @@ class NavigatorState extends State<Navigator> {
assert(() { _debugLocked = true; return true; });
Route route = _history.last;
assert(route._navigator == this);
bool debugPredictedWouldPop;
assert(() { debugPredictedWouldPop = !route.willHandlePopInternally; return true; });
if (route.didPop(result)) {
assert(debugPredictedWouldPop);
if (_history.length > 1) {
setState(() {
// We use setState to guarantee that we'll rebuild, since the routes
......@@ -295,6 +306,8 @@ class NavigatorState extends State<Navigator> {
assert(() { _debugLocked = false; return true; });
return false;
}
} else {
assert(!debugPredictedWouldPop);
}
assert(() { _debugLocked = false; return true; });
return true;
......@@ -306,6 +319,11 @@ class NavigatorState extends State<Navigator> {
_pop();
}
bool canPop() {
assert(_history.length > 0);
return _history.length > 1 || _history[0].willHandlePopInternally;
}
bool _hadTransaction = true;
bool openTransaction(NavigatorTransactionCallback callback) {
......
......@@ -236,6 +236,9 @@ abstract class LocalHistoryRoute<T> extends Route<T> {
}
return super.didPop(result);
}
bool get willHandlePopInternally {
return _localHistory != null && _localHistory.length > 0;
}
}
class _ModalScopeStatus extends InheritedWidget {
......
......@@ -29,7 +29,9 @@ void main() {
expect(tester.findText('Settings'), isNull);
expect(tester.findText('Overlay'), isNull);
expect(Navigator.canPop(containerKey1.currentContext), isFalse);
Navigator.pushNamed(containerKey1.currentContext, '/settings');
expect(Navigator.canPop(containerKey1.currentContext), isTrue);
tester.pump();
......@@ -63,6 +65,7 @@ void main() {
expect(tester.findText('Settings'), isOnStage);
expect(tester.findText('Overlay'), isOnStage);
expect(Navigator.canPop(containerKey2.currentContext), isTrue);
Navigator.pop(containerKey2.currentContext);
tester.pump();
......@@ -76,6 +79,7 @@ void main() {
expect(tester.findText('Settings'), isOnStage);
expect(tester.findText('Overlay'), isNull);
expect(Navigator.canPop(containerKey2.currentContext), isTrue);
Navigator.pop(containerKey2.currentContext);
tester.pump();
......@@ -89,6 +93,8 @@ void main() {
expect(tester.findText('Settings'), isNull);
expect(tester.findText('Overlay'), isNull);
expect(Navigator.canPop(containerKey1.currentContext), isFalse);
});
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment