在Flutter中,设置日期和时间的插件如何设置中文(国际化)
即:showDatePicker&showTimePicker国际化
1.添加国际化 flutter_localizations
在/pubspec.yaml中添加:
...
dependencies:
flutter:
sdk: flutter
flutter_localizations: # 新增
sdk: flutter # 新增
...
执行 flutter pub get 命令
2.引入国际化插件
/lib/main.dart中的 MaterialApp中加入以下代码:
import 'package:flutter_localizations/flutter_localizations.dart';
MaterialApp(
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
const Locale('zh', 'CH'),
const Locale('en', 'US'),
],
locale: const Locale('zh'),
...
)
3.showDatePicker,默认使用MaterialApp中的locale设置
在pad中显示的效果4.showTimePicker,默认使用MaterialApp中的locale设置
在pad中显示时间的效果如果自定义 locale 可以参考如下
await showTimePicker(
context: context,
initialTime: TimeOfDay.now(),
builder: (BuildContext context, Widget child) {
return Localizations(
locale: const Locale('zh'),
child: child,
delegates: <LocalizationsDelegate>[
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
]
)
}
)
网友评论