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,
Related
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',
);
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'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.
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.