Unverified Commit 1bb9f3f7 authored by chunhtai's avatar chunhtai Committed by GitHub

fix pushAndRemoveUntil incorrectly removes the routes below the first… (#56732)

parent c969b8af
......@@ -3435,9 +3435,8 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
assert(predicate != null);
int index = _history.length - 1;
_history.add(_RouteEntry(newRoute, initialState: _RouteLifecycle.push));
while (index >= 0) {
final _RouteEntry entry = _history[index];
if (entry.isPresent && !predicate(entry.route))
while (index >= 0 && !predicate(_history[index].route)) {
if (_history[index].isPresent)
_history[index].remove();
index -= 1;
}
......
......@@ -682,6 +682,42 @@ void main() {
expect(find.text('B'), isOnstage);
});
testWidgets('pushAndRemoveUntil does not remove routes below the first route that pass the predicate', (WidgetTester tester) async {
// Regression https://github.com/flutter/flutter/issues/56688
final GlobalKey<NavigatorState> navigator = GlobalKey<NavigatorState>();
final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
'/': (BuildContext context) => const Text('home'),
'/A': (BuildContext context) => const Text('page A'),
'/A/B': (BuildContext context) => OnTapPage(
id: 'B',
onTap: () {
Navigator.of(context).pushNamedAndRemoveUntil('/D', ModalRoute.withName('/A'));
},
),
'/D': (BuildContext context) => const Text('page D'),
};
await tester.pumpWidget(
MaterialApp(
navigatorKey: navigator,
routes: routes,
initialRoute: '/A/B',
)
);
await tester.pumpAndSettle();
await tester.tap(find.text('B'));
await tester.pumpAndSettle();
expect(find.text('page D'), isOnstage);
navigator.currentState.pop();
await tester.pumpAndSettle();
expect(find.text('page A'), isOnstage);
navigator.currentState.pop();
await tester.pumpAndSettle();
expect(find.text('home'), isOnstage);
});
testWidgets('replaceNamed returned value', (WidgetTester tester) async {
Future<String> value;
......
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