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>[
                      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) {
          onPressed: () {
            if (_formKey.currentState.validate()) {
            } 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) {
                          }, */ 
      onChanged: (dt) =>
                   setState(() => date = dt),