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',
);
Related
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
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),
),
I want to disable the current date in showDatePicker. I was able to disable the past date in the showDatePicker but i also want to disable the current date in showDatePicker
here is my code:
showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime.now()
.subtract(Duration(days: 0)),
lastDate: DateTime(2025),
// selectableDayPredicate:
// _decideWhichDayToEnable,
);
i have updated, user can still press the 24 but i want it to be disable..
intialDate property is the key here.
DateTime intialDate = DateTime.now().add(Duration(days: 1));
showDatePicker(
context: context,
initialDate: intialDate,
initialDatePickerMode: DatePickerMode.day,
firstDate: intialDate,
lastDate: DateTime(2025));
This is the output am getting.
From your code what I understand is that you have to set Tomorrow as the firstDate and inialDate. You just need to set those values like DateTime.now().add(const Duration(days: 1)). I hope that will work.
I used DateTimeField to get data control. Here I want user to select date in financial year only:
ex:
Today date: 11 March 2020
so this case user can select from 1 April 2019 to till date.
I tried it but failed I able to restrict for last one year.
like for above example user can select from 11 March 2019 to till date.
DateTime(DateTime.now().year - 1, DateTime.now().month,DateTime.now().day)
My onShowPicker looks like:
onShowPicker: (context, currentValue) {
return showDatePicker(
context: context,
firstDate: DateTime(DateTime.now().year - 1, DateTime.now().month,
DateTime.now().day),
initialDate: currentValue ?? DateTime.now(),
lastDate: DateTime.now(),
);
},
Any hint will be helpful.
How about this?
var now = DateTime.now();
var minus = 0;
if (now.month <= 3) {
minus = 1;
}
var firstDate = DateTime(DateTime.now().year - minus, 4, 1);
and
firstDate: firstDate,
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),
);