diff --git a/packages/flutter/lib/src/material/scaffold.dart b/packages/flutter/lib/src/material/scaffold.dart index 41e753e918..4ad7ef7e4a 100644 --- a/packages/flutter/lib/src/material/scaffold.dart +++ b/packages/flutter/lib/src/material/scaffold.dart @@ -2360,7 +2360,7 @@ class ScaffoldState extends State with TickerProviderStateMixin, Resto final LocalHistoryEntry? entry = isPersistent ? null : LocalHistoryEntry(onRemove: () { - if (!removedEntry) { + if (!removedEntry && _currentBottomSheet?._widget == bottomSheet) { _removeCurrentBottomSheet(); } }); diff --git a/packages/flutter/test/material/bottom_sheet_test.dart b/packages/flutter/test/material/bottom_sheet_test.dart index 0c8a2b1557..c70848ea4a 100644 --- a/packages/flutter/test/material/bottom_sheet_test.dart +++ b/packages/flutter/test/material/bottom_sheet_test.dart @@ -1204,6 +1204,58 @@ void main() { expect(tester.takeException(), isNull); }); + testWidgets('Calling PersistentBottomSheetController.close does not crash when it is not the current bottom sheet', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/93717 + PersistentBottomSheetController? sheetController1; + await tester.pumpWidget(MaterialApp( + home: Scaffold( + body: Builder(builder: (BuildContext context) { + return SafeArea( + child: Column( + children: [ + ElevatedButton( + child: const Text('show 1'), + onPressed: () { + sheetController1 = Scaffold.of(context).showBottomSheet( + (BuildContext context) => const Text('BottomSheet 1'), + ); + }, + ), + ElevatedButton( + child: const Text('show 2'), + onPressed: () { + Scaffold.of(context).showBottomSheet( + (BuildContext context) => const Text('BottomSheet 2'), + ); + }, + ), + ElevatedButton( + child: const Text('close 1'), + onPressed: (){ + sheetController1!.close(); + }, + ), + ], + ), + ); + }), + ), + )); + + await tester.tap(find.text('show 1')); + await tester.pumpAndSettle(); + expect(find.text('BottomSheet 1'), findsOneWidget); + + await tester.tap(find.text('show 2')); + await tester.pumpAndSettle(); + expect(find.text('BottomSheet 2'), findsOneWidget); + + // This will throw an assertion if regressed + await tester.tap(find.text('close 1')); + await tester.pumpAndSettle(); + expect(find.text('BottomSheet 2'), findsOneWidget); + }); + group('constraints', () { testWidgets('No constraints by default for bottomSheet property', (WidgetTester tester) async {