barrierColor property in DialogTheme (#142490)

This commit is contained in:
David Martos 2024-02-09 10:50:14 +01:00 committed by GitHub
parent 8b228b9eda
commit f0bb9d57e6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 28 additions and 3 deletions

View file

@ -1327,7 +1327,8 @@ Widget _buildMaterialDialogTransitions(BuildContext context, Animation<double> a
/// barrier will dismiss the dialog. It is `true` by default and can not be `null`.
///
/// The `barrierColor` argument is used to specify the color of the modal
/// barrier that darkens everything below the dialog. If `null` the default color
/// barrier that darkens everything below the dialog. If `null` the `barrierColor`
/// field from `DialogTheme` is used. If that is `null` the default color
/// `Colors.black54` is used.
///
/// The `useSafeArea` argument is used to indicate if the dialog should only
@ -1428,7 +1429,7 @@ Future<T?> showDialog<T>({
return Navigator.of(context, rootNavigator: useRootNavigator).push<T>(DialogRoute<T>(
context: context,
builder: builder,
barrierColor: barrierColor ?? Colors.black54,
barrierColor: barrierColor ?? Theme.of(context).dialogTheme.barrierColor ?? Colors.black54,
barrierDismissible: barrierDismissible,
barrierLabel: barrierLabel,
useSafeArea: useSafeArea,

View file

@ -38,6 +38,7 @@ class DialogTheme with Diagnosticable {
this.titleTextStyle,
this.contentTextStyle,
this.actionsPadding,
this.barrierColor,
});
/// Overrides the default value for [Dialog.backgroundColor].
@ -72,6 +73,9 @@ class DialogTheme with Diagnosticable {
/// Used to configure the [IconTheme] for the [AlertDialog.icon] widget.
final Color? iconColor;
/// Overrides the default value for [barrierColor] in [showDialog].
final Color? barrierColor;
/// Creates a copy of this object but with the given fields replaced with the
/// new values.
DialogTheme copyWith({
@ -85,6 +89,7 @@ class DialogTheme with Diagnosticable {
TextStyle? titleTextStyle,
TextStyle? contentTextStyle,
EdgeInsetsGeometry? actionsPadding,
Color? barrierColor,
}) {
return DialogTheme(
backgroundColor: backgroundColor ?? this.backgroundColor,
@ -97,6 +102,7 @@ class DialogTheme with Diagnosticable {
titleTextStyle: titleTextStyle ?? this.titleTextStyle,
contentTextStyle: contentTextStyle ?? this.contentTextStyle,
actionsPadding: actionsPadding ?? this.actionsPadding,
barrierColor: barrierColor ?? this.barrierColor,
);
}
@ -123,6 +129,7 @@ class DialogTheme with Diagnosticable {
titleTextStyle: TextStyle.lerp(a?.titleTextStyle, b?.titleTextStyle, t),
contentTextStyle: TextStyle.lerp(a?.contentTextStyle, b?.contentTextStyle, t),
actionsPadding: EdgeInsetsGeometry.lerp(a?.actionsPadding, b?.actionsPadding, t),
barrierColor: Color.lerp(a?.barrierColor, b?.barrierColor, t),
);
}
@ -147,7 +154,8 @@ class DialogTheme with Diagnosticable {
&& other.iconColor == iconColor
&& other.titleTextStyle == titleTextStyle
&& other.contentTextStyle == contentTextStyle
&& other.actionsPadding == actionsPadding;
&& other.actionsPadding == actionsPadding
&& other.barrierColor == barrierColor;
}
@override
@ -163,5 +171,6 @@ class DialogTheme with Diagnosticable {
properties.add(DiagnosticsProperty<TextStyle>('titleTextStyle', titleTextStyle, defaultValue: null));
properties.add(DiagnosticsProperty<TextStyle>('contentTextStyle', contentTextStyle, defaultValue: null));
properties.add(DiagnosticsProperty<EdgeInsetsGeometry>('actionsPadding', actionsPadding, defaultValue: null));
properties.add(ColorProperty('barrierColor', barrierColor));
}
}

View file

@ -66,6 +66,7 @@ void main() {
titleTextStyle: TextStyle(color: Color(0xffffffff)),
contentTextStyle: TextStyle(color: Color(0xff000000)),
actionsPadding: EdgeInsets.all(8.0),
barrierColor: Color(0xff000005),
).debugFillProperties(builder);
final List<String> description = builder.properties
.where((DiagnosticsNode n) => !n.isFiltered(DiagnosticLevel.info))
@ -80,6 +81,7 @@ void main() {
'titleTextStyle: TextStyle(inherit: true, color: Color(0xffffffff))',
'contentTextStyle: TextStyle(inherit: true, color: Color(0xff000000))',
'actionsPadding: EdgeInsets.all(8.0)',
'barrierColor: Color(0xff000005)',
]);
});
@ -499,4 +501,17 @@ void main() {
final RenderParagraph content = _getTextRenderObject(tester, contentText);
expect(content.text.style!.color, contentTextStyle.color);
});
testWidgets('Custom barrierColor - Theme', (WidgetTester tester) async {
const Color barrierColor = Colors.blue;
const SimpleDialog dialog = SimpleDialog();
final ThemeData theme = ThemeData(dialogTheme: const DialogTheme(barrierColor: barrierColor));
await tester.pumpWidget(_appWithDialog(tester, dialog, theme: theme));
await tester.tap(find.text('X'));
await tester.pumpAndSettle();
final ModalBarrier modalBarrier = tester.widget(find.byType(ModalBarrier).last);
expect(modalBarrier.color, barrierColor);
});
}