diff --git a/packages/flutter/lib/src/cupertino/sheet.dart b/packages/flutter/lib/src/cupertino/sheet.dart index 474d4b3c6b4..117ab97ad4e 100644 --- a/packages/flutter/lib/src/cupertino/sheet.dart +++ b/packages/flutter/lib/src/cupertino/sheet.dart @@ -894,8 +894,7 @@ class _CupertinoDragGestureController { } else { if (isCurrent) { // This route is destined to pop at this point. Reuse navigator's pop. - final NavigatorState rootNavigator = Navigator.of(navigator.context, rootNavigator: true); - rootNavigator.pop(); + navigator.pop(); } if (popDragController.isAnimating) { diff --git a/packages/flutter/test/cupertino/sheet_test.dart b/packages/flutter/test/cupertino/sheet_test.dart index 081022503a9..73c53fe6272 100644 --- a/packages/flutter/test/cupertino/sheet_test.dart +++ b/packages/flutter/test/cupertino/sheet_test.dart @@ -1141,6 +1141,94 @@ void main() { await tester.pumpAndSettle(); }); + testWidgets('drag dismiss uses route navigator instead of root navigator', ( + WidgetTester tester, + ) async { + final GlobalKey homeKey = GlobalKey(); + final GlobalKey nestedNavigatorKey = GlobalKey(); + final GlobalKey sheetKey = GlobalKey(); + bool wasPopped = false; + bool rootNavigatorPopped = false; + + await tester.pumpWidget( + CupertinoApp( + home: PopScope( + onPopInvokedWithResult: (bool didPop, Object? result) { + if (didPop) { + rootNavigatorPopped = true; + } + }, + child: CupertinoPageScaffold( + key: homeKey, + child: Navigator( + key: nestedNavigatorKey, + onGenerateRoute: (RouteSettings settings) { + return CupertinoPageRoute( + settings: settings, + builder: (BuildContext context) { + return Center( + child: Column( + children: [ + const Text('Page 1'), + CupertinoButton( + onPressed: () { + Navigator.push( + context, + CupertinoSheetRoute( + builder: (BuildContext context) { + return PopScope( + onPopInvokedWithResult: (bool didPop, Object? result) { + if (didPop) { + wasPopped = true; + } + }, + child: CupertinoPageScaffold( + key: sheetKey, + child: const Center(child: Text('Page 2')), + ), + ); + }, + ), + ); + }, + child: const Text('Push Page 2'), + ), + ], + ), + ); + }, + ); + }, + ), + ), + ), + ), + ); + + await tester.tap(find.text('Push Page 2')); + await tester.pumpAndSettle(); + + expect(find.text('Page 2'), findsOneWidget); + expect(wasPopped, false); + expect(rootNavigatorPopped, false); + + // Start drag gesture and drag down far enough to trigger dismissal + final TestGesture gesture = await tester.startGesture(const Offset(100, 200)); + await gesture.moveBy(const Offset(0, 350)); + await tester.pump(); + + await gesture.up(); + await tester.pumpAndSettle(); + + // Verify the sheet was dismissed and the PopScope callback was triggered + expect(find.text('Page 2'), findsNothing); + expect(find.text('Page 1'), findsOneWidget); + // Verify that the nested navigator was used (sheet PopScope triggered) + // but the root navigator was NOT used (root PopScope not triggered) + expect(wasPopped, true); + expect(rootNavigatorPopped, false); + }); + testWidgets('dragging does not move the sheet when enableDrag is false', ( WidgetTester tester, ) async {