简介
运行选择一年的可滚动年份列表
- 年份选择器widget很少使用,相反,请考虑使用showDatePicker,它会创建一个日期选择器对话框
基本用法
- 使用firstData,设置用户选择的最早日期
- 使用lastData,设置用户可选择的最晚日期
- 使用selectedDate,设置当前选择的日期
实例演示
import 'dart:async';
import 'package:flutter/material.dart';
class YearPickerDemo extends StatefulWidget {
@override
State<StatefulWidget> createState() => _YearPickerDemoState();
}
class _YearPickerDemoState extends State<YearPickerDemo> {
DateTime _date = DateTime.now();
Future<void> _selectDate(BuildContext context) async {
final DateTime picked = await showDatePicker(
context: context,
initialDate: _date,
firstDate: DateTime(2015, 8),
lastDate: DateTime(2101));
if (picked != null && picked != _date)
print("data selectied :${_date.toString()}");
setState(() {
_date = picked;
});
if (picked == null) _date = DateTime.now();
}
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Text('日期选择'),
RaisedButton(
child: Text('date selected:${_date.toString()}'),
onPressed: () {
_selectDate(context);
},
),
],
);
}
}