简体   繁体   中英

Flutter: Disable picking the day from datePicker

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.

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:

证明禁用特定日期

you can expand with your personal logic into this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM