Home > Mobile >  Can't change date with DatePicker in Flutter
Can't change date with DatePicker in Flutter

Time:09-27

  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?

CodePudding user response:

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;
  });
  • Related