Skip to content

Commit df95cb2

Browse files
authored
Add locale in DatePickerThemeData (#148292)
*This PR changes the date picker were add locale in DatePickerThemeData* *List which issues are fixed by this PR. You must list at least one issue. An issue is not required if the PR fixes something trivial like a typo.* Fixes #148202 *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].*
1 parent 3d4fd55 commit df95cb2

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

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

+9
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,15 @@ Future<DateTime?> showDatePicker({
241241
locale: locale,
242242
child: dialog,
243243
);
244+
} else {
245+
final DatePickerThemeData datePickerTheme = DatePickerTheme.of(context);
246+
if (datePickerTheme.locale != null) {
247+
dialog = Localizations.override(
248+
context: context,
249+
locale: datePickerTheme.locale,
250+
child: dialog,
251+
);
252+
}
244253
}
245254

246255
return showDialog<DateTime>(

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class DatePickerThemeData with Diagnosticable {
7575
this.inputDecorationTheme,
7676
this.cancelButtonStyle,
7777
this.confirmButtonStyle,
78+
this.locale,
7879
});
7980

8081
/// Overrides the default value of [Dialog.backgroundColor].
@@ -347,6 +348,10 @@ class DatePickerThemeData with Diagnosticable {
347348
/// Overrides the default style of the confirm (OK) button of a [DatePickerDialog].
348349
final ButtonStyle? confirmButtonStyle;
349350

351+
/// An optional [locale] argument can be used to set the locale for the date
352+
/// picker. It defaults to the ambient locale provided by [Localizations].
353+
final Locale? locale;
354+
350355
/// Creates a copy of this object with the given fields replaced with the
351356
/// new values.
352357
DatePickerThemeData copyWith({
@@ -387,6 +392,7 @@ class DatePickerThemeData with Diagnosticable {
387392
InputDecorationTheme? inputDecorationTheme,
388393
ButtonStyle? cancelButtonStyle,
389394
ButtonStyle? confirmButtonStyle,
395+
Locale? locale,
390396
}) {
391397
return DatePickerThemeData(
392398
backgroundColor: backgroundColor ?? this.backgroundColor,
@@ -426,6 +432,7 @@ class DatePickerThemeData with Diagnosticable {
426432
inputDecorationTheme: inputDecorationTheme ?? this.inputDecorationTheme,
427433
cancelButtonStyle: cancelButtonStyle ?? this.cancelButtonStyle,
428434
confirmButtonStyle: confirmButtonStyle ?? this.confirmButtonStyle,
435+
locale: locale ?? this.locale,
429436
);
430437
}
431438

@@ -472,6 +479,7 @@ class DatePickerThemeData with Diagnosticable {
472479
inputDecorationTheme: t < 0.5 ? a?.inputDecorationTheme : b?.inputDecorationTheme,
473480
cancelButtonStyle: ButtonStyle.lerp(a?.cancelButtonStyle, b?.cancelButtonStyle, t),
474481
confirmButtonStyle: ButtonStyle.lerp(a?.confirmButtonStyle, b?.confirmButtonStyle, t),
482+
locale: t < 0.5 ? a?.locale : b?.locale,
475483
);
476484
}
477485

@@ -524,6 +532,7 @@ class DatePickerThemeData with Diagnosticable {
524532
inputDecorationTheme,
525533
cancelButtonStyle,
526534
confirmButtonStyle,
535+
locale,
527536
]);
528537

529538
@override
@@ -568,7 +577,8 @@ class DatePickerThemeData with Diagnosticable {
568577
&& other.dividerColor == dividerColor
569578
&& other.inputDecorationTheme == inputDecorationTheme
570579
&& other.cancelButtonStyle == cancelButtonStyle
571-
&& other.confirmButtonStyle == confirmButtonStyle;
580+
&& other.confirmButtonStyle == confirmButtonStyle
581+
&& other.locale == locale;
572582
}
573583

574584
@override
@@ -611,6 +621,7 @@ class DatePickerThemeData with Diagnosticable {
611621
properties.add(DiagnosticsProperty<InputDecorationTheme>('inputDecorationTheme', inputDecorationTheme, defaultValue: null));
612622
properties.add(DiagnosticsProperty<ButtonStyle>('cancelButtonStyle', cancelButtonStyle, defaultValue: null));
613623
properties.add(DiagnosticsProperty<ButtonStyle>('confirmButtonStyle', confirmButtonStyle, defaultValue: null));
624+
properties.add(DiagnosticsProperty<Locale>('locale', locale, defaultValue: null));
614625
}
615626
}
616627

packages/flutter/test/material/date_picker_theme_test.dart

+6-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ void main() {
5050
),
5151
cancelButtonStyle: ButtonStyle(foregroundColor: MaterialStatePropertyAll<Color>(Color(0xffffff6f))),
5252
confirmButtonStyle: ButtonStyle(foregroundColor: MaterialStatePropertyAll<Color>(Color(0xffffff7f))),
53+
locale: Locale('en'),
5354
);
5455

5556
Material findDialogMaterial(WidgetTester tester) {
@@ -145,6 +146,7 @@ void main() {
145146
expect(theme.inputDecorationTheme, null);
146147
expect(theme.cancelButtonStyle, null);
147148
expect(theme.confirmButtonStyle, null);
149+
expect(theme.locale, null);
148150
});
149151

150152
testWidgets('DatePickerTheme.defaults M3 defaults', (WidgetTester tester) async {
@@ -223,6 +225,7 @@ void main() {
223225
expect(m3.inputDecorationTheme, null);
224226
expect(m3.cancelButtonStyle.toString(), equalsIgnoringHashCodes(TextButton.styleFrom().toString()));
225227
expect(m3.confirmButtonStyle.toString(), equalsIgnoringHashCodes(TextButton.styleFrom().toString()));
228+
expect(m3.locale, null);
226229
});
227230

228231
testWidgets('DatePickerTheme.defaults M2 defaults', (WidgetTester tester) async {
@@ -293,6 +296,7 @@ void main() {
293296
expect(m2.inputDecorationTheme, null);
294297
expect(m2.cancelButtonStyle.toString(), equalsIgnoringHashCodes(TextButton.styleFrom().toString()));
295298
expect(m2.confirmButtonStyle.toString(), equalsIgnoringHashCodes(TextButton.styleFrom().toString()));
299+
expect(m2.locale, null);
296300
});
297301

298302
testWidgets('Default DatePickerThemeData debugFillProperties', (WidgetTester tester) async {
@@ -354,7 +358,8 @@ void main() {
354358
'dividerColor: Color(0xffffff4f)',
355359
'inputDecorationTheme: InputDecorationTheme#00000(fillColor: Color(0xffffff5f), border: UnderlineInputBorder())',
356360
'cancelButtonStyle: ButtonStyle#00000(foregroundColor: WidgetStatePropertyAll(Color(0xffffff6f)))',
357-
'confirmButtonStyle: ButtonStyle#00000(foregroundColor: WidgetStatePropertyAll(Color(0xffffff7f)))'
361+
'confirmButtonStyle: ButtonStyle#00000(foregroundColor: WidgetStatePropertyAll(Color(0xffffff7f)))',
362+
'locale: en',
358363
]));
359364
});
360365

0 commit comments

Comments
 (0)