Skip to content

Commit 64906c2

Browse files
authored
Update ThemeData.dialogTheme type to accept DialogThemeData (#155129)
Following flutter/flutter#153982, this PR is to normalize `ThemeData.dialogTheme`; change the `DialogTheme dialogTheme` property to `DialogThemeData dialogTheme` in ThemeData. In `ThemeData()` and `ThemeData.copyWith()`, the dialogTheme parameter type is changed to `Object?` to accept both `DialogTheme` and `DialogThemeData` so that we won't cause immediate breaking change and make sure rolling is smooth. Once all component themes are normalized, these `Object?` types should be changed to `xxxThemeData`. There's no way to create a dart fix because we can't add a "@deprecated" label for `DialogTheme`; `DialogTheme` is a new `InheritedWidget` subclass now. Addresses the "theme normalization" sub project within flutter/flutter#91772
1 parent 07745fb commit 64906c2

File tree

7 files changed

+161
-37
lines changed

7 files changed

+161
-37
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ class _DatePickerDialogState extends State<DatePickerDialog> with RestorationMix
707707
// layout issues.
708708
final double textScaleFactor = MediaQuery.textScalerOf(context).clamp(maxScaleFactor: _kMaxTextScaleFactor).scale(_fontSizeToScale) / _fontSizeToScale;
709709
final Size dialogSize = _dialogSize(context) * textScaleFactor;
710-
final DialogTheme dialogTheme = theme.dialogTheme;
710+
final DialogThemeData dialogTheme = theme.dialogTheme;
711711
return Dialog(
712712
backgroundColor: datePickerTheme.backgroundColor ?? defaults.backgroundColor,
713713
elevation: useMaterial3
@@ -1660,7 +1660,7 @@ class _DateRangePickerDialogState extends State<DateRangePickerDialog> with Rest
16601660
: localizations.dateRangePickerHelpText.toUpperCase()
16611661
),
16621662
);
1663-
final DialogTheme dialogTheme = theme.dialogTheme;
1663+
final DialogThemeData dialogTheme = theme.dialogTheme;
16641664
size = orientation == Orientation.portrait
16651665
? (useMaterial3 ? _inputPortraitDialogSizeM3 : _inputPortraitDialogSizeM2)
16661666
: _inputRangeLandscapeDialogSize;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ class Dialog extends StatelessWidget {
230230
@override
231231
Widget build(BuildContext context) {
232232
final ThemeData theme = Theme.of(context);
233-
final DialogThemeData dialogTheme = DialogTheme.of(context).data;
233+
final DialogThemeData dialogTheme = DialogTheme.of(context);
234234
final EdgeInsets effectivePadding = MediaQuery.viewInsetsOf(context)
235235
+ (insetPadding ?? dialogTheme.insetPadding ?? _defaultInsetPadding);
236236
final DialogThemeData defaults = theme.useMaterial3
@@ -720,7 +720,7 @@ class AlertDialog extends StatelessWidget {
720720
assert(debugCheckHasMaterialLocalizations(context));
721721
final ThemeData theme = Theme.of(context);
722722

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

726726
String? label = semanticLabel;
@@ -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).data.titleTextStyle ?? theme.textTheme.titleLarge!;
1238+
final TextStyle defaultTextStyle = titleTextStyle ?? DialogTheme.of(context).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;
@@ -1445,7 +1445,7 @@ Future<T?> showDialog<T>({
14451445
builder: builder,
14461446
barrierColor: barrierColor
14471447
?? DialogTheme.of(context).barrierColor
1448-
?? Theme.of(context).dialogTheme.data.barrierColor
1448+
?? Theme.of(context).dialogTheme.barrierColor
14491449
?? Colors.black54,
14501450
barrierDismissible: barrierDismissible,
14511451
barrierLabel: barrierLabel,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,9 @@ class DialogTheme extends InheritedTheme with Diagnosticable {
190190
}
191191

192192
/// The [ThemeData.dialogTheme] property of the ambient [Theme].
193-
static DialogTheme of(BuildContext context) {
193+
static DialogThemeData of(BuildContext context) {
194194
final DialogTheme? dialogTheme = context.dependOnInheritedWidgetOfExactType<DialogTheme>();
195-
return dialogTheme ?? Theme.of(context).dialogTheme;
195+
return dialogTheme?.data ?? Theme.of(context).dialogTheme;
196196
}
197197

198198
@override

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

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,8 @@ class ThemeData with Diagnosticable {
320320
ChipThemeData? chipTheme,
321321
DataTableThemeData? dataTableTheme,
322322
DatePickerThemeData? datePickerTheme,
323-
DialogTheme? dialogTheme,
323+
// TODO(QuncCccccc): Change the parameter type to DialogThemeData
324+
Object? dialogTheme,
324325
DividerThemeData? dividerTheme,
325326
DrawerThemeData? drawerTheme,
326327
DropdownMenuThemeData? dropdownMenuTheme,
@@ -497,7 +498,15 @@ class ThemeData with Diagnosticable {
497498
chipTheme ??= const ChipThemeData();
498499
dataTableTheme ??= const DataTableThemeData();
499500
datePickerTheme ??= const DatePickerThemeData();
500-
dialogTheme ??= const DialogTheme();
501+
// TODO(QuncCccccc): Clean this up once the type of `dialogTheme` is changed to `DialogThemeData`
502+
if (dialogTheme != null) {
503+
if (dialogTheme is DialogTheme) {
504+
dialogTheme = dialogTheme.data;
505+
} else if (dialogTheme is! DialogThemeData) {
506+
throw ArgumentError('dialogTheme must be either a DialogThemeData or a DialogTheme');
507+
}
508+
}
509+
dialogTheme ??= const DialogThemeData();
501510
dividerTheme ??= const DividerThemeData();
502511
drawerTheme ??= const DrawerThemeData();
503512
dropdownMenuTheme ??= const DropdownMenuThemeData();
@@ -590,7 +599,7 @@ class ThemeData with Diagnosticable {
590599
chipTheme: chipTheme,
591600
dataTableTheme: dataTableTheme,
592601
datePickerTheme: datePickerTheme,
593-
dialogTheme: dialogTheme,
602+
dialogTheme: dialogTheme as DialogThemeData,
594603
dividerTheme: dividerTheme,
595604
drawerTheme: drawerTheme,
596605
dropdownMenuTheme: dropdownMenuTheme,
@@ -1276,7 +1285,7 @@ class ThemeData with Diagnosticable {
12761285
final DatePickerThemeData datePickerTheme;
12771286

12781287
/// A theme for customizing the shape of a dialog.
1279-
final DialogTheme dialogTheme;
1288+
final DialogThemeData dialogTheme;
12801289

12811290
/// A theme for customizing the color, thickness, and indents of [Divider]s,
12821291
/// [VerticalDivider]s, etc.
@@ -1462,7 +1471,8 @@ class ThemeData with Diagnosticable {
14621471
ChipThemeData? chipTheme,
14631472
DataTableThemeData? dataTableTheme,
14641473
DatePickerThemeData? datePickerTheme,
1465-
DialogTheme? dialogTheme,
1474+
// TODO(QuncCccccc): Change the parameter type to DialogThemeData
1475+
Object? dialogTheme,
14661476
DividerThemeData? dividerTheme,
14671477
DrawerThemeData? drawerTheme,
14681478
DropdownMenuThemeData? dropdownMenuTheme,
@@ -1510,6 +1520,15 @@ class ThemeData with Diagnosticable {
15101520
ButtonBarThemeData? buttonBarTheme,
15111521
}) {
15121522
cupertinoOverrideTheme = cupertinoOverrideTheme?.noDefault();
1523+
1524+
// TODO(QuncCccccc): Clean this up once the type of `dialogTheme` is changed to `DialogThemeData`
1525+
if (dialogTheme != null) {
1526+
if (dialogTheme is DialogTheme) {
1527+
dialogTheme = dialogTheme.data;
1528+
} else if (dialogTheme is! DialogThemeData) {
1529+
throw ArgumentError('dialogTheme must be either a DialogThemeData or a DialogTheme');
1530+
}
1531+
}
15131532
return ThemeData.raw(
15141533
// For the sanity of the reader, make sure these properties are in the same
15151534
// order in every place that they are separated by section comments (e.g.
@@ -1571,7 +1590,7 @@ class ThemeData with Diagnosticable {
15711590
chipTheme: chipTheme ?? this.chipTheme,
15721591
dataTableTheme: dataTableTheme ?? this.dataTableTheme,
15731592
datePickerTheme: datePickerTheme ?? this.datePickerTheme,
1574-
dialogTheme: dialogTheme ?? this.dialogTheme,
1593+
dialogTheme: dialogTheme as DialogThemeData? ?? this.dialogTheme,
15751594
dividerTheme: dividerTheme ?? this.dividerTheme,
15761595
drawerTheme: drawerTheme ?? this.drawerTheme,
15771596
dropdownMenuTheme: dropdownMenuTheme ?? this.dropdownMenuTheme,
@@ -1764,7 +1783,7 @@ class ThemeData with Diagnosticable {
17641783
chipTheme: ChipThemeData.lerp(a.chipTheme, b.chipTheme, t)!,
17651784
dataTableTheme: DataTableThemeData.lerp(a.dataTableTheme, b.dataTableTheme, t),
17661785
datePickerTheme: DatePickerThemeData.lerp(a.datePickerTheme, b.datePickerTheme, t),
1767-
dialogTheme: DialogTheme.lerp(a.dialogTheme, b.dialogTheme, t),
1786+
dialogTheme: DialogThemeData.lerp(a.dialogTheme, b.dialogTheme, t),
17681787
dividerTheme: DividerThemeData.lerp(a.dividerTheme, b.dividerTheme, t),
17691788
drawerTheme: DrawerThemeData.lerp(a.drawerTheme, b.drawerTheme, t)!,
17701789
dropdownMenuTheme: DropdownMenuThemeData.lerp(a.dropdownMenuTheme, b.dropdownMenuTheme, t),
@@ -2062,7 +2081,7 @@ class ThemeData with Diagnosticable {
20622081
properties.add(DiagnosticsProperty<ChipThemeData>('chipTheme', chipTheme, level: DiagnosticLevel.debug));
20632082
properties.add(DiagnosticsProperty<DataTableThemeData>('dataTableTheme', dataTableTheme, defaultValue: defaultData.dataTableTheme, level: DiagnosticLevel.debug));
20642083
properties.add(DiagnosticsProperty<DatePickerThemeData>('datePickerTheme', datePickerTheme, defaultValue: defaultData.datePickerTheme, level: DiagnosticLevel.debug));
2065-
properties.add(DiagnosticsProperty<DialogTheme>('dialogTheme', dialogTheme, defaultValue: defaultData.dialogTheme, level: DiagnosticLevel.debug));
2084+
properties.add(DiagnosticsProperty<DialogThemeData>('dialogTheme', dialogTheme, defaultValue: defaultData.dialogTheme, level: DiagnosticLevel.debug));
20662085
properties.add(DiagnosticsProperty<DividerThemeData>('dividerTheme', dividerTheme, defaultValue: defaultData.dividerTheme, level: DiagnosticLevel.debug));
20672086
properties.add(DiagnosticsProperty<DrawerThemeData>('drawerTheme', drawerTheme, defaultValue: defaultData.drawerTheme, level: DiagnosticLevel.debug));
20682087
properties.add(DiagnosticsProperty<DropdownMenuThemeData>('dropdownMenuTheme', dropdownMenuTheme, defaultValue: defaultData.dropdownMenuTheme, level: DiagnosticLevel.debug));

packages/flutter/test/material/date_picker_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ void main() {
603603
expect(defaultDialogMaterial.elevation, datePickerDefaultDialogTheme.elevation);
604604

605605
// Test that it honors ThemeData.dialogTheme settings
606-
const DialogTheme customDialogTheme = DialogTheme(
606+
const DialogThemeData customDialogTheme = DialogThemeData(
607607
shape: RoundedRectangleBorder(
608608
borderRadius: BorderRadius.all(Radius.circular(40.0)),
609609
),

0 commit comments

Comments
 (0)