当用户单击日历中的“取消”按钮时,我想使变量为null。我得到了确定按钮的动作。但我不知道如何执行取消操作
下面是我的代码
Future<Null> selectDate(BuildContext context) async {
final DateTime picked = await showDatePicker(
context: context,
initialDate: selectedDate,
firstDate: DateTime(2015, 8),
lastDate: DateTime(2101),
);
if (picked != null && picked != selectedDate) {
selectedDate = picked;
}
formattedDate = DateFormat('yyyy-MM-dd').format(selectedDate);
print('selected date is '+ "$formattedDate");
setState(() {});
}
答案 0 :(得分:0)
showDatePicker将选定的日期返回给您。因此,如果单击“取消”按钮,它将返回null。
如果您想取消控制,您可以这样做
final DateTime picked = await showDatePicker(
context: context,
initialDate: selectedDate,
firstDate: DateTime(2015, 8),
lastDate: DateTime(2101),
);
if(picked == null){
print("Hi bro, i came from cancel button or via click outside of datepicker");
}