DateTImePickerFormField 在选择日期后不关闭键盘

DateTImePickerFormField Does not dismiss keyboard after date selected

我正在尝试在 flutter 中使用 DateTimePickerFormField 包创建一个年龄字段验证器。我可以显示日历,但是我无法在 select 日期后关闭它。

我已经查看了 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'),
        )
      ],
    ),

我希望用户 select 出现的日期然后关闭键盘,同时将值留在字段中。

您应该创建 DateTime 日期;并在 OnChanged()
中设置值 希望它有效... =)

 // Instead of onSaved 
                        /*  onSaved: (value) {
                            debugPrint(value.toString());
                            print(value.toString());
                          }, */ 
//USE 
      onChanged: (dt) =>
                   setState(() => date = dt),