Skip to content

Commit 41c21bb

Browse files
authored
Apply normalization to TimePickerThemeData.inputDecorationTheme (flutter#171584)
## Description This PR is similar to what was done for `DatePickerThemeData` in flutter#168981. It changes `TimePickerThemeData.inputDecorationTheme` type to `InputDecorationThemeData` (instead of `InputDecorationTheme`) and uses Object? for the corresponding constructor parameter. ## Tests Adds 1 test
1 parent 50407af commit 41c21bb

4 files changed

Lines changed: 51 additions & 15 deletions

File tree

dev/tools/gen_defaults/lib/time_picker_template.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,15 +299,15 @@ class _${blockName}DefaultsM3 extends _TimePickerDefaults {
299299
}
300300
301301
@override
302-
InputDecorationTheme get inputDecorationTheme {
302+
InputDecorationThemeData get inputDecorationTheme {
303303
// This is NOT correct, but there's no token for
304304
// 'time-input.container.shape', so this is using the radius from the shape
305305
// for the hour/minute selector. It's a BorderRadiusGeometry, so we have to
306306
// resolve it before we can use it.
307307
final BorderRadius selectorRadius = ${shape('$hourMinuteComponent.container')}
308308
.borderRadius
309309
.resolve(Directionality.of(context));
310-
return InputDecorationTheme(
310+
return InputDecorationThemeData(
311311
contentPadding: EdgeInsets.zero,
312312
filled: true,
313313
// This should be derived from a token, but there isn't one for 'time-input'.

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2101,7 +2101,7 @@ class _HourMinuteTextFieldState extends State<_HourMinuteTextField> with Restora
21012101
: _TimePickerDefaultsM2(context);
21022102
final bool alwaysUse24HourFormat = MediaQuery.alwaysUse24HourFormatOf(context);
21032103

2104-
final InputDecorationTheme inputDecorationTheme =
2104+
final InputDecorationThemeData inputDecorationTheme =
21052105
timePickerTheme.inputDecorationTheme ?? defaultTheme.inputDecorationTheme;
21062106
InputDecoration inputDecoration = InputDecoration(
21072107
// Prevent the error text from appearing when
@@ -2110,7 +2110,7 @@ class _HourMinuteTextFieldState extends State<_HourMinuteTextField> with Restora
21102110
// https://github.com/flutter/flutter/issues/54104
21112111
// is fixed.
21122112
errorStyle: defaultTheme.inputDecorationTheme.errorStyle,
2113-
).applyDefaults(inputDecorationTheme.data);
2113+
).applyDefaults(inputDecorationTheme);
21142114
// Remove the hint text when focused because the centered cursor
21152115
// appears odd above the hint text.
21162116
final String? hintText = focusNode.hasFocus ? null : _formattedValue;
@@ -3256,7 +3256,7 @@ abstract class _TimePickerDefaults extends TimePickerThemeData {
32563256
TextStyle get hourMinuteTextStyle;
32573257

32583258
@override
3259-
InputDecorationTheme get inputDecorationTheme;
3259+
InputDecorationThemeData get inputDecorationTheme;
32603260

32613261
@override
32623262
EdgeInsetsGeometry get padding;
@@ -3462,8 +3462,8 @@ class _TimePickerDefaultsM2 extends _TimePickerDefaults {
34623462
}
34633463

34643464
@override
3465-
InputDecorationTheme get inputDecorationTheme {
3466-
return InputDecorationTheme(
3465+
InputDecorationThemeData get inputDecorationTheme {
3466+
return InputDecorationThemeData(
34673467
contentPadding: EdgeInsets.zero,
34683468
filled: true,
34693469
fillColor: _hourMinuteInputColor,
@@ -3779,15 +3779,15 @@ class _TimePickerDefaultsM3 extends _TimePickerDefaults {
37793779
}
37803780

37813781
@override
3782-
InputDecorationTheme get inputDecorationTheme {
3782+
InputDecorationThemeData get inputDecorationTheme {
37833783
// This is NOT correct, but there's no token for
37843784
// 'time-input.container.shape', so this is using the radius from the shape
37853785
// for the hour/minute selector. It's a BorderRadiusGeometry, so we have to
37863786
// resolve it before we can use it.
37873787
final BorderRadius selectorRadius = const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8.0)))
37883788
.borderRadius
37893789
.resolve(Directionality.of(context));
3790-
return InputDecorationTheme(
3790+
return InputDecorationThemeData(
37913791
contentPadding: EdgeInsets.zero,
37923792
filled: true,
37933793
// This should be derived from a token, but there isn't one for 'time-input'.

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,19 @@ class TimePickerThemeData with Diagnosticable {
6666
this.hourMinuteShape,
6767
this.hourMinuteTextColor,
6868
this.hourMinuteTextStyle,
69-
this.inputDecorationTheme,
69+
// TODO(bleroux): Clean this up once `InputDecorationTheme` is fully normalized.
70+
Object? inputDecorationTheme,
7071
this.padding,
7172
this.shape,
7273
this.timeSelectorSeparatorColor,
7374
this.timeSelectorSeparatorTextStyle,
74-
}) : _dayPeriodColor = dayPeriodColor;
75+
}) : assert(
76+
inputDecorationTheme == null ||
77+
(inputDecorationTheme is InputDecorationTheme ||
78+
inputDecorationTheme is InputDecorationThemeData),
79+
),
80+
_inputDecorationTheme = inputDecorationTheme,
81+
_dayPeriodColor = dayPeriodColor;
7582

7683
/// The background color of a time picker.
7784
///
@@ -267,7 +274,17 @@ class TimePickerThemeData with Diagnosticable {
267274
/// The input decoration theme for the [TextField]s in the time picker.
268275
///
269276
/// If this is null, the time picker provides its own defaults.
270-
final InputDecorationTheme? inputDecorationTheme;
277+
// TODO(bleroux): Clean this up once `InputDecorationTheme` is fully normalized.
278+
InputDecorationThemeData? get inputDecorationTheme {
279+
if (_inputDecorationTheme == null) {
280+
return null;
281+
}
282+
return _inputDecorationTheme is InputDecorationTheme
283+
? _inputDecorationTheme.data
284+
: _inputDecorationTheme as InputDecorationThemeData;
285+
}
286+
287+
final Object? _inputDecorationTheme;
271288

272289
/// The padding around the time picker dialog when the entry mode is
273290
/// [TimePickerEntryMode.dial] or [TimePickerEntryMode.dialOnly].
@@ -529,8 +546,8 @@ class TimePickerThemeData with Diagnosticable {
529546
);
530547
properties.add(
531548
DiagnosticsProperty<InputDecorationThemeData>(
532-
'inputDecorationTheme.data',
533-
inputDecorationTheme?.data,
549+
'inputDecorationTheme',
550+
inputDecorationTheme,
534551
defaultValue: null,
535552
),
536553
);

packages/flutter/test/material/time_picker_theme_test.dart

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ void main() {
120120
'hourMinuteShape: RoundedRectangleBorder(BorderSide(color: ${const Color(0xffffffff)}), BorderRadius.zero)',
121121
'hourMinuteTextColor: ${const Color(0xfffffff0)}',
122122
'hourMinuteTextStyle: TextStyle(inherit: true, color: ${const Color(0xfffffff1)})',
123-
'inputDecorationTheme.data: InputDecorationThemeData#ff861(labelStyle: TextStyle(inherit: true, color: ${const Color(0xfffffff2)}))',
123+
'inputDecorationTheme: InputDecorationThemeData#ff861(labelStyle: TextStyle(inherit: true, color: ${const Color(0xfffffff2)}))',
124124
'padding: EdgeInsets.all(1.0)',
125125
'shape: RoundedRectangleBorder(BorderSide(color: ${const Color(0xfffffff3)}), BorderRadius.zero)',
126126
'timeSelectorSeparatorColor: WidgetStatePropertyAll(${const Color(0xfffffff4)})',
@@ -129,6 +129,25 @@ void main() {
129129
);
130130
});
131131

132+
test(
133+
'TimePickerThemeData.inputDecorationTheme accepts only InputDecorationTheme or InputDecorationThemeData instances',
134+
() {
135+
const InputDecorationTheme decorationTheme = InputDecorationTheme();
136+
TimePickerThemeData timePickerTheme = const TimePickerThemeData(
137+
inputDecorationTheme: decorationTheme,
138+
);
139+
expect(timePickerTheme.inputDecorationTheme, decorationTheme.data);
140+
141+
timePickerTheme = TimePickerThemeData(inputDecorationTheme: decorationTheme.data);
142+
expect(timePickerTheme.inputDecorationTheme, decorationTheme.data);
143+
144+
// Wrong type throws.
145+
expect(() {
146+
TimePickerThemeData(inputDecorationTheme: Object());
147+
}, throwsA(isA<AssertionError>()));
148+
},
149+
);
150+
132151
testWidgets('Material2 - Passing no TimePickerThemeData uses defaults', (
133152
WidgetTester tester,
134153
) async {

0 commit comments

Comments
 (0)