Flutter – CupertinoDatePicker showing one day ahead of the today?

Issue

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.

Solution

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

Answered By – Ravinder Kumar

Answer Checked By – Katrina (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.