我想在演出日期选择器中将语言从英语更改为法语,请在下面使用我正在使用的代码,以及一个应该解决的代码,但该代码对此部分无效:
new Step(
title: new Text("Quelle est la date de 1er immatriculation?"),
content: Column(
children: <Widget>[
Text(_datetime == null ? "Vous n'avez pas encore choisi de date" : _datetime.toString().substring(0, 10)),
RaisedButton(
child: Text('choisissez une date'),
onPressed: () {
showDatePicker(context: context,
locale : const Locale("fr","FR"),//this line making the code not working too
builder: (BuildContext context, Widget child) {
return Theme(
data: ThemeData.fallback(),
child: child,
);
},
// locale: const Locale('eu', 'FR'),
initialDate: DateTime.now(),
firstDate: DateTime(1920),
lastDate: DateTime(2100),
).then((date) {
setState(() {
_datetime = date;
});
});
}
),
],
),
isActive: _currentStep >= 0,
state:
_currentStep >= 2 ? StepState.complete : StepState.disabled,
),
答案 0 :(得分:20)
为了以本地语言显示日期选择器,您需要使用flutter_localizations
插件,并在主代码的localizationDelegates
中指定supportedLocales
和MaterialApp
。以下是示例工作代码,显示了French
中的日期选择器:
flutter_localizations
中添加pubspec.yaml
插件并运行pub get
。在MaterialApp
内,添加以下内容:
return MaterialApp(
localizationsDelegates: [
GlobalMaterialLocalizations.delegate
],
supportedLocales: [
const Locale('en'),
const Locale('fr')
],
....
body: Center(
child: RaisedButton(
child: Text('Tap'),
onPressed: () {
showDatePicker(
context: context,
locale : const Locale("fr","FR"),
initialDate: DateTime.now(),
firstDate: DateTime(2018),
lastDate: DateTime(2030),
builder: (BuildContext context, Widget child) {
return Theme(
data: ThemeData.dark(),
child: child,
);
}
);
},
)
)
再次运行应用程序(热重启),然后在French
中看到日期选择器。
希望这能回答您的问题。
答案 1 :(得分:2)
我遵循@Darshan的回答,但出现以下错误:
Unsupported operation: Cannot set value in unmodifiable Map
从await initializeDateFormatting('fr_FR');
中删除main.dart
后,对我来说效果很好,请尽情享受