Pesto home stack (#5168)

This commit is contained in:
Hans Muller
2016-08-01 15:09:50 -07:00
committed by GitHub
parent fb3e64cce4
commit 56a2d2262c
5 changed files with 128 additions and 14 deletions

View File

@@ -161,6 +161,9 @@ class NavigatorObserver {
void didPop(Route<dynamic> route, Route<dynamic> previousRoute) { }
}
/// Signature for the [Navigator.popUntil] predicate argument.
typedef bool RoutePredicate(Route<dynamic> route);
/// A widget that manages a set of child widgets with a stack discipline.
///
/// Many apps have a navigator near the top of their widget hierarchy in order
@@ -243,10 +246,11 @@ class Navigator extends StatefulWidget {
return Navigator.of(context).pop(result);
}
/// Calls pop() repeatedly until the given route is the current route.
/// If it is already the current route, nothing happens.
static void popUntil(BuildContext context, Route<dynamic> targetRoute) {
Navigator.of(context).popUntil(targetRoute);
/// Calls [pop()] repeatedly until the predicate returns false.
/// The predicate may be applied to the same route more than once if
/// [Route.willHandlePopInternally] is true.
static void popUntil(BuildContext context, RoutePredicate predicate) {
Navigator.of(context).popUntil(predicate);
}
/// Whether the navigator that most tightly encloses the given context can be popped.
@@ -463,9 +467,8 @@ class NavigatorState extends State<Navigator> {
return true;
}
void popUntil(Route<dynamic> targetRoute) {
assert(_history.contains(targetRoute));
while (!targetRoute.isCurrent)
void popUntil(RoutePredicate predicate) {
while (!predicate(_history.last))
pop();
}

View File

@@ -487,6 +487,17 @@ abstract class ModalRoute<T> extends TransitionRoute<T> with LocalHistoryRoute<T
}
}
/// Returns a predicate that's true if the route has the specified name and if
/// popping the route will not yield the same route, i.e. if the route's
/// [willHandlePopInternally] property is false.
///
/// This function is typically used with [Navigator.popUntil()].
static RoutePredicate withName(String name) {
return (Route<dynamic> route) {
return !route.willHandlePopInternally
&& route is ModalRoute && route.settings.name == name;
};
}
// The API for subclasses to override - used by _ModalScope

View File

@@ -11,7 +11,7 @@ final List<String> results = <String>[];
Set<TestRoute> routes = new HashSet<TestRoute>();
class TestRoute extends Route<String> {
class TestRoute extends LocalHistoryRoute<String> {
TestRoute(this.name);
final String name;
@@ -329,7 +329,7 @@ void main() {
await runNavigatorTest(
tester,
host,
() { host.popUntil(routeB); },
() { host.popUntil((Route<dynamic> route) => route == routeB); },
<String>[
'C: didPop null',
'C: dispose',
@@ -341,4 +341,42 @@ void main() {
expect(routes.isEmpty, isTrue);
results.clear();
});
testWidgets('Route localHistory - popUntil', (WidgetTester tester) async {
TestRoute routeA = new TestRoute('A');
routeA.addLocalHistoryEntry(new LocalHistoryEntry(
onRemove: () { routeA.log('onRemove 0'); }
));
routeA.addLocalHistoryEntry(new LocalHistoryEntry(
onRemove: () { routeA.log('onRemove 1'); }
));
GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>();
await tester.pumpWidget(new Navigator(
key: navigatorKey,
onGenerateRoute: (_) => routeA
));
NavigatorState host = navigatorKey.currentState;
await runNavigatorTest(
tester,
host,
() { host.popUntil((Route<dynamic> route) => !route.willHandlePopInternally); },
<String>[
'A: install',
'A: didPush',
'A: didChangeNext null',
'A: didPop null',
'A: onRemove 1',
'A: didPop null',
'A: onRemove 0',
]
);
await runNavigatorTest(
tester,
host,
() { host.popUntil((Route<dynamic> route) => !route.willHandlePopInternally); },
<String>[
]
);
});
}