DatePicker 是一個(gè)日期組件,提供時(shí)間和日期的選擇。
在 Flutter 里沒有對(duì)應(yīng)的組件,而是提供兩個(gè)函數(shù):showDatePicker() 和 showTimePicker()。
Future<DateTime> showDatePicker ({
@required BuildContext context, // 上下文
@required DateTime initialDate, // 初始日期
@required DateTime firstDate, // 日期范圍,開始
@required DateTime lastDate, // 日期范圍,結(jié)尾
SelectableDayPredicate selectableDayPredicate,
DatePickerMode initialDatePickerMode: DatePickerMode.day,
Locale locale, // 國(guó)際化
TextDirection textDirection,
});
Future<TimeOfDay> showTimePicker({
@required BuildContext context,
@required TimeOfDay initialTime
});

new MaterialButton(
child: new Text('選擇日期'),
onPressed: () {
// 調(diào)用函數(shù)打開
showDatePicker(
context: context,
initialDate: new DateTime.now(),
firstDate: new DateTime.now().subtract(new Duration(days: 30)), // 減 30 天
lastDate: new DateTime.now().add(new Duration(days: 30)), // 加 30 天
).then((DateTime val) {
print(val); // 2018-07-12 00:00:00.000
}).catchError((err) {
print(err);
});
},
),

showDatePicker 打開的面板樣式是系統(tǒng)自帶的,在不同版本的 Android 上會(huì)有不同的體現(xiàn),在比較低版本的 Android 上,樣式比較難看,6.0 以上會(huì)好一點(diǎn)。
new MaterialButton(
child: new Text('選擇時(shí)間'),
onPressed: () {
showTimePicker(
context: context,
initialTime: new TimeOfDay.now(),
).then((val) {
print(val);
}).catchError((err) {
print(err);
});
},
)
showTimePicker 打開的面板樣式是系統(tǒng)自帶的,在不同版本的 Android 上會(huì)有不同的體現(xiàn),在比較低版本的 Android 上,樣式比較難看,6.0 以上會(huì)好一點(diǎn)。

注意了,DatePicker 默認(rèn)是英文說明的,就算手機(jī)設(shè)置為中文。如果需要是中文說明,則需要自己做 國(guó)際化 處理。