Home > Mobile >  Flutter : How to disable dates in datepicker in flutter?
Flutter : How to disable dates in datepicker in flutter?

Time:11-30

I want to disable the dates in the datepicker in flutter. Actually I am wokring on an application where user has to generate salary but I don't want him to generate salary if the salary is already created.

suppose salary is created 1-10-2022 to 30-10-2022 then i want to disable all the previous dates from 30-10-2022...How can i do this ??

enter image description here

 onTap: () async {
                          await showDatePicker(
                            context: context,
                            initialDate: DateTime.now(),
                            firstDate: DateTime(2000),
                            lastDate: DateTime(3000),
                          ).then((selectedDate) {
                            if (selectedDate != null) {
                              _startDatePickerController.text =
                                  DateFormat('d-MM-y')
                                      .format(selectedDate)
                                      .toString();
                            }
                            return null;
                          });
                        },

CodePudding user response:

Try setting that date in the firstDate property, so previous dates than that will be disabled:

    firstDate: DateTime(2022, 10, 30),
    // ...

this will give you the idea, make a variable and then manage it with your logic.

CodePudding user response:

I think your question is already answered here:

https://stackoverflow.com/a/53657221/5812524

Create a list of dates which you want to disable and check inside

selectableDayPredicate: (DateTime val) 

if the date is inside your list.

  • Related