I am trying to create an age field validator using the DateTimePickerFormField package in flutter. I can make the calendar appear but, I cannot dismiss it after selecting the date.

I have checked on youtube and flutter package site and I'm not sure where I went wrong here

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'),
        )
      ],
    ),

I want the user to select the date the comes up and then dismiss the keyboard while leaving the value in the field.


Solution 1: GirlWhoCode

You should create DateTime date; and set the value in OnChanged()
Hope it Works... =)

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