mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Cleans up navigator pop and remove logic (#175612)
<!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> fixes https://github.com/flutter/flutter/issues/170250 The original issue is that when a page gets stuck in the adding stage due to top-level animation is still ongoing, removing that route causes it to be in a inconsistent state. While fixing this issue, I also do some clean up on the onDidRemovePage logic because the existing logic can't handle this kind of operation ## Pre-launch Checklist - [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [ ] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [ ] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [ ] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [ ] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
This commit is contained in:
parent
1fff278a93
commit
13b4e3bc33
@ -3163,6 +3163,10 @@ class _RouteEntry extends RouteTransitionRecord {
|
||||
_RoutePlaceholder? lastAnnouncedNextRoute = notAnnounced; // last argument to Route.didChangeNext
|
||||
int? lastFocusNode; // The last focused semantic node for the route entry.
|
||||
|
||||
// Whether this route is removed without using a Navigator.pages api.
|
||||
// For example, Navigator.pop or Navigator.pushReplacement.
|
||||
bool imperativeRemoval = false;
|
||||
|
||||
/// Restoration ID to be used for the encapsulating route when restoration is
|
||||
/// enabled for it or null if restoration cannot be enabled for it.
|
||||
String? get restorationId {
|
||||
@ -3302,10 +3306,6 @@ class _RouteEntry extends RouteTransitionRecord {
|
||||
return false;
|
||||
}
|
||||
route.onPopInvokedWithResult(true, pendingResult);
|
||||
if (pageBased) {
|
||||
final Page<Object?> page = route.settings as Page<Object?>;
|
||||
navigator.widget.onDidRemovePage?.call(page);
|
||||
}
|
||||
pendingResult = null;
|
||||
return true;
|
||||
}
|
||||
@ -3322,8 +3322,14 @@ class _RouteEntry extends RouteTransitionRecord {
|
||||
required Route<dynamic>? previousPresent,
|
||||
}) {
|
||||
assert(navigator._debugLocked);
|
||||
assert(route._navigator == navigator);
|
||||
currentState = _RouteLifecycle.removing;
|
||||
if (route._navigator == navigator) {
|
||||
currentState = _RouteLifecycle.removing;
|
||||
} else {
|
||||
// This route is still waiting to be added while a top-most push or pop
|
||||
// animation is still on-going. In this case, this route can be disposed
|
||||
// directly since nothing has been initialized yet.
|
||||
currentState = _RouteLifecycle.dispose;
|
||||
}
|
||||
if (_reportRemovalToObserver) {
|
||||
navigator._observedRouteDeletions.add(_NavigatorRemoveObservation(route, previousPresent));
|
||||
}
|
||||
@ -3343,21 +3349,17 @@ class _RouteEntry extends RouteTransitionRecord {
|
||||
|
||||
Object? pendingResult;
|
||||
|
||||
void pop<T>(T? result) {
|
||||
void pop<T>(T? result, {required bool imperativeRemoval}) {
|
||||
assert(isPresent);
|
||||
pendingResult = result;
|
||||
currentState = _RouteLifecycle.pop;
|
||||
this.imperativeRemoval = imperativeRemoval;
|
||||
}
|
||||
|
||||
bool _reportRemovalToObserver = true;
|
||||
|
||||
// Route completes with `result` and is removed.
|
||||
void complete<T>(T result, {bool isReplaced = false}) {
|
||||
assert(
|
||||
!pageBased || isWaitingForExitingDecision,
|
||||
'A page-based route cannot be completed using imperative api, provide a '
|
||||
'new list without the corresponding Page to Navigator.pages instead. ',
|
||||
);
|
||||
void complete<T>(T result, {required bool isReplaced, required bool imperativeRemoval}) {
|
||||
if (currentState.index >= _RouteLifecycle.remove.index) {
|
||||
return;
|
||||
}
|
||||
@ -3365,6 +3367,7 @@ class _RouteEntry extends RouteTransitionRecord {
|
||||
_reportRemovalToObserver = !isReplaced;
|
||||
pendingResult = result;
|
||||
currentState = _RouteLifecycle.complete;
|
||||
this.imperativeRemoval = imperativeRemoval;
|
||||
}
|
||||
|
||||
void finalize() {
|
||||
@ -3529,7 +3532,7 @@ class _RouteEntry extends RouteTransitionRecord {
|
||||
final bool popResult = route.didPop(result);
|
||||
assert(!popResult);
|
||||
}
|
||||
pop<dynamic>(result);
|
||||
pop<dynamic>(result, imperativeRemoval: false);
|
||||
_isWaitingForExitingDecision = false;
|
||||
}
|
||||
|
||||
@ -3541,7 +3544,7 @@ class _RouteEntry extends RouteTransitionRecord {
|
||||
'been made or it does not require an explicit decision on how to transition '
|
||||
'out.',
|
||||
);
|
||||
complete<dynamic>(result);
|
||||
complete<dynamic>(result, isReplaced: false, imperativeRemoval: false);
|
||||
_isWaitingForExitingDecision = false;
|
||||
}
|
||||
|
||||
@ -4491,21 +4494,21 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
|
||||
navigator: this,
|
||||
previousPresent: _getRouteBefore(index, _RouteEntry.willBePresentPredicate)?.route,
|
||||
);
|
||||
assert(entry.currentState == _RouteLifecycle.removing);
|
||||
assert(entry.currentState.index >= _RouteLifecycle.removing.index);
|
||||
continue;
|
||||
case _RouteLifecycle.removing:
|
||||
if (!canRemoveOrAdd && next != null) {
|
||||
// We aren't allowed to remove this route yet.
|
||||
break;
|
||||
}
|
||||
if (entry.pageBased) {
|
||||
widget.onDidRemovePage?.call(entry.route.settings as Page<Object?>);
|
||||
}
|
||||
entry.currentState = _RouteLifecycle.dispose;
|
||||
continue;
|
||||
case _RouteLifecycle.dispose:
|
||||
// Delay disposal until didChangeNext/didChangePrevious have been sent.
|
||||
toBeDisposed.add(_history.removeAt(index));
|
||||
if (entry.pageBased && entry.imperativeRemoval) {
|
||||
widget.onDidRemovePage?.call(entry.route.settings as Page<Object?>);
|
||||
}
|
||||
entry = next;
|
||||
case _RouteLifecycle.disposing:
|
||||
case _RouteLifecycle.disposed:
|
||||
@ -5185,7 +5188,9 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
|
||||
'Navigator has no active routes to replace.',
|
||||
);
|
||||
assert(entry.currentState == _RouteLifecycle.pushReplace);
|
||||
_history.lastWhere(_RouteEntry.isPresentPredicate).complete(result, isReplaced: true);
|
||||
_history
|
||||
.lastWhere(_RouteEntry.isPresentPredicate)
|
||||
.complete(result, isReplaced: true, imperativeRemoval: true);
|
||||
_history.add(entry);
|
||||
_flushHistoryUpdates();
|
||||
assert(() {
|
||||
@ -5284,7 +5289,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
|
||||
_history.add(entry);
|
||||
while (index >= 0 && !predicate(_history[index].route)) {
|
||||
if (_history[index].isPresent) {
|
||||
_history[index].complete(null);
|
||||
_history[index].complete(null, isReplaced: false, imperativeRemoval: true);
|
||||
}
|
||||
index -= 1;
|
||||
}
|
||||
@ -5369,7 +5374,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
|
||||
);
|
||||
final bool wasCurrent = oldRoute.isCurrent;
|
||||
_history.insert(index + 1, entry);
|
||||
_history[index].complete(null, isReplaced: true);
|
||||
_history[index].complete(null, isReplaced: true, imperativeRemoval: true);
|
||||
_flushHistoryUpdates();
|
||||
assert(() {
|
||||
_debugLocked = false;
|
||||
@ -5459,7 +5464,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
|
||||
}
|
||||
assert(index >= 0, 'There are no routes below the specified anchorRoute.');
|
||||
_history.insert(index + 1, entry);
|
||||
_history[index].complete(null, isReplaced: true);
|
||||
_history[index].complete(null, isReplaced: true, imperativeRemoval: true);
|
||||
_flushHistoryUpdates();
|
||||
assert(() {
|
||||
_debugLocked = false;
|
||||
@ -5582,7 +5587,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
|
||||
entry.route.onPopInvokedWithResult(true, result);
|
||||
}
|
||||
} else {
|
||||
entry.pop<T>(result);
|
||||
entry.pop<T>(result, imperativeRemoval: true);
|
||||
assert(entry.currentState == _RouteLifecycle.pop);
|
||||
}
|
||||
if (entry.currentState == _RouteLifecycle.pop) {
|
||||
@ -5634,7 +5639,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
|
||||
assert(route._navigator == this);
|
||||
final bool wasCurrent = route.isCurrent;
|
||||
final _RouteEntry entry = _history.firstWhere(_RouteEntry.isRoutePredicate(route));
|
||||
entry.complete(result);
|
||||
entry.complete(result, isReplaced: false, imperativeRemoval: true);
|
||||
_flushHistoryUpdates(rearrangeOverlay: false);
|
||||
assert(() {
|
||||
_debugLocked = false;
|
||||
@ -5671,7 +5676,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
|
||||
index -= 1;
|
||||
}
|
||||
assert(index >= 0, 'There are no routes below the specified anchorRoute.');
|
||||
_history[index].complete(result);
|
||||
_history[index].complete(result, isReplaced: false, imperativeRemoval: true);
|
||||
_flushHistoryUpdates(rearrangeOverlay: false);
|
||||
assert(() {
|
||||
_debugLocked = false;
|
||||
|
||||
@ -0,0 +1,127 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
Future<void> buildPages(
|
||||
List<Page<void>> pages,
|
||||
WidgetTester tester, {
|
||||
GlobalKey<NavigatorState>? navKey,
|
||||
required List<Page<void>> removedPage,
|
||||
}) {
|
||||
return tester.pumpWidget(
|
||||
MediaQuery(
|
||||
data: MediaQueryData.fromView(tester.view),
|
||||
child: Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: Navigator(key: navKey, pages: pages, onDidRemovePage: removedPage.add),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
testWidgets('Page API will not call onDidRemovePage', (WidgetTester tester) async {
|
||||
final List<Page<void>> removedPages = <Page<void>>[];
|
||||
|
||||
const MaterialPage<void> page = MaterialPage<void>(
|
||||
key: ValueKey<String>('page'),
|
||||
child: Text('page'),
|
||||
);
|
||||
const MaterialPage<void> page1 = MaterialPage<void>(
|
||||
key: ValueKey<String>('page1'),
|
||||
child: Text('page1'),
|
||||
);
|
||||
const MaterialPage<void> page2 = MaterialPage<void>(
|
||||
key: ValueKey<String>('page2'),
|
||||
child: Text('page2'),
|
||||
);
|
||||
const MaterialPage<void> page3 = MaterialPage<void>(
|
||||
key: ValueKey<String>('page3'),
|
||||
child: Text('page3'),
|
||||
);
|
||||
const MaterialPage<void> page4 = MaterialPage<void>(
|
||||
key: ValueKey<String>('page4'),
|
||||
child: Text('page4'),
|
||||
);
|
||||
const MaterialPage<void> page5 = MaterialPage<void>(
|
||||
key: ValueKey<String>('page5'),
|
||||
child: Text('page5'),
|
||||
);
|
||||
const MaterialPage<void> page6 = MaterialPage<void>(
|
||||
key: ValueKey<String>('page6'),
|
||||
child: Text('page6'),
|
||||
);
|
||||
await buildPages(<Page<void>>[page], tester, removedPage: removedPages);
|
||||
|
||||
expect(find.text('page'), findsOneWidget);
|
||||
|
||||
await buildPages(<Page<void>>[page, page1, page2, page3], tester, removedPage: removedPages);
|
||||
await buildPages(<Page<void>>[page, page4, page5, page6], tester, removedPage: removedPages);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('page6'), findsOneWidget);
|
||||
expect(removedPages, isEmpty);
|
||||
|
||||
await buildPages(<Page<void>>[page, page4, page5], tester, removedPage: removedPages);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('page5'), findsOneWidget);
|
||||
expect(removedPages, isEmpty);
|
||||
|
||||
await buildPages(<Page<void>>[page], tester, removedPage: removedPages);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('page'), findsOneWidget);
|
||||
expect(removedPages, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('pop calls onDidRemovePage', (WidgetTester tester) async {
|
||||
final GlobalKey<NavigatorState> key = GlobalKey<NavigatorState>();
|
||||
final List<Page<void>> removedPage = <Page<void>>[];
|
||||
|
||||
const MaterialPage<void> page = MaterialPage<void>(
|
||||
key: ValueKey<String>('page'),
|
||||
child: Text('page'),
|
||||
);
|
||||
const MaterialPage<void> page1 = MaterialPage<void>(
|
||||
key: ValueKey<String>('page1'),
|
||||
child: Text('page1'),
|
||||
);
|
||||
await buildPages(<Page<void>>[page, page1], tester, removedPage: removedPage, navKey: key);
|
||||
|
||||
expect(find.text('page1'), findsOneWidget);
|
||||
|
||||
key.currentState!.pop();
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('page'), findsOneWidget);
|
||||
expect(removedPage, <Page<void>>[page1]);
|
||||
});
|
||||
|
||||
testWidgets('pushReplacement calls onDidRemovePage', (WidgetTester tester) async {
|
||||
final GlobalKey<NavigatorState> key = GlobalKey<NavigatorState>();
|
||||
final List<Page<void>> removedPage = <Page<void>>[];
|
||||
|
||||
const MaterialPage<void> page = MaterialPage<void>(
|
||||
key: ValueKey<String>('page'),
|
||||
child: Text('page'),
|
||||
);
|
||||
const MaterialPage<void> page1 = MaterialPage<void>(
|
||||
key: ValueKey<String>('page1'),
|
||||
child: Text('page1'),
|
||||
);
|
||||
await buildPages(<Page<void>>[page, page1], tester, removedPage: removedPage, navKey: key);
|
||||
|
||||
expect(find.text('page1'), findsOneWidget);
|
||||
|
||||
key.currentState!.pushReplacement(
|
||||
MaterialPageRoute<void>(
|
||||
builder: (_) {
|
||||
return const Text('new page');
|
||||
},
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('new page'), findsOneWidget);
|
||||
expect(removedPage, <Page<void>>[page1]);
|
||||
});
|
||||
}
|
||||
@ -250,40 +250,91 @@ void main() {
|
||||
expect(tester.widget<Overlay>(find.byType(Overlay)).clipBehavior, Clip.none);
|
||||
});
|
||||
|
||||
testWidgets('Navigator should call observer.didPop when pops page-based routes', (
|
||||
WidgetTester tester,
|
||||
) async {
|
||||
testWidgets('Navigator can set clip behavior', (WidgetTester tester) async {
|
||||
const MaterialPage<void> page = MaterialPage<void>(child: Text('page'));
|
||||
const MaterialPage<void> page1 = MaterialPage<void>(child: Text('page1'));
|
||||
final List<Page<void>> pages = <Page<void>>[page, page1];
|
||||
final _MockNavigatorObserver observer = _MockNavigatorObserver();
|
||||
final GlobalKey<NavigatorState> nav = GlobalKey<NavigatorState>();
|
||||
await tester.pumpWidget(
|
||||
MediaQuery(
|
||||
data: MediaQueryData.fromView(tester.view),
|
||||
child: Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: Navigator(pages: const <Page<void>>[page], onPopPage: (_, _) => false),
|
||||
),
|
||||
),
|
||||
);
|
||||
// Default to hard edge.
|
||||
expect(tester.widget<Overlay>(find.byType(Overlay)).clipBehavior, Clip.hardEdge);
|
||||
|
||||
await tester.pumpWidget(
|
||||
MediaQuery(
|
||||
data: MediaQueryData.fromView(tester.view),
|
||||
child: Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: Navigator(
|
||||
key: nav,
|
||||
pages: pages,
|
||||
observers: <NavigatorObserver>[observer],
|
||||
onPopPage: (Route<dynamic> route, dynamic result) {
|
||||
route.didPop(result);
|
||||
pages.remove(route.settings);
|
||||
return true;
|
||||
},
|
||||
pages: const <Page<void>>[page],
|
||||
clipBehavior: Clip.none,
|
||||
onPopPage: (_, _) => false,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(tester.widget<Overlay>(find.byType(Overlay)).clipBehavior, Clip.none);
|
||||
});
|
||||
|
||||
expect(find.text('page1'), findsOneWidget);
|
||||
testWidgets('Can remove page before they are added', (WidgetTester tester) async {
|
||||
final GlobalKey<NavigatorState> key = GlobalKey<NavigatorState>();
|
||||
Future<void> buildPages(List<Page<void>> pages) {
|
||||
return tester.pumpWidget(
|
||||
MediaQuery(
|
||||
data: MediaQueryData.fromView(tester.view),
|
||||
child: Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: Navigator(key: key, pages: pages, onDidRemovePage: (_) {}),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
const MaterialPage<void> page = MaterialPage<void>(
|
||||
key: ValueKey<String>('page'),
|
||||
child: Text('page'),
|
||||
);
|
||||
const MaterialPage<void> page1 = MaterialPage<void>(
|
||||
key: ValueKey<String>('page1'),
|
||||
child: Text('page1'),
|
||||
);
|
||||
const MaterialPage<void> page2 = MaterialPage<void>(
|
||||
key: ValueKey<String>('page2'),
|
||||
child: Text('page2'),
|
||||
);
|
||||
const MaterialPage<void> page3 = MaterialPage<void>(
|
||||
key: ValueKey<String>('page3'),
|
||||
child: Text('page3'),
|
||||
);
|
||||
const MaterialPage<void> page4 = MaterialPage<void>(
|
||||
key: ValueKey<String>('page4'),
|
||||
child: Text('page4'),
|
||||
);
|
||||
const MaterialPage<void> page5 = MaterialPage<void>(
|
||||
key: ValueKey<String>('page5'),
|
||||
child: Text('page5'),
|
||||
);
|
||||
const MaterialPage<void> page6 = MaterialPage<void>(
|
||||
key: ValueKey<String>('page6'),
|
||||
child: Text('page6'),
|
||||
);
|
||||
await buildPages(<Page<void>>[page]);
|
||||
|
||||
observer._invocations.clear();
|
||||
nav.currentState!.pop();
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('page'), findsOneWidget);
|
||||
observer._checkInvocations(const <Symbol>[#didPop, #didChangeTop]);
|
||||
|
||||
await buildPages(<Page<void>>[page, page1, page2, page3]);
|
||||
|
||||
// In mid transition;
|
||||
await tester.pump(const Duration(microseconds: 150));
|
||||
// Cause page1 and page2 to be removed before they are added.
|
||||
await buildPages(<Page<void>>[page, page4, page5, page6]);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('page6'), findsOneWidget);
|
||||
expect(tester.takeException(), isNull);
|
||||
});
|
||||
|
||||
testWidgets('Zero transition page-based route correctly notifies observers when it is popped', (
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user