0

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.

https://i.stack.imgur.com/cSzE8.png

anaximander
  • 7,083
  • 3
  • 44
  • 62
anıl
  • 1

1 Answers1

0

In CupertinoDatePicker widget we have onDateTimeChanged method. you can update the string inside it. Here's an example

String fromDate = "";

 @override
void initState() {
  fromDate = DateTime.now().toString();
super.initState();
 }

 TextButton(onPressed: () {
  
        showModalBottomSheet(context: context, builder: (context) {
          return Column(
            children: [
              ListTile(
                leading: Text('Cancel'),
                trailing: Text('Done'),
              ),
              Expanded(
                child: CupertinoDatePicker(
                  mode: CupertinoDatePickerMode.date,
                  onDateTimeChanged: (value) {
                  
                   debugPrint('value: $value');
                   
                   //Update your string here
                  setState(() {
                    fromDate = value.toString();
                  });
                  
                },),
              )
            ],
          );
        },);
        
      }, child: Text(fromDate))
Aks
  • 405
  • 4
  • 6
  • Yes you can do that but since the text button is initialised already, the changes that you make with onDateTimeChange method will not have affects on the textButton. What I need is a textButton that changes dynamicly due to my date selections, or any alternative ways to do that. – anıl Aug 31 '23 at 10:12
  • What do you mean by textButton that changes dynamically? you need to show something else instead of textbutton? – Aks Aug 31 '23 at 11:19
  • What I mean by that is that, there is the date on the textButton. Which is todays date. When you press the button, CupertinoDatePicker shows up. I want to use the selected new date on the CupertinoDatePicker to instantly change the text on the textButton. So that the text in the textButton becomes the newly selected day with the CupertinoDatePicker. That was the thing I meant by dynamicly. – anıl Aug 31 '23 at 11:33
  • You tried the answer? This code is working as you describe, and please explain why the changes I made in onDateTimeChange method will not have affect on textButton? – Aks Aug 31 '23 at 11:34
  • well apparently I forgot to override the initState() on the previous try my fault. Now it works! Thank you very much. – anıl Aug 31 '23 at 11:48