I want to display the text when I pick the date from showDatePicker, but now click the date is not going to update the text.
Only after I close and reopen the AlertDialog it will update but i want to update the value after I choose the date on showDatePicker and confirm it.
How can I do?
DateTime putdate = DateTime.now();
Future openDialog() => showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text(
'Choose Date',
style: TextStyle(fontWeight: FontWeight.bold, color: Colors.red),
),
content: SizedBox(
height: 280,
child:
Row(
children: [
IconButton(
icon: const Icon(Icons.calendar_month),
onPressed: () async {
DateTime? newDate = await showDatePicker(
context: context,
initialDate: putdate,
firstDate: DateTime(2000),
lastDate: DateTime(2100),
);
//if 'cancel' => null
if (newDate == null) {
return;
}
//if 'ok' => Datetime
setState(() => putdate = newDate);
},
),
Text(
'${putdate.year}/${putdate.month}/${putdate.day}',
style: const TextStyle(fontWeight: FontWeight.bold),
),
],
),
actions: [
TextButton(
child: const Text('cancel'),
onPressed: () => Navigator.of(context).pop(),
),
TextButton(
child: const Text('confirm'),
onPressed: () {
debugPrint('print');
setState(() {});
Navigator.of(context).pop();
}
},
),
],
),
);