Skip to content

Commit 6dd929a

Browse files
authored
Normalize Dialog theme (flutter#153982)
This PR is to make preparations to make `DialogTheme` conform to Flutter's conventions for component themes: * Added a `DialogThemeData` class which defines overrides for the defaults for `Dialog` properties. * Added 2 `DialogTheme` constructor parameters: `DialogThemeData? data` and `Widget? child`. This is now the preferred way to configure a `DialogTheme`: ``` DialogTheme( data: DialogThemeData(color: xxx, elevation: xxx, ...), child: Dialog(...) ) ``` These two properties are made nullable to not break existing apps which has customized `ThemeData.dialogTheme`. * Changed the type of theme defaults from `DialogTheme` to `DialogThemeData`. TODO: * Fix internal failures. * Change the type of `ThemeData.dialogTheme` from `DialogTheme` to `DialogThemeData`. This may cause breaking changes, a migration guide will be created. Addresses the "theme normalization" sub project within flutter#91772
1 parent 4d17998 commit 6dd929a

File tree

4 files changed

+487
-64
lines changed

4 files changed

+487
-64
lines changed

dev/tools/gen_defaults/lib/dialog_template.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class DialogTemplate extends TokenTemplate {
1212

1313
@override
1414
String generate() => '''
15-
class _${blockName}DefaultsM3 extends DialogTheme {
15+
class _${blockName}DefaultsM3 extends DialogThemeData {
1616
_${blockName}DefaultsM3(this.context)
1717
: super(
1818
alignment: Alignment.center,
@@ -54,7 +54,7 @@ class DialogFullscreenTemplate extends TokenTemplate {
5454

5555
@override
5656
String generate() => '''
57-
class _${blockName}DefaultsM3 extends DialogTheme {
57+
class _${blockName}DefaultsM3 extends DialogThemeData {
5858
const _${blockName}DefaultsM3(this.context): super(clipBehavior: Clip.none);
5959
6060
final BuildContext context;

packages/flutter/lib/src/material/dialog.dart

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class Dialog extends StatelessWidget {
108108
/// and a surface tint overlay on the background color if [surfaceTintColor] is
109109
/// non null.
110110
///
111-
/// If null then [DialogTheme.elevation] is used, and if that is null then
111+
/// If null then [DialogThemeData.elevation] is used, and if that is null then
112112
/// the elevation will match the Material Design specification for Dialogs.
113113
///
114114
/// See also:
@@ -197,7 +197,7 @@ class Dialog extends StatelessWidget {
197197
/// See the enum [Clip] for details of all possible options and their common
198198
/// use cases.
199199
///
200-
/// If null, then [DialogTheme.clipBehavior] is used. If that is also null,
200+
/// If null, then [DialogThemeData.clipBehavior] is used. If that is also null,
201201
/// defaults to [Clip.none].
202202
/// {@endtemplate}
203203
final Clip? clipBehavior;
@@ -214,7 +214,7 @@ class Dialog extends StatelessWidget {
214214
/// {@template flutter.material.dialog.alignment}
215215
/// How to align the [Dialog].
216216
///
217-
/// If null, then [DialogTheme.alignment] is used. If that is also null, the
217+
/// If null, then [DialogThemeData.alignment] is used. If that is also null, the
218218
/// default is [Alignment.center].
219219
/// {@endtemplate}
220220
final AlignmentGeometry? alignment;
@@ -230,10 +230,10 @@ class Dialog extends StatelessWidget {
230230
@override
231231
Widget build(BuildContext context) {
232232
final ThemeData theme = Theme.of(context);
233-
final DialogTheme dialogTheme = DialogTheme.of(context);
233+
final DialogThemeData dialogTheme = DialogTheme.of(context).data;
234234
final EdgeInsets effectivePadding = MediaQuery.viewInsetsOf(context)
235235
+ (insetPadding ?? dialogTheme.insetPadding ?? _defaultInsetPadding);
236-
final DialogTheme defaults = theme.useMaterial3
236+
final DialogThemeData defaults = theme.useMaterial3
237237
? (_fullscreen ? _DialogFullscreenDefaultsM3(context) : _DialogDefaultsM3(context))
238238
: _DialogDefaultsM2(context);
239239

@@ -481,7 +481,7 @@ class AlertDialog extends StatelessWidget {
481481

482482
/// Color for the [Icon] in the [icon] of this [AlertDialog].
483483
///
484-
/// If null, [DialogTheme.iconColor] is used. If that is null, defaults to
484+
/// If null, [DialogThemeData.iconColor] is used. If that is null, defaults to
485485
/// color scheme's [ColorScheme.secondary] if [ThemeData.useMaterial3] is
486486
/// true, black otherwise.
487487
final Color? iconColor;
@@ -519,7 +519,7 @@ class AlertDialog extends StatelessWidget {
519519

520520
/// Style for the text in the [title] of this [AlertDialog].
521521
///
522-
/// If null, [DialogTheme.titleTextStyle] is used. If that's null, defaults to
522+
/// If null, [DialogThemeData.titleTextStyle] is used. If that's null, defaults to
523523
/// [TextTheme.headlineSmall] of [ThemeData.textTheme] if
524524
/// [ThemeData.useMaterial3] is true, [TextTheme.titleLarge] otherwise.
525525
final TextStyle? titleTextStyle;
@@ -554,7 +554,7 @@ class AlertDialog extends StatelessWidget {
554554

555555
/// Style for the text in the [content] of this [AlertDialog].
556556
///
557-
/// If null, [DialogTheme.contentTextStyle] is used. If that's null, defaults
557+
/// If null, [DialogThemeData.contentTextStyle] is used. If that's null, defaults
558558
/// to [TextTheme.bodyMedium] of [ThemeData.textTheme] if
559559
/// [ThemeData.useMaterial3] is true, [TextTheme.titleMedium] otherwise.
560560
final TextStyle? contentTextStyle;
@@ -720,8 +720,8 @@ class AlertDialog extends StatelessWidget {
720720
assert(debugCheckHasMaterialLocalizations(context));
721721
final ThemeData theme = Theme.of(context);
722722

723-
final DialogTheme dialogTheme = DialogTheme.of(context);
724-
final DialogTheme defaults = theme.useMaterial3 ? _DialogDefaultsM3(context) : _DialogDefaultsM2(context);
723+
final DialogThemeData dialogTheme = DialogTheme.of(context).data;
724+
final DialogThemeData defaults = theme.useMaterial3 ? _DialogDefaultsM3(context) : _DialogDefaultsM2(context);
725725

726726
String? label = semanticLabel;
727727
switch (theme.platform) {
@@ -1155,7 +1155,7 @@ class SimpleDialog extends StatelessWidget {
11551155

11561156
/// Style for the text in the [title] of this [SimpleDialog].
11571157
///
1158-
/// If null, [DialogTheme.titleTextStyle] is used. If that's null, defaults to
1158+
/// If null, [DialogThemeData.titleTextStyle] is used. If that's null, defaults to
11591159
/// [TextTheme.titleLarge] of [ThemeData.textTheme].
11601160
final TextStyle? titleTextStyle;
11611161

@@ -1235,7 +1235,7 @@ class SimpleDialog extends StatelessWidget {
12351235

12361236
// The paddingScaleFactor is used to adjust the padding of Dialog
12371237
// children.
1238-
final TextStyle defaultTextStyle = titleTextStyle ?? DialogTheme.of(context).titleTextStyle ?? theme.textTheme.titleLarge!;
1238+
final TextStyle defaultTextStyle = titleTextStyle ?? DialogTheme.of(context).data.titleTextStyle ?? theme.textTheme.titleLarge!;
12391239
final double fontSize = defaultTextStyle.fontSize ?? kDefaultFontSize;
12401240
final double fontSizeToScale = fontSize == 0.0 ? kDefaultFontSize : fontSize;
12411241
final double effectiveTextScale = MediaQuery.textScalerOf(context).scale(fontSizeToScale) / fontSizeToScale;
@@ -1342,7 +1342,7 @@ Widget _buildMaterialDialogTransitions(BuildContext context, Animation<double> a
13421342
///
13431343
/// The `barrierColor` argument is used to specify the color of the modal
13441344
/// barrier that darkens everything below the dialog. If `null` the `barrierColor`
1345-
/// field from `DialogTheme` is used. If that is `null` the default color
1345+
/// field from `DialogThemeData` is used. If that is `null` the default color
13461346
/// `Colors.black54` is used.
13471347
///
13481348
/// The `useSafeArea` argument is used to indicate if the dialog should only
@@ -1443,7 +1443,10 @@ Future<T?> showDialog<T>({
14431443
return Navigator.of(context, rootNavigator: useRootNavigator).push<T>(DialogRoute<T>(
14441444
context: context,
14451445
builder: builder,
1446-
barrierColor: barrierColor ?? Theme.of(context).dialogTheme.barrierColor ?? Colors.black54,
1446+
barrierColor: barrierColor
1447+
?? DialogTheme.of(context).barrierColor
1448+
?? Theme.of(context).dialogTheme.data.barrierColor
1449+
?? Colors.black54,
14471450
barrierDismissible: barrierDismissible,
14481451
barrierLabel: barrierLabel,
14491452
useSafeArea: useSafeArea,
@@ -1633,7 +1636,7 @@ double _scalePadding(double textScaleFactor) {
16331636
}
16341637

16351638
// Hand coded defaults based on Material Design 2.
1636-
class _DialogDefaultsM2 extends DialogTheme {
1639+
class _DialogDefaultsM2 extends DialogThemeData {
16371640
_DialogDefaultsM2(this.context)
16381641
: _textTheme = Theme.of(context).textTheme,
16391642
_iconTheme = Theme.of(context).iconTheme,
@@ -1674,7 +1677,7 @@ class _DialogDefaultsM2 extends DialogTheme {
16741677
// Design token database by the script:
16751678
// dev/tools/gen_defaults/bin/gen_defaults.dart.
16761679

1677-
class _DialogDefaultsM3 extends DialogTheme {
1680+
class _DialogDefaultsM3 extends DialogThemeData {
16781681
_DialogDefaultsM3(this.context)
16791682
: super(
16801683
alignment: Alignment.center,
@@ -1718,7 +1721,7 @@ class _DialogDefaultsM3 extends DialogTheme {
17181721
// Design token database by the script:
17191722
// dev/tools/gen_defaults/bin/gen_defaults.dart.
17201723

1721-
class _DialogFullscreenDefaultsM3 extends DialogTheme {
1724+
class _DialogFullscreenDefaultsM3 extends DialogThemeData {
17221725
const _DialogFullscreenDefaultsM3(this.context): super(clipBehavior: Clip.none);
17231726

17241727
final BuildContext context;

0 commit comments

Comments
 (0)