flutterdartdatepickerflutter-layoutflutter-dialog

Dialog not updating when using date picker flutter


I have a dialog which has the datePicker inside but when I select the date the Text("${selectedDate.toLocal()}".split(' ')[0]), is not updating. How can I solve this?. I have try so many solution but it’s not working

code:

 Future birthday(widgets) => showDialog(
  context: context,
  builder: (context) {
    return StatefulBuilder(
      builder: (context, StateSetter setState) {
        return AlertDialog(
         title: Text("${selectedDate.toLocal()}".split(' ')[0]),
          content: SizedBox(
              child: Column(
                children: [
                  InkWell(
                    child: Row(
                        children: const [
                          Text(
                            '- Select date',
                            style: TextStyle(fontSize: 16),
                          ),
                        ]),
                    onTap: () => selectDate(context),
                  ),
                ],
              ),
            ),
          actions: [
              OutlinedButton(
                onPressed: () async => {},
                style: OutlinedButton.styleFrom(
                  side: const BorderSide(width: 5.0, color: Colors.transparent),
                ),
                child: const Text(
                  'Confirm',
                  style: TextStyle(color: Colors.grey, fontSize: 16.0),
                ),
              ),
            ],
        );

  selectDate(BuildContext context) async {
    final DateTime? picked = await showDatePicker(
        context: context,
        initialDate: selectedDate,
        firstDate: DateTime(1000, 1),
        lastDate: DateTime(2023, 1));
    if (picked != null && picked != selectedDate) {
      setState(() {
        selectedDate = picked;
      });
    }
  }

Solution

  • I think current setState is getting from state class. Try with passing StatefulBuilder's setState on selectDate

     onTap: () => selectDate(context, setState),
    

    And receiving as positional argument

    selectDate(BuildContext context, setState) async {