Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't change date with DatePicker in Flutter

  MyInputField(
            title: "Tarih",
            hint: DateFormat('dd/MM/yyyy').format(_selectedDate),
            widget: IconButton(
                onPressed: () {
                  _getDateFromUser();
                },

Date data I show in TextFormField.

  _getDateFromUser() async {
DateTime? _pickerDate = await showDatePicker(
    context: context,
    initialDate: DateTime.now(),
    firstDate: DateTime(2000),
    lastDate: DateTime(2030));

if (_pickerDate != null) {
  setState(() {
    _pickerDate = _selectedDate;
  });
}
 }
}

The _getDateFromUser() function I defined

When I click on the icon to change the date, the calendar appears. But the date I changed doesn’t change in TextFormField. DateFormat(‘dd/MM/yyyy’) Is it because of this structure?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Turn the variables around. You want to assign the pickerDate to the selectedDate, not the other way around.

setState(() {
    _pickerDate = _selectedDate;
  });

to this:

setState(() {
    _selectedDate = _pickerDate;
  });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading