我正在尝试使用 flutter 中的 DateTimePickerFormField 包创建一个年龄字段验证器。我可以显示日历,但是在选择日期后我无法关闭它。
我已经查看了 youtube 和 flutter 包网站,我不确定我哪里出错了
Widget _buildDateField(BuildContext context) {
final _ageFocus = FocusNode();
final dateFormat = DateFormat("mm-dd-yyyy");
var _formKey = GlobalKey<FormState>();
return Container(
width: 120,
child: Column(
children: <Widget>[
Form(
key: _formKey,
child: Container(
child: DateTimePickerFormField(
dateOnly: true,
format: dateFormat,
validator: (val) {
if (val != null) {
return null;
} else {
return 'Date Field is Empty';
}
},
decoration: InputDecoration(
hintText: 'Age', icon: Icon(Icons.calendar_today)),
style: TextStyle(fontWeight: FontWeight.bold),
initialDate: DateTime.now(),
onSaved: (value) {
debugPrint(value.toString());
print(value.toString());
},
),
),
),
RaisedButton(
onPressed: () {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
} else {
}
},
child: Text('Submit'),
)
],
),
我希望用户选择出现的日期,然后在将值保留在字段中的同时关闭键盘。
最佳答案
您应该创建 DateTime 日期;并在 OnChanged()
中设置值
希望它有效... =)
// Instead of onSaved
/* onSaved: (value) {
debugPrint(value.toString());
print(value.toString());
}, */
//USE
onChanged: (dt) =>
setState(() => date = dt),
关于flutter - DateTImePickerFormField 在选择日期后不关闭键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55753215/