2

I am able to set the minimum date for the widget, however, I don't want the user to be able to select a time in the past. How do I ensure this? CupertinoDatePicker.

Sidenote: How do I make the '00' and '30' appear only once in the minutes wheel? Below is some relevant code snippets.

final minDate = DateTime.now();
final difference = 30 - minDate.minute % 30;
final initialDate = minDate.add(Duration(minutes: difference));

Stack(
  children: <Widget>[
   CupertinoDatePicker(
     minimumDate: minDate,
     maximumDate: minDate.add(Duration(days: 7)),
     initialDateTime: initialDate,
     minuteInterval: 30,
     onDateTimeChanged: (dateTime) {
        // Do stuff                  
     },
   ),
   Align(
     alignment: Alignment.topRight,
     child: CupertinoButton(
       child: Text("Done"),
       onPressed: () {
          Navigator.of(context).pop();
       }),
     ),
  ],
),
kaustubnvd
  • 41
  • 1
  • 3

2 Answers2

0

While you cannot limit the time the user picks by default, only the date. You can add validation code in your onDateTimeChanged block and prevent this from happening. It's not a great workaround, but it will ensure you don't get a time in the past with minimum efforts.

nanibir
  • 234
  • 1
  • 9
0

just do

DateTime.now().subtract(Duration(days:1