diff --git a/packages/flutter/lib/src/widgets/overlay.dart b/packages/flutter/lib/src/widgets/overlay.dart index 6dc44e97e3..a9da324058 100644 --- a/packages/flutter/lib/src/widgets/overlay.dart +++ b/packages/flutter/lib/src/widgets/overlay.dart @@ -123,23 +123,26 @@ class OverlayEntry { /// /// This should only be called once. /// - /// If this method is called while the [SchedulerBinding.schedulerPhase] is - /// [SchedulerPhase.persistentCallbacks], i.e. during the build, layout, or - /// paint phases (see [WidgetsBinding.drawFrame]), then the removal is - /// delayed until the post-frame callbacks phase. Otherwise the removal is - /// done synchronously. This means that it is safe to call during builds, but - /// also that if you do call this during a build, the UI will not update until - /// the next frame (i.e. many milliseconds later). + /// This method removes this overlay entry from the overlay immediately. The + /// UI will be updated in the same frame if this method is called before the + /// overlay rebuild in this frame; otherwise, the UI will be updated in the + /// next frame. This means that it is safe to call during builds, but also + /// that if you do call this after the overlay rebuild, the UI will not update + /// until the next frame (i.e. many milliseconds later). void remove() { assert(_overlay != null); final OverlayState overlay = _overlay; _overlay = null; + if (!overlay.mounted) + return; + + overlay._entries.remove(this); if (SchedulerBinding.instance.schedulerPhase == SchedulerPhase.persistentCallbacks) { SchedulerBinding.instance.addPostFrameCallback((Duration duration) { - overlay._remove(this); + overlay._markDirty(); }); } else { - overlay._remove(this); + overlay._markDirty(); } } @@ -405,11 +408,9 @@ class OverlayState extends State with TickerProviderStateMixin { }); } - void _remove(OverlayEntry entry) { + void _markDirty() { if (mounted) { - setState(() { - _entries.remove(entry); - }); + setState(() {}); } } diff --git a/packages/flutter/test/widgets/navigator_test.dart b/packages/flutter/test/widgets/navigator_test.dart index 597d871d6d..fb11e72893 100644 --- a/packages/flutter/test/widgets/navigator_test.dart +++ b/packages/flutter/test/widgets/navigator_test.dart @@ -521,6 +521,57 @@ void main() { await tester.pumpAndSettle(); }); + testWidgets('Pages update does update overlay correctly', (WidgetTester tester) async { + // Regression Test for https://github.com/flutter/flutter/issues/64941. + List> pages = >[ + MaterialPage( + key: const ValueKey(0), + builder: (BuildContext context) => const Text('page 0'), + ), + MaterialPage( + key: const ValueKey(1), + builder: (BuildContext context) => const Text('page 1'), + ), + ]; + Widget buildNavigator() { + return Navigator( + pages: pages, + onPopPage: (Route route, dynamic result) => false, + ); + } + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: buildNavigator(), + ), + ); + + expect(find.text('page 1'), findsOneWidget); + expect(find.text('page 0'), findsNothing); + + // Removes the first page. + pages = >[ + MaterialPage( + key: const ValueKey(1), + builder: (BuildContext context) => const Text('page 1'), + ), + ]; + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: buildNavigator(), + ), + ); + // Overlay updates correctly. + expect(find.text('page 1'), findsOneWidget); + expect(find.text('page 0'), findsNothing); + + await tester.pumpAndSettle(); + expect(find.text('page 1'), findsOneWidget); + expect(find.text('page 0'), findsNothing); + }); + testWidgets('replaceNamed replaces', (WidgetTester tester) async { final Map routes = { '/' : (BuildContext context) => OnTapPage(id: '/', onTap: () { Navigator.pushReplacementNamed(context, '/A'); }),