Flutter: Disable picking the day from datePicker - flutter

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.

Related

How do I define the end and start date in date picker(flutter)?

I have built a custome scrollable date picker in the flutter and i dont know how to limit the display of the dateenter image description herecode of date pickerDate picker
I want the program to diplay upto 7 date from the current date.
There is no need for a custom Date Picker, just use the built-in one:
DateTime? dateTime = showDatePicker(
initialDate: DateTime.now(),
firstDate: DateTime.now(),
lastDate: DateTime.now().add(Duration(days: 7)),
(...)
Here, you can set the bouds yourself. This is an example for the time range now-in 7 days

Flutter Date Range Picker limit to certain days

How can we allow only selection of 30 days with any start date in DateRangePicker?
Only firstDate and lastDate can be provided, which can be restricted to 30 days starting from the provided firstDate only.
final picked = await showDateRangePicker(
context: context,
firstDate: (DateTime.now()).subtract(Duration(days: 30)),
lastDate: DateTime.now(),
currentDate: DateTime.now(),
saveText: 'Done',
);

How to make user select only 1st of every month in datepicker in flutter?

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,
);
}
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...
I changed the initial date to 1st of the month. It's working

how to disable past day on table calendar flutter

i use package table calendar, how to disable table calendar past day on flutter
if on week view hide all past day
on mont view grey all past day on this month and disable to past month
enter image description here
showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime.now(),
lastDate: DateTime(DateTime.now().year + 5),
),

Flutter showDatePicker show year selection first

In Flutter, I use showDatePicker method to show the date picker, but how to open date picker that show/open the year selection by default, before going to select month & date?
Set the property initialDatePickerMode to DatePickerMode.year (doc)
Code Sample:
showDatePicker(
context: context,
initialDatePickerMode: DatePickerMode.year,
initialDate: initialDate,
firstDate: DateTime(firstDate),
lastDate: DateTime(lastDate),
);