Flutter 中避免不了使用日期、时间选择器,这里咱们使用官方showDatePicker
与showTimePicker
。ui
要想使得这两个组件为中文的先决条件为项目加入国际化(flutter_localizations
)。code
在/pubspec.yaml
中:ci
... dependencies: flutter: sdk: flutter flutter_localizations: # 添加 sdk: flutter # 添加 ...
/lib/main.dart
中:get
import 'package:flutter_localizations/flutter_localizations.dart'; // 添加 // MaterialApp中加入 ... localizationsDelegates: [ GlobalMeterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, ], supportedLocales: [ const Locale('zh', 'CH'), const Locale('en', 'US'), ], locale: const locale('zh'), ...
默认使用MaterialApp
设置中的。 showDatePicker
提供了locale
参数,若是有须要能够继续更改。it
默认使用MaterialApp
设置中的。 showTimePicker
没有提供locale
参数,若是想改变语言可使用:io
await showTimePicker( context: context, initialTime: TimeOfDay.now(), builder: (BuildContext context, Widget child) { return Localizations( locale: const Locale('zh'), child: child, delegates: <LocalizationsDelegate>[ GlobalMeterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, ] ) } )