skip to Main Content

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;
                          });
                        },

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search