diff --git a/packages/flutter/lib/src/material/date_picker.dart b/packages/flutter/lib/src/material/date_picker.dart index 6d8fed0fcab..66480446a89 100644 --- a/packages/flutter/lib/src/material/date_picker.dart +++ b/packages/flutter/lib/src/material/date_picker.dart @@ -241,6 +241,15 @@ Future showDatePicker({ locale: locale, child: dialog, ); + } else { + final DatePickerThemeData datePickerTheme = DatePickerTheme.of(context); + if (datePickerTheme.locale != null) { + dialog = Localizations.override( + context: context, + locale: datePickerTheme.locale, + child: dialog, + ); + } } return showDialog( diff --git a/packages/flutter/lib/src/material/date_picker_theme.dart b/packages/flutter/lib/src/material/date_picker_theme.dart index cbdf7a9abdd..4028f981460 100644 --- a/packages/flutter/lib/src/material/date_picker_theme.dart +++ b/packages/flutter/lib/src/material/date_picker_theme.dart @@ -75,6 +75,7 @@ class DatePickerThemeData with Diagnosticable { this.inputDecorationTheme, this.cancelButtonStyle, this.confirmButtonStyle, + this.locale, }); /// Overrides the default value of [Dialog.backgroundColor]. @@ -347,6 +348,10 @@ class DatePickerThemeData with Diagnosticable { /// Overrides the default style of the confirm (OK) button of a [DatePickerDialog]. final ButtonStyle? confirmButtonStyle; + /// An optional [locale] argument can be used to set the locale for the date + /// picker. It defaults to the ambient locale provided by [Localizations]. + final Locale? locale; + /// Creates a copy of this object with the given fields replaced with the /// new values. DatePickerThemeData copyWith({ @@ -387,6 +392,7 @@ class DatePickerThemeData with Diagnosticable { InputDecorationTheme? inputDecorationTheme, ButtonStyle? cancelButtonStyle, ButtonStyle? confirmButtonStyle, + Locale? locale, }) { return DatePickerThemeData( backgroundColor: backgroundColor ?? this.backgroundColor, @@ -426,6 +432,7 @@ class DatePickerThemeData with Diagnosticable { inputDecorationTheme: inputDecorationTheme ?? this.inputDecorationTheme, cancelButtonStyle: cancelButtonStyle ?? this.cancelButtonStyle, confirmButtonStyle: confirmButtonStyle ?? this.confirmButtonStyle, + locale: locale ?? this.locale, ); } @@ -472,6 +479,7 @@ class DatePickerThemeData with Diagnosticable { inputDecorationTheme: t < 0.5 ? a?.inputDecorationTheme : b?.inputDecorationTheme, cancelButtonStyle: ButtonStyle.lerp(a?.cancelButtonStyle, b?.cancelButtonStyle, t), confirmButtonStyle: ButtonStyle.lerp(a?.confirmButtonStyle, b?.confirmButtonStyle, t), + locale: t < 0.5 ? a?.locale : b?.locale, ); } @@ -524,6 +532,7 @@ class DatePickerThemeData with Diagnosticable { inputDecorationTheme, cancelButtonStyle, confirmButtonStyle, + locale, ]); @override @@ -568,7 +577,8 @@ class DatePickerThemeData with Diagnosticable { && other.dividerColor == dividerColor && other.inputDecorationTheme == inputDecorationTheme && other.cancelButtonStyle == cancelButtonStyle - && other.confirmButtonStyle == confirmButtonStyle; + && other.confirmButtonStyle == confirmButtonStyle + && other.locale == locale; } @override @@ -611,6 +621,7 @@ class DatePickerThemeData with Diagnosticable { properties.add(DiagnosticsProperty('inputDecorationTheme', inputDecorationTheme, defaultValue: null)); properties.add(DiagnosticsProperty('cancelButtonStyle', cancelButtonStyle, defaultValue: null)); properties.add(DiagnosticsProperty('confirmButtonStyle', confirmButtonStyle, defaultValue: null)); + properties.add(DiagnosticsProperty('locale', locale, defaultValue: null)); } } diff --git a/packages/flutter/test/material/date_picker_theme_test.dart b/packages/flutter/test/material/date_picker_theme_test.dart index 4095e7d562a..85a84aff75f 100644 --- a/packages/flutter/test/material/date_picker_theme_test.dart +++ b/packages/flutter/test/material/date_picker_theme_test.dart @@ -50,6 +50,7 @@ void main() { ), cancelButtonStyle: ButtonStyle(foregroundColor: MaterialStatePropertyAll(Color(0xffffff6f))), confirmButtonStyle: ButtonStyle(foregroundColor: MaterialStatePropertyAll(Color(0xffffff7f))), + locale: Locale('en'), ); Material findDialogMaterial(WidgetTester tester) { @@ -145,6 +146,7 @@ void main() { expect(theme.inputDecorationTheme, null); expect(theme.cancelButtonStyle, null); expect(theme.confirmButtonStyle, null); + expect(theme.locale, null); }); testWidgets('DatePickerTheme.defaults M3 defaults', (WidgetTester tester) async { @@ -223,6 +225,7 @@ void main() { expect(m3.inputDecorationTheme, null); expect(m3.cancelButtonStyle.toString(), equalsIgnoringHashCodes(TextButton.styleFrom().toString())); expect(m3.confirmButtonStyle.toString(), equalsIgnoringHashCodes(TextButton.styleFrom().toString())); + expect(m3.locale, null); }); testWidgets('DatePickerTheme.defaults M2 defaults', (WidgetTester tester) async { @@ -293,6 +296,7 @@ void main() { expect(m2.inputDecorationTheme, null); expect(m2.cancelButtonStyle.toString(), equalsIgnoringHashCodes(TextButton.styleFrom().toString())); expect(m2.confirmButtonStyle.toString(), equalsIgnoringHashCodes(TextButton.styleFrom().toString())); + expect(m2.locale, null); }); testWidgets('Default DatePickerThemeData debugFillProperties', (WidgetTester tester) async { @@ -354,7 +358,8 @@ void main() { 'dividerColor: Color(0xffffff4f)', 'inputDecorationTheme: InputDecorationTheme#00000(fillColor: Color(0xffffff5f), border: UnderlineInputBorder())', 'cancelButtonStyle: ButtonStyle#00000(foregroundColor: WidgetStatePropertyAll(Color(0xffffff6f)))', - 'confirmButtonStyle: ButtonStyle#00000(foregroundColor: WidgetStatePropertyAll(Color(0xffffff7f)))' + 'confirmButtonStyle: ButtonStyle#00000(foregroundColor: WidgetStatePropertyAll(Color(0xffffff7f)))', + 'locale: en', ])); });