diff --git a/packages/flutter/lib/src/cupertino/app.dart b/packages/flutter/lib/src/cupertino/app.dart index f68ffc92bc..78ef191ea2 100644 --- a/packages/flutter/lib/src/cupertino/app.dart +++ b/packages/flutter/lib/src/cupertino/app.dart @@ -547,6 +547,7 @@ class _CupertinoAppState extends State { routerDelegate: widget.routerDelegate, routerConfig: widget.routerConfig, backButtonDispatcher: widget.backButtonDispatcher, + onNavigationNotification: widget.onNavigationNotification, builder: widget.builder, title: widget.title, onGenerateTitle: widget.onGenerateTitle, diff --git a/packages/flutter/lib/src/material/app.dart b/packages/flutter/lib/src/material/app.dart index 9bcb7174e9..6560e015e3 100644 --- a/packages/flutter/lib/src/material/app.dart +++ b/packages/flutter/lib/src/material/app.dart @@ -1016,6 +1016,7 @@ class _MaterialAppState extends State { routerDelegate: widget.routerDelegate, routerConfig: widget.routerConfig, backButtonDispatcher: widget.backButtonDispatcher, + onNavigationNotification: widget.onNavigationNotification, builder: _materialBuilder, title: widget.title, onGenerateTitle: widget.onGenerateTitle, diff --git a/packages/flutter/lib/src/widgets/app.dart b/packages/flutter/lib/src/widgets/app.dart index 3dd610c8ef..db31a2654a 100644 --- a/packages/flutter/lib/src/widgets/app.dart +++ b/packages/flutter/lib/src/widgets/app.dart @@ -706,7 +706,12 @@ class WidgetsApp extends StatefulWidget { /// {@template flutter.widgets.widgetsApp.onNavigationNotification} /// The callback to use when receiving a [NavigationNotification]. /// - /// By default this updates the engine with the navigation status. + /// By default this updates the engine with the navigation status and stops + /// bubbling the notification. + /// + /// See also: + /// + /// * [NotificationListener.onNotification], which uses this callback. /// {@endtemplate} final NotificationListenerCallback? onNavigationNotification; diff --git a/packages/flutter/test/cupertino/app_test.dart b/packages/flutter/test/cupertino/app_test.dart index 2a77e40a84..3da1c08ac7 100644 --- a/packages/flutter/test/cupertino/app_test.dart +++ b/packages/flutter/test/cupertino/app_test.dart @@ -179,6 +179,52 @@ void main() { expect(find.text('popped'), findsOneWidget); }); + testWidgets('CupertinoApp.router works with onNavigationNotification', (WidgetTester tester) async { + // This is a regression test for https://github.com/flutter/flutter/issues/139903. + final PlatformRouteInformationProvider provider = PlatformRouteInformationProvider( + initialRouteInformation: RouteInformation( + uri: Uri.parse('initial'), + ), + ); + addTearDown(provider.dispose); + final SimpleNavigatorRouterDelegate delegate = SimpleNavigatorRouterDelegate( + builder: (BuildContext context, RouteInformation information) { + return Text(information.uri.toString()); + }, + onPopPage: (Route route, void result, SimpleNavigatorRouterDelegate delegate) { + delegate.routeInformation = RouteInformation( + uri: Uri.parse('popped'), + ); + return route.didPop(result); + }, + ); + addTearDown(delegate.dispose); + + int navigationCount = 0; + + await tester.pumpWidget(CupertinoApp.router( + routeInformationProvider: provider, + routeInformationParser: SimpleRouteInformationParser(), + routerDelegate: delegate, + onNavigationNotification: (NavigationNotification? notification) { + navigationCount += 1; + return true; + }, + )); + expect(find.text('initial'), findsOneWidget); + + expect(navigationCount, greaterThan(0)); + final int navigationCountAfterBuild = navigationCount; + + // Simulate android back button intent. + final ByteData message = const JSONMethodCodec().encodeMethodCall(const MethodCall('popRoute')); + await tester.binding.defaultBinaryMessenger.handlePlatformMessage('flutter/navigation', message, (_) { }); + await tester.pumpAndSettle(); + expect(find.text('popped'), findsOneWidget); + + expect(navigationCount, greaterThan(navigationCountAfterBuild)); + }); + testWidgets('CupertinoApp.router route information parser is optional', (WidgetTester tester) async { final SimpleNavigatorRouterDelegate delegate = SimpleNavigatorRouterDelegate( builder: (BuildContext context, RouteInformation information) { diff --git a/packages/flutter/test/material/app_test.dart b/packages/flutter/test/material/app_test.dart index e5824b8132..4cae541dd0 100644 --- a/packages/flutter/test/material/app_test.dart +++ b/packages/flutter/test/material/app_test.dart @@ -1118,6 +1118,52 @@ void main() { expect(find.text('popped'), findsOneWidget); }); + testWidgets('MaterialApp.router works with onNavigationNotification', (WidgetTester tester) async { + // This is a regression test for https://github.com/flutter/flutter/issues/139903. + final PlatformRouteInformationProvider provider = PlatformRouteInformationProvider( + initialRouteInformation: RouteInformation( + uri: Uri.parse('initial'), + ), + ); + addTearDown(provider.dispose); + final SimpleNavigatorRouterDelegate delegate = SimpleNavigatorRouterDelegate( + builder: (BuildContext context, RouteInformation information) { + return Text(information.uri.toString()); + }, + onPopPage: (Route route, void result, SimpleNavigatorRouterDelegate delegate) { + delegate.routeInformation = RouteInformation( + uri: Uri.parse('popped'), + ); + return route.didPop(result); + }, + ); + addTearDown(delegate.dispose); + + int navigationCount = 0; + + await tester.pumpWidget(MaterialApp.router( + routeInformationProvider: provider, + routeInformationParser: SimpleRouteInformationParser(), + routerDelegate: delegate, + onNavigationNotification: (NavigationNotification? notification) { + navigationCount += 1; + return true; + }, + )); + expect(find.text('initial'), findsOneWidget); + + expect(navigationCount, greaterThan(0)); + final int navigationCountAfterBuild = navigationCount; + + // Simulate android back button intent. + final ByteData message = const JSONMethodCodec().encodeMethodCall(const MethodCall('popRoute')); + await tester.binding.defaultBinaryMessenger.handlePlatformMessage('flutter/navigation', message, (_) { }); + await tester.pumpAndSettle(); + expect(find.text('popped'), findsOneWidget); + + expect(navigationCount, greaterThan(navigationCountAfterBuild)); + }); + testWidgets('MaterialApp.router route information parser is optional', (WidgetTester tester) async { final SimpleNavigatorRouterDelegate delegate = SimpleNavigatorRouterDelegate( builder: (BuildContext context, RouteInformation information) {