Flutter: Disable picking the day from datePicker

874 views Asked by At

I'm trying to create a month/year picker in the Flutter project for both material and Cupertino styles. However, I can't find an ability to disable the days on the default date picker and the available packages only offer Cupertino-styled month pickers.

Did anyone face this kind of problem, and what was the most optimal solution? Any help is appreciated.

1

There are 1 answers

1
Gwhyyy On

You need to use the selectableDayPredicate to control which DateTime should be enabled or disabled with a bool condition in your date picker :

      showDatePicker(
              context: context,
              initialDate: DateTime.now(),
              firstDate: DateTime(2022, 12, 10),
              lastDate: DateTime(2022, 12, 30),
              selectableDayPredicate: (date) {
                return date.day != 27;
              },
            )

In this example, the date picker will enable all days between the 10 and 30 of this month except the day 27, it will be disabled, preview :

prove of disabling a specific day

you can expand with your personal logic into this.