I have a Form that contains some fields, one of them is a date field:

  onTap: ()  {
  child: AbsorbPointer(
    child: TextFormField(
      initialValue: dataEvento,
      decoration: InputDecoration(
          labelText: 'Date ${dataEvento}',
          labelStyle: TextStyle(
              fontFamily: 'acumin-pro',
              MediaQuery.of(context).size.height * 0.022,
              fontWeight: FontWeight.bold,
              color: Colors.grey),
          focusedBorder: UnderlineInputBorder(
              borderSide: BorderSide(color: dataUserGlobal.yellow))),
      style: TextStyle(color: dataUserGlobal.lightBlue),
      validator: (value) =>
      value.isEmpty ? 'Campo obbligatorio' : null,
      onSaved: (value) => dataEvento = value,

When I tap on that field I call this function

DateTime selectedDate = DateTime.now();
Future selectDate(BuildContext context) async {
  DateTime picked = await showDatePicker(
      context: context,
      initialDate: selectedDate,
      firstDate: DateTime(2015, 8),
      lastDate: DateTime(2101));
  if (picked != null && picked != selectedDate)
    setState(() {
      dataEvento = DateFormat('yyyy-MM-dd').format(picked);

but I can't change the value inside the field.

For debugging it I try to insert the name of the variable inside my label text labelText: 'Date ${dataEvento}' and this shows me the date when I click on the field and chose the date. But the field initialValue remains blank.

What I'm doing wrong?

1 Answers

Outreagous ONE On Best Solutions

What about adding a controller to your textFormField like this :

TextEditingController _txtFormCtrl = TextEditingController();

, and this is a function that can help you :

 void _changeDatetime(int year, int month, int date) {
    setState(() {
      _year = year;
      _month = month;
      _date = date;
      _datetime = '$year-$month-$date';
      _txtFormCtrl.text = _datetime;