如何在 flutter 中更改显示日期选择器的语言
How to change language of Show date picker in flutter
我想在节目日期选择器中将语言从英语更改为法语,请在下面找到我正在使用的代码和一个类似的代码,它应该可以解决这个问题,但它使代码不适用于这部分:
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,
),
为了以本地语言显示日期选择器,您需要使用 flutter_localizations
插件并在主文件的 MaterialApp
中指定 localizationDelegates
和 supportedLocales
代码。下面是在 French
:
中显示日期选择器的示例工作代码
- 在
pubspec.yaml
和运行pub get
中添加flutter_localizations
插件。
flutter_localizations:
sdk: flutter
- 在dart文件中导入插件。
import 'package:flutter_localizations/flutter_localizations.dart';
在 MaterialApp
内,添加以下内容:
return MaterialApp(
localizationsDelegates: [
GlobalMaterialLocalizations.delegate
],
supportedLocales: [
const Locale('en'),
const Locale('fr')
],
Center(
child: ElevatedButton(
child: const 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
。
有关本地化的更多信息,请访问 official docs
我按照 进行操作,但出现以下错误:
Unsupported operation: Cannot set value in unmodifiable Map
在我从 main.dart
中删除 await initializeDateFormatting('fr_FR');
后,它起作用了!
dependencies:
flutter:
sdk: flutter
flutter_localizations: # Add this line
sdk: flutter # Add this line
在main.dart
import 'package:flutter_localizations/flutter_localizations.dart';
在 MaterialApp 中
return const MaterialApp(
title: 'Localizations Sample App',
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
Locale('en', ''), code
Locale('ar', ''), // arabic, no country code
],
home: MyHomePage(),
);
然后将其添加到您的日期时间选择器
locale: const Locale("ar","AR"),
我想在节目日期选择器中将语言从英语更改为法语,请在下面找到我正在使用的代码和一个类似的代码,它应该可以解决这个问题,但它使代码不适用于这部分:
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,
),
为了以本地语言显示日期选择器,您需要使用 flutter_localizations
插件并在主文件的 MaterialApp
中指定 localizationDelegates
和 supportedLocales
代码。下面是在 French
:
- 在
pubspec.yaml
和运行pub get
中添加flutter_localizations
插件。
flutter_localizations:
sdk: flutter
- 在dart文件中导入插件。
import 'package:flutter_localizations/flutter_localizations.dart';
在
MaterialApp
内,添加以下内容:return MaterialApp( localizationsDelegates: [ GlobalMaterialLocalizations.delegate ], supportedLocales: [ const Locale('en'), const Locale('fr') ],
Center(
child: ElevatedButton(
child: const 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
。
有关本地化的更多信息,请访问 official docs
我按照
Unsupported operation: Cannot set value in unmodifiable Map
在我从 main.dart
中删除 await initializeDateFormatting('fr_FR');
后,它起作用了!
dependencies:
flutter:
sdk: flutter
flutter_localizations: # Add this line
sdk: flutter # Add this line
在main.dart
import 'package:flutter_localizations/flutter_localizations.dart';
在 MaterialApp 中
return const MaterialApp(
title: 'Localizations Sample App',
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
Locale('en', ''), code
Locale('ar', ''), // arabic, no country code
],
home: MyHomePage(),
);
然后将其添加到您的日期时间选择器
locale: const Locale("ar","AR"),