skip to Main Content

I want the user to select only 1st of every month when they click on the date picker. Currently, I’m able to show the date picker to select a date only if the current date is 1st using selectableDayPredicate if the current date is not the 1st of that month the date picker will not open. I want the date picker to open even if the current date is not the 1st of that month and all the other dates should be disabled and can select a different month’s 1st date.

Here is the code:

  Future<void> _selectDate(BuildContext context) async {
    final DateTime? picked = await showDatePicker(
      context: context,
      initialDate: selectedDate,
      firstDate: DateTime(2015, 8),
      lastDate: DateTime(2101),
      selectableDayPredicate: (day) => day.day == 1 ? true : false,
    );
  }

2

Answers


  1. Chosen as BEST ANSWER

    I changed the initial date to 1st of the month. It's working


  2. here is the code that you need:

    
    final DateTime? picked = await showDatePicker
    (
          context: context,
          initialDate: DateTime(DateTime.now().year,DateTime.now().month,1),
          firstDate: DateTime(2015, 8),
          lastDate: DateTime(2101),
          selectableDayPredicate: (day) => day.day == 1 ? true : false,
    );
    
    

    you should initialDate to the first day of every month and because of this it selects the first day of the month that you are in. and your conditions works in this way because you are sure that you are not disabling the first days that you have initialized.

    happy coding…

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