How to limit the timing that can be picked from Cupertino date picker in Flutter?

1.9k views Asked by At

Currently, this is how my Cupertino date picker look like

enter image description here

My code is as shown:

CupertinoDatePicker(
                      initialDateTime: DateTime.now(),
                      onDateTimeChanged: (val) {
                        setState(() {
                          dateSelected = val;
                        });
                      })

How can I make it such that I only allow the user to pick timing after 6 pm and before 3 am, every day?

2

There are 2 answers

1
Usama majid On BEST ANSWER

We cannot limit the timer. We can instead check if the current picked time is equal to our expectations. If not so show the user a dialog or a message to re enter the data and the current data entered is not available. Maybe this works for you.

0
Yakup Okumuş On

You can do it like this

              CupertinoDatePicker(
                  maximumDate:...,// give it DateTime 
                  minimumDate:...,// give it DateTime
                  initialDateTime: DateTime.now(),
                  onDateTimeChanged: (val) {
                    setState(() {
                      dateSelected = val;
                    });
                  })