anıl
anıl

Reputation: 1

Is there a way to change the written date using Cupertino date picker?

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.sstatic.net/cSzE8.png

Upvotes: 0

Views: 222

Answers (1)

Aks
Aks

Reputation: 1671

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

Upvotes: 0

Related Questions