在Flutter中,設(shè)置日期和時間的插件如何設(shè)置中文(國際化)
即:showDatePicker&showTimePicker國際化
1.添加國際化 flutter_localizations
在/pubspec.yaml中添加:
...
dependencies:
flutter:
sdk: flutter
flutter_localizations: # 新增
sdk: flutter # 新增
...
執(zhí)行 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,默認(rèn)使用MaterialApp中的locale設(shè)置

在pad中顯示的效果
4.showTimePicker,默認(rèn)使用MaterialApp中的locale設(shè)置

在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,
]
)
}
)