diff --git a/packages/flutter/lib/src/material/dialog.dart b/packages/flutter/lib/src/material/dialog.dart index 5fa183d20aba..24f8c8d462b3 100644 --- a/packages/flutter/lib/src/material/dialog.dart +++ b/packages/flutter/lib/src/material/dialog.dart @@ -1327,7 +1327,8 @@ Widget _buildMaterialDialogTransitions(BuildContext context, Animation 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 showDialog({ return Navigator.of(context, rootNavigator: useRootNavigator).push(DialogRoute( context: context, builder: builder, - barrierColor: barrierColor ?? Colors.black54, + barrierColor: barrierColor ?? Theme.of(context).dialogTheme.barrierColor ?? Colors.black54, barrierDismissible: barrierDismissible, barrierLabel: barrierLabel, useSafeArea: useSafeArea, diff --git a/packages/flutter/lib/src/material/dialog_theme.dart b/packages/flutter/lib/src/material/dialog_theme.dart index f27728418b64..ba5fc19ad61a 100644 --- a/packages/flutter/lib/src/material/dialog_theme.dart +++ b/packages/flutter/lib/src/material/dialog_theme.dart @@ -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('titleTextStyle', titleTextStyle, defaultValue: null)); properties.add(DiagnosticsProperty('contentTextStyle', contentTextStyle, defaultValue: null)); properties.add(DiagnosticsProperty('actionsPadding', actionsPadding, defaultValue: null)); + properties.add(ColorProperty('barrierColor', barrierColor)); } } diff --git a/packages/flutter/test/material/dialog_theme_test.dart b/packages/flutter/test/material/dialog_theme_test.dart index 0d41077d0c87..66ed2cf0efb7 100644 --- a/packages/flutter/test/material/dialog_theme_test.dart +++ b/packages/flutter/test/material/dialog_theme_test.dart @@ -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 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); + }); }