Flutter 如何更改CupertinodePicker显示语言?
我正跟着弗利特医生向库比诺达提克展示。但语言总是英语。请告诉我如何改变这个 这是我的代码:Flutter 如何更改CupertinodePicker显示语言?,flutter,Flutter,我正跟着弗利特医生向库比诺达提克展示。但语言总是英语。请告诉我如何改变这个 这是我的代码: void _showDatePicker(BuildContext context, bool isYou) { showModalBottomSheet( context: context, builder: (context) { return CupertinoDatePicker( onDateTimeChange
void _showDatePicker(BuildContext context, bool isYou) {
showModalBottomSheet(
context: context,
builder: (context) {
return CupertinoDatePicker(
onDateTimeChanged: (DateTime value) {
setState(() {
if (isYou) {
_dateOfBirth = value;
} else {
_dateOfBirthAnother = value;
}
});
},
initialDateTime: DateTime.now(),
mode: CupertinoDatePickerMode.date,
maximumYear: 2018,
minimumYear: 1950,
);
});
}
将所需语言添加到MaterialApp(或CupertinoApp)配置中。 像这样:
return MaterialApp(
localizationsDelegates: [
// ... app-specific localization delegate[s] here
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
DefaultCupertinoLocalizations.delegate,
],
supportedLocales: [
const Locale('en', 'US'), // English
const Locale('de', 'DE'), // German
// ... other locales the app supports
], <the rest of your configuration> );
此外,Cupertino小部件尚未完全本地化。
但是您可以编写自己的本地化配置类,并将它们添加到我的代码中的“DefaultCupertinoLocalizations.delegate”行下面
例如,为了在德语中使用CupertinodePicker,我必须创建自定义德语本地化,如下所示:
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
class _CupertinoLocalizationsDelegate extends LocalizationsDelegate<CupertinoLocalizations> {
const _CupertinoLocalizationsDelegate();
@override
bool isSupported(Locale locale) => locale.languageCode == 'de';
@override
Future<CupertinoLocalizations> load(Locale locale) => GermanCupertinoLocalizations.load(locale);
@override
bool shouldReload(_CupertinoLocalizationsDelegate old) => false;
@override
String toString() => 'DefaultCupertinoLocalizations.delegate(de_DE)';
}
/// US English strings for the cupertino widgets.
class GermanCupertinoLocalizations implements CupertinoLocalizations {
/// Constructs an object that defines the cupertino widgets' localized strings
/// for US English (only).
///
/// [LocalizationsDelegate] implementations typically call the static [load]
/// function, rather than constructing this class directly.
const GermanCupertinoLocalizations();
static const List<String> _shortWeekdays = <String>[
'Mo',
'Di',
'Mi',
'Do',
'Fr',
'Sa',
'So',
];
static const List<String> _shortMonths = <String>[
'Jan',
'Feb',
'Mär',
'Apr',
'Mai',
'Jun',
'Jul',
'Aug',
'Sep',
'Okt',
'Nov',
'Dez',
];
static const List<String> _months = <String>[
'Januar',
'Februar',
'März',
'April',
'Mai',
'Juni',
'Juli',
'August',
'September',
'Oktober',
'November',
'Dezember',
];
@override
String datePickerYear(int yearIndex) => yearIndex.toString();
@override
String datePickerMonth(int monthIndex) => _months[monthIndex - 1];
@override
String datePickerDayOfMonth(int dayIndex) => dayIndex.toString();
@override
String datePickerHour(int hour) => hour.toString();
@override
String datePickerHourSemanticsLabel(int hour) => hour.toString() + " Uhr";
@override
String datePickerMinute(int minute) => minute.toString().padLeft(2, '0');
@override
String datePickerMinuteSemanticsLabel(int minute) {
if (minute == 1)
return '1 Minute';
return minute.toString() + ' Minuten';
}
@override
String datePickerMediumDate(DateTime date) {
return '${_shortWeekdays[date.weekday - DateTime.monday]} '
'${_shortMonths[date.month - DateTime.january]} '
'${date.day.toString().padRight(2)}';
}
@override
DatePickerDateOrder get datePickerDateOrder => DatePickerDateOrder.mdy;
@override
DatePickerDateTimeOrder get datePickerDateTimeOrder => DatePickerDateTimeOrder.date_time_dayPeriod;
@override
String get anteMeridiemAbbreviation => 'AM';
@override
String get postMeridiemAbbreviation => 'PM';
@override
String get alertDialogLabel => 'Info';
@override
String timerPickerHour(int hour) => hour.toString();
@override
String timerPickerMinute(int minute) => minute.toString();
@override
String timerPickerSecond(int second) => second.toString();
@override
String timerPickerHourLabel(int hour) => hour == 1 ? 'Stunde' : 'Stunden';
@override
String timerPickerMinuteLabel(int minute) => 'Min';
@override
String timerPickerSecondLabel(int second) => 'Sek';
@override
String get cutButtonLabel => 'Ausschneiden';
@override
String get copyButtonLabel => 'Kopieren';
@override
String get pasteButtonLabel => 'Einfügen';
@override
String get selectAllButtonLabel => 'Alles auswählen';
/// Creates an object that provides US English resource values for the
/// cupertino library widgets.
///
/// The [locale] parameter is ignored.
///
/// This method is typically used to create a [LocalizationsDelegate].
static Future<CupertinoLocalizations> load(Locale locale) {
return SynchronousFuture<CupertinoLocalizations>(const GermanCupertinoLocalizations());
}
/// A [LocalizationsDelegate] that uses [DefaultCupertinoLocalizations.load]
/// to create an instance of this class.
static const LocalizationsDelegate<CupertinoLocalizations> delegate = _CupertinoLocalizationsDelegate();
}
导入'dart:async';
进口“包装:颤振/cupertino.dart”;
进口“包装:颤振/基础.dart”;
导入“package:flatter/widgets.dart”;
类_CupertinoLocalizationsDelegate扩展本地化sDelegate{
const_cupertinologizationsdelegate();
@凌驾
bool isSupported(Locale)=>Locale.languageCode=='de';
@凌驾
未来加载(Locale)=>GermanSupertinolocalizations.load(Locale);
@凌驾
bool应该重新加载(_cupertinologizationsdelegateold)=>false;
@凌驾
字符串toString()=>“DefaultCupertinoLocalizations.delegate(de_de)”;
}
///cupertino小部件的美英字符串。
类GermanSuperTinoLocalizations实现CupertinoLocalization{
///构造一个对象,该对象定义cupertino小部件的本地化字符串
///我们只有英语。
///
///[LocalizationsDelegate]实现通常调用静态[load]
///函数,而不是直接构造此类。
常量GermanSupertinolocalizations();
静态常量列表_短工作日=[
"莫",,
"地",,
"米",,
“做”,
“Fr”,
"Sa",,
“那么”,
];
静态常量列表_shortMonths=[
“一月”,
二月,,
“Mär”,
“四月”,
"麦",,
"六月",,
七月,,
"八月",,
"九月",,
“好的”,
十一月,,
“迪兹”,
];
静态常量列表_个月=[
“一月”,
“二月”,
“马尔兹”,
"四月",,
"麦",,
"Juni",,
"朱莉",,
“八月”,
"九月",,
“Oktober”,
"十一月",,
“Dezember”,
];
@凌驾
字符串datePickerYear(int yearIndex)=>yearIndex.toString();
@凌驾
字符串datePickerMonth(int-monthIndex)=>\u个月[monthIndex-1];
@凌驾
字符串datePickerDayOfMonth(int dayIndex)=>dayIndex.toString();
@凌驾
字符串datePickerHour(整小时)=>hour.toString();
@凌驾
字符串datePickerHourSemanticsLabel(整数小时)=>hour.toString()+“Uhr”;
@凌驾
字符串datePickerMinute(int-minute)=>minute.toString().padLeft(2,'0');
@凌驾
字符串DatePickerMinuteManticsLabel(整数分钟){
如果(分钟==1)
返回“1分钟”;
返回minute.toString()+'Minuten';
}
@凌驾
字符串datePickerMediumDate(日期时间日期){
返回“${u shortWeekdays[date.weekday-DateTime.monday]}”
“${u shortMonths[date.month-DateTime.一月]}”
“${date.day.toString().padRight(2)}”;
}
@凌驾
DatePickerDateOrder get DatePickerDateOrder=>DatePickerDateOrder.mdy;
@凌驾
DatePickerDateTimeOrder获取DatePickerDateTimeOrder=>DatePickerDateTimeOrder.date\u time\u dayPeriod;
@凌驾
字符串get ANTEMERIDIEMABREVIATION=>AM';
@凌驾
字符串get POSTERDIEMABREVIATION=>PM';
@凌驾
字符串get-alertDialogLabel=>'Info';
@凌驾
字符串timerPickerHour(int hour)=>hour.toString();
@凌驾
字符串timerPickerMinute(int minute)=>minute.toString();
@凌驾
字符串timerPickerSecond(int second)=>second.toString();
@凌驾
字符串timerPickerHourLabel(int hour)=>hour==1?'Stunde':'Stunden';
@凌驾
字符串timerPickerMinuteLabel(int-minute)=>“Min”;
@凌驾
字符串timerPickerSecondLabel(整数秒)=>“Sek”;
@凌驾
字符串get cutButtonLabel=>Ausschneiden';
@凌驾
字符串get copyButtonLabel=>Kopieren';
@凌驾
字符串get pasteButtonLabel=>Einfügen';
@凌驾
字符串get-selectAllButtonLabel=>“Alles auswählen”;
///创建一个对象,该对象向我们提供
///cupertino库小部件。
///
///忽略[locale]参数。
///
///此方法通常用于创建[LocalizationsDelegate]。
静态未来加载(区域设置){
返回SynchronousFuture(const GermanSupertinolocalizations());
}
///使用[DefaultCupertinoLocalizations.load]的[LocalizationsDelegate]
///创建此类的实例。
static const LocalizationsDelegate委托=_cupertinologizationsdelegate();
}
我已经找到了问题的解决方案,必须向本地化数据包添加回退,如下所示:
localizationsDelegates: [ ..... const FallbackCupertinoLocalisationsDelegate(), ]
代表:
class FallbackCupertinoLocalisationsDelegate extends LocalizationsDelegate {
const FallbackCupertinoLocalisationsDelegate();
@override
bool isSupported(Locale locale) => true;
@override
Future load(Locale locale) =>
DefaultCupertinoLocalizations.load(locale);
@override
bool shouldReload(FallbackCupertinoLocalisationsDelegate old) => false;
}
首先,您应该将其添加到pubspec.yaml中
flutter_cupertino_localizations: ^1.0.1
flutter_localizations:
sdk: flutter
MaterialApp(
...
本地化授权:[
GlobalMaterialAllocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalSuperTinoLocalizations.delegate,
]
)
这就解决了问题。不,你不需要所有这些工作。只需添加您的特定本地化,日期选择器将始终像您的Localeflutter\u cupertino\u本地化是正确的解决方案,请检查@malibayram91答案
flutter_cupertino_localizations: ^1.0.1
flutter_localizations:
sdk: flutter