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

问题描述 投票:0回答:1

我正在尝试使用flutter中的DateTimePickerFormField包创建一个age field验证器。我可以让日历显示但是,我在选择日期后无法忽略它。

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

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

flutter widget datetimepicker
1个回答
2
投票

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

 // Instead of onSaved 
                        /*  onSaved: (value) {
                            debugPrint(value.toString());
                            print(value.toString());
                          }, */ 
//USE 
      onChanged: (dt) =>
                   setState(() => date = dt),
© www.soinside.com 2019 - 2024. All rights reserved.