The current date is already written in the text button. When you press it, a CupertinoDatePicker pops up, but I want to change the written date instantly when I change the date in CupertinoDatePicker. With normal date picker it is possible but after a few hours of work I couldn’t figure out how to do that with CupertinoDatePicker.
2
Answers
In CupertinoDatePicker widget we have onDateTimeChanged method.
you can update the string inside it.
Here’s an example
The
CupertinoDatePicker
in Flutter’s Cupertino library provides a way to select dates in an iOS-like style. However, unlike theDatePicker
widget from the Material library,CupertinoDatePicker
does not automatically update the date being displayed in the associated widget.To achieve the behavior you’re looking for – updating the displayed date instantly as the user interacts with the
CupertinoDatePicker
, you can use theValueChanged
callback provided by theCupertinoDatePicker
to update the displayed date as the user changes the date in the picker.Here’s how you can achieve this:
In this example, the
selectedDate
variable is used to hold the currently selected date. When the user changes the date in theCupertinoDatePicker
, theonDateTimeChanged
callback updates theselectedDate
value, and the displayed date in the UI gets updated automatically due to the use ofsetState
.With this approach, the date displayed on the button will instantly change as the user interacts with the
CupertinoDatePicker
.