Skip to content

Commit

Permalink
barrierColor property in DialogTheme (#142490)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmartos96 authored Feb 9, 2024
1 parent 8b228b9 commit f0bb9d5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
5 changes: 3 additions & 2 deletions packages/flutter/lib/src/material/dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 10 additions & 1 deletion packages/flutter/lib/src/material/dialog_theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class DialogTheme with Diagnosticable {
this.titleTextStyle,
this.contentTextStyle,
this.actionsPadding,
this.barrierColor,
});

/// Overrides the default value for [Dialog.backgroundColor].
Expand Down Expand Up @@ -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({
Expand All @@ -85,6 +89,7 @@ class DialogTheme with Diagnosticable {
TextStyle? titleTextStyle,
TextStyle? contentTextStyle,
EdgeInsetsGeometry? actionsPadding,
Color? barrierColor,
}) {
return DialogTheme(
backgroundColor: backgroundColor ?? this.backgroundColor,
Expand All @@ -97,6 +102,7 @@ class DialogTheme with Diagnosticable {
titleTextStyle: titleTextStyle ?? this.titleTextStyle,
contentTextStyle: contentTextStyle ?? this.contentTextStyle,
actionsPadding: actionsPadding ?? this.actionsPadding,
barrierColor: barrierColor ?? this.barrierColor,
);
}

Expand All @@ -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),
);
}

Expand All @@ -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
Expand All @@ -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));
}
}
15 changes: 15 additions & 0 deletions packages/flutter/test/material/dialog_theme_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)',
]);
});

Expand Down Expand Up @@ -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);
});
}

0 comments on commit f0bb9d5

Please sign in to comment.