0

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

1 Answer 1

1

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 {
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.