我希望程序只要求用户输入年份。但如您所知,日期选择器要求提供完整的日期,包括月份和日期。有没有办法让用户只被问到这一年?
最佳答案
Afaik,您不能使用 datePicker 仅选择年份。但是你可以通过使用对话框和 YearPicker 来做到这一点。小部件。
这是一个示例代码,用于显示带有 YearPicker 的对话框(阅读评论):
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Select Year"),
content: Container( // Need to use container to add size constraint.
width: 300,
height: 300,
child: YearPicker(
firstDate: DateTime(DateTime.now().year - 100, 1),
lastDate: DateTime(DateTime.now().year + 100, 1),
initialDate: DateTime.now(),
// save the selected date to _selectedDate DateTime variable.
// It's used to set the previous selected date when
// re-showing the dialog.
selectedDate: _selectedDate,
onChanged: (DateTime dateTime) {
// close the dialog when year is selected.
Navigator.pop(context);
// Do something with the dateTime selected.
// Remember that you need to use dateTime.year to get the year
},
),
),
);
},
);
关于android - 如何仅在 Flutter 的 datepicker 中显示年份?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62022135/