DateTImePickerFormField选择日期后不关闭键盘

时间:2019-04-18 20:20:23

标签: flutter widget datetimepicker

我试图在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'),
        )
      ],
    ),

我希望用户选择显示日期,然后关闭键盘,同时将值保留在字段中。

1 个答案:

答案 0 :(得分:2)

您应该创建DateTime日期;并在OnChanged()中设置值
希望它能起作用... =)

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