1

Currently, implementing a default widget CupertinoDatePicker in my flutter app. In this date picker i want the feature that user should not be able to select the day before today. User should be able to select only the future and today's dates.

So, i am passing "minimumDate: DateTime.now()" property properly, but while the CupertinoDatePicker gets opened it shows me one date ahead of today. I have checked the value of "DateTime.now()" property. It is showing current time properly but getting issue from CupertinoDatePicker widget.

CupertinoDatePicker(
                    minimumDate: DateTime.now(),
                    minuteInterval: 1,
                    mode: CupertinoDatePickerMode.dateAndTime,
                    onDateTimeChanged: (DateTime dateTime) {
                      print("dateTime: ${dateTime}");
                    },
)

Also, applied "flutter clean" command. Still facing the same issue.

Can anyone suggest a working solution?

Thanks.

Jay Mungara
  • 6,663
  • 2
  • 27
  • 49

1 Answers1

5

Quick Fix,

  @override
  Widget build(BuildContext context) {
    var now = DateTime.now();
    var today= new DateTime(now.year, now.month, now.day);

then pass today into minimumDate

CupertinoDatePicker(
                    minimumDate: today,
                    minuteInterval: 1,
                    mode: CupertinoDatePickerMode.dateAndTime,
                    onDateTimeChanged: (DateTime dateTime) {
                      print("dateTime: ${dateTime}");
                    },
                  ),

EDIT

Or you can use below code as suggested by @Leonard Arnold in comment

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

Output

enter image description here

Ravinder Kumar
  • 7,407
  • 3
  • 28
  • 54
  • 1
    or in one line: `minimumDate: DateTime.now().subtract(Duration(days: 1))` i think the Problem is that DateTime.now() uses also the time - so the widget can't handle a minimum time but minimum date. Thats why Ravinder's solution works and mine also :) – Leonard Arnold Dec 10 '19 at 11:53
  • Yes, I found that too. the time returned by `DateTime.now()` is the real culprit. – Ravinder Kumar Dec 10 '19 at 12:01