modal bottom sheet barrier label (#126431)

Adds barrierLabel as optional param in showModalBottomSheet

Fixes #83180
This commit is contained in:
NikolajHarderNota 2023-05-23 20:59:49 +02:00 committed by GitHub
parent 4341ed4229
commit c84b10474c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 1 deletions

View File

@ -1152,6 +1152,9 @@ class _BottomSheetSuspendedCurve extends ParametricCurve<double> {
/// 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<T?> showModalBottomSheet<T>({
required BuildContext context,
required WidgetBuilder builder,
Color? backgroundColor,
String? barrierLabel,
double? elevation,
ShapeBorder? shape,
Clip? clipBehavior,
@ -1208,7 +1212,7 @@ Future<T?> showModalBottomSheet<T>({
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,

View File

@ -1999,6 +1999,58 @@ void main() {
});
});
group('showModalBottomSheet modalBarrierDismissLabel', () {
testWidgets('Verify that modalBarrierDismissLabel is used if provided',
(WidgetTester tester) async {
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
const String customLabel = 'custom label';
await tester.pumpWidget(MaterialApp(
home: Scaffold(
key: scaffoldKey,
body: const Center(child: Text('body')),
),
));
showModalBottomSheet<void>(
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<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
await tester.pumpWidget(MaterialApp(
home: Scaffold(
key: scaffoldKey,
body: const Center(child: Text('body')),
),
));
showModalBottomSheet<void>(
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 {