From 8600eb129c13fd76c1acf3187af0e417305c3417 Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Fri, 9 Jun 2023 10:06:49 +0300 Subject: [PATCH] Fix `showBottomSheet` doesn't remove scrim when draggable sheet is dismissed (#128455) fixes https://github.com/flutter/flutter/issues/128367
code sample ```dart import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, theme: ThemeData(useMaterial3: true), home: Scaffold( floatingActionButton: Builder( builder: (BuildContext context) => FloatingActionButton( child: const Icon(Icons.add), onPressed: () { Scaffold.of(context).showBottomSheet( (_) { return DraggableScrollableSheet( expand: false, builder: (_, ScrollController scrollController) => ListView.builder( controller: scrollController, itemCount: 25, itemBuilder: (BuildContext context, int index) { return ListTile( title: Text('Item $index - tap to close'), onTap: () { Navigator.of(context).pop(); }, ); }, ), ); }, ); }, ), ), ), ); } } ```
### Before https://github.com/flutter/flutter/assets/48603081/fa87feb9-54f2-4e50-bf71-c81d9e54ff61 ### After https://github.com/flutter/flutter/assets/48603081/7d192059-7600-4d65-ae84-6321f3598133 --- .../flutter/lib/src/material/scaffold.dart | 2 + .../flutter/test/material/scaffold_test.dart | 46 +++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/packages/flutter/lib/src/material/scaffold.dart b/packages/flutter/lib/src/material/scaffold.dart index d9d239c529..7d40251848 100644 --- a/packages/flutter/lib/src/material/scaffold.dart +++ b/packages/flutter/lib/src/material/scaffold.dart @@ -2312,6 +2312,8 @@ class ScaffoldState extends State with TickerProviderStateMixin, Resto bottomSheetKey.currentState!.close(); setState(() { + _showBodyScrim = false; + _bodyScrimColor = Colors.black.withOpacity(0.0); _currentBottomSheet = null; }); diff --git a/packages/flutter/test/material/scaffold_test.dart b/packages/flutter/test/material/scaffold_test.dart index f4dd934ec5..acba212853 100644 --- a/packages/flutter/test/material/scaffold_test.dart +++ b/packages/flutter/test/material/scaffold_test.dart @@ -2754,6 +2754,52 @@ void main() { expect(tester.takeException(), isNull); }); + + testWidgets('showBottomSheet removes scrim when draggable sheet is dismissed', (WidgetTester tester) async { + final DraggableScrollableController draggableController = DraggableScrollableController(); + final GlobalKey scaffoldKey = GlobalKey(); + PersistentBottomSheetController? sheetController; + + await tester.pumpWidget(MaterialApp( + home: Scaffold( + key: scaffoldKey, + body: const Center(child: Text('body')), + ), + )); + + sheetController = scaffoldKey.currentState!.showBottomSheet((_) { + return DraggableScrollableSheet( + expand: false, + controller: draggableController, + builder: (BuildContext context, ScrollController scrollController) { + return SingleChildScrollView( + controller: scrollController, + child: const Placeholder(), + ); + }, + ); + }); + + Finder findModalBarrier() => find.descendant(of: find.byType(Scaffold), matching: find.byType(ModalBarrier)); + + await tester.pump(); + expect(find.byType(BottomSheet), findsOneWidget); + + // The scrim is not present yet. + expect(findModalBarrier(), findsNothing); + + // Expand the sheet to 80% of parent height to show the scrim. + draggableController.jumpTo(0.8); + await tester.pump(); + expect(findModalBarrier(), findsOneWidget); + + // Dismiss the sheet. + sheetController.close(); + await tester.pumpAndSettle(); + + // The scrim should be gone. + expect(findModalBarrier(), findsNothing); + }); } class _GeometryListener extends StatefulWidget {