diff --git a/packages/flutter/lib/src/widgets/navigator.dart b/packages/flutter/lib/src/widgets/navigator.dart index 3169a18373..3716c9dc40 100644 --- a/packages/flutter/lib/src/widgets/navigator.dart +++ b/packages/flutter/lib/src/widgets/navigator.dart @@ -3613,7 +3613,7 @@ class NavigatorState extends State with TickerProviderStateMixin, Res void _handleHistoryChanged() { final bool navigatorCanPop = canPop(); - late final bool routeBlocksPop; + final bool routeBlocksPop; if (!navigatorCanPop) { final _RouteEntry? lastEntry = _lastRouteEntryWhereOrNull(_RouteEntry.isPresentPredicate); routeBlocksPop = lastEntry != null diff --git a/packages/flutter/lib/src/widgets/routes.dart b/packages/flutter/lib/src/widgets/routes.dart index 8fdb6671a9..8205542995 100644 --- a/packages/flutter/lib/src/widgets/routes.dart +++ b/packages/flutter/lib/src/widgets/routes.dart @@ -1807,6 +1807,9 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute extends TransitionRoute with LocalHistoryRoute extends TransitionRoute with LocalHistoryRoute notifications = []; + await tester.pumpWidget( + NotificationListener( + onNotification: (NavigationNotification notification) { + notifications.add(notification); + return true; + }, + child: Directionality( + textDirection: TextDirection.ltr, + child: Navigator( + initialRoute: '/', + onGenerateRoute: (RouteSettings settings) { + return MaterialPageRoute( + builder: (BuildContext context) { + return const SizedBox.shrink(); + }, + settings: settings, + ); + }, + ), + ), + ), + ); + + // Only one notification, from the initial route, where a pop can't be + // handled because there's no other route to pop. + expect(notifications, hasLength(1)); + expect(notifications.first.canHandlePop, isFalse); + }); + + testWidgets('with WillPopScope', (WidgetTester tester) async { + final List notifications = []; + await tester.pumpWidget( + NotificationListener( + onNotification: (NavigationNotification notification) { + notifications.add(notification); + return true; + }, + child: Directionality( + textDirection: TextDirection.ltr, + child: Navigator( + initialRoute: '/', + onGenerateRoute: (RouteSettings settings) { + return MaterialPageRoute( + builder: (BuildContext context) { + return WillPopScope( + onWillPop: () { + return Future.value(false); + }, + child: const SizedBox.shrink(), + ); + }, + settings: settings, + ); + }, + ), + ), + ), + ); + + // Two notifications. The first is from the initial route, where a pop + // can't be handled because it's the only route. The second is from + // registering the WillPopScope, where it will always want to receive + // pops. + expect(notifications, hasLength(2)); + expect(notifications.first.canHandlePop, isFalse); + expect(notifications.last.canHandlePop, isTrue); + }); + }); } double _getOpacity(GlobalKey key, WidgetTester tester) {