diff --git a/packages/flutter/lib/src/material/bottom_sheet.dart b/packages/flutter/lib/src/material/bottom_sheet.dart index f041d1f865..7177a5fc06 100644 --- a/packages/flutter/lib/src/material/bottom_sheet.dart +++ b/packages/flutter/lib/src/material/bottom_sheet.dart @@ -1152,6 +1152,9 @@ class _BottomSheetSuspendedCurve extends ParametricCurve { /// Returns a `Future` that resolves to the value (if any) that was passed to /// [Navigator.pop] when the modal bottom sheet was closed. /// +/// The 'barrierLabel' parameter can be used to set a custom barrierlabel. +/// Will default to modalBarrierDismissLabel of context if not set. +/// /// {@tool dartpad} /// This example demonstrates how to use [showModalBottomSheet] to display a /// bottom sheet that obscures the content behind it when a user taps a button. @@ -1184,6 +1187,7 @@ Future showModalBottomSheet({ required BuildContext context, required WidgetBuilder builder, Color? backgroundColor, + String? barrierLabel, double? elevation, ShapeBorder? shape, Clip? clipBehavior, @@ -1208,7 +1212,7 @@ Future showModalBottomSheet({ builder: builder, capturedThemes: InheritedTheme.capture(from: context, to: navigator.context), isScrollControlled: isScrollControlled, - barrierLabel: localizations.scrimLabel, + barrierLabel: barrierLabel ?? localizations.scrimLabel, barrierOnTapHint: localizations.scrimOnTapHint(localizations.bottomSheetLabel), backgroundColor: backgroundColor, elevation: elevation, diff --git a/packages/flutter/test/material/bottom_sheet_test.dart b/packages/flutter/test/material/bottom_sheet_test.dart index 8a002286d7..3789255283 100644 --- a/packages/flutter/test/material/bottom_sheet_test.dart +++ b/packages/flutter/test/material/bottom_sheet_test.dart @@ -1999,6 +1999,58 @@ void main() { }); }); + + group('showModalBottomSheet modalBarrierDismissLabel', () { + testWidgets('Verify that modalBarrierDismissLabel is used if provided', + (WidgetTester tester) async { + final GlobalKey scaffoldKey = GlobalKey(); + const String customLabel = 'custom label'; + await tester.pumpWidget(MaterialApp( + home: Scaffold( + key: scaffoldKey, + body: const Center(child: Text('body')), + ), + )); + + showModalBottomSheet( + barrierLabel: 'custom label', + context: scaffoldKey.currentContext!, + builder: (BuildContext context) { + return const Text('BottomSheet'); + }, + ); + await tester.pump(); + await tester.pump(const Duration(seconds: 1)); + + final ModalBarrier modalBarrier = + tester.widget(find.byType(ModalBarrier).last); + expect(modalBarrier.semanticsLabel, customLabel); + }); + + testWidgets('Verify that modalBarrierDismissLabel from context is used if barrierLabel is not provided', + (WidgetTester tester) async { + final GlobalKey scaffoldKey = GlobalKey(); + await tester.pumpWidget(MaterialApp( + home: Scaffold( + key: scaffoldKey, + body: const Center(child: Text('body')), + ), + )); + + showModalBottomSheet( + context: scaffoldKey.currentContext!, + builder: (BuildContext context) { + return const Text('BottomSheet'); + }, + ); + await tester.pump(); + await tester.pump(const Duration(seconds: 1)); + + final ModalBarrier modalBarrier = + tester.widget(find.byType(ModalBarrier).last); + expect(modalBarrier.semanticsLabel, MaterialLocalizations.of(scaffoldKey.currentContext!).scrimLabel); + }); + }); } class _TestPage extends StatelessWidget {