Open ersinceylan26 opened 3 years ago
No, not with the current implementation.
actually, it is, the initial date you can set as 'DateTime.now().subtract(Duration(days: 30)),' this will show the last month, the initialSelectedDate you set as DateTime.now(), in the jumpToSelection function you add this line _datePickerState._currentDate = DateTime.now(); and add the function to your init state _controller.jumpToSelection(context), its gonna show to last 30 days that you can select but it always gonna start with today selected.
DatePicker( DateTime.now().subtract(Duration(days: 30)), height: height * 0.1, width: width * 0.19, controller: _controller, initialSelectedDate: DateTime.now(), locale: "pt-BR", onDateChange: (date) { setState(() { print("Data selecionada $date"); _selectedValue = date; }); }, ),
`void jumpToSelection(var context) { assert(_datePickerState != null, 'DatePickerController is not attached to any DatePicker View.');
// jump to the current Date
_datePickerState._currentDate = DateTime.now();
_datePickerState._controller.jumpTo(
_calculateDateOffset(_datePickerState._currentDate) -
MediaQuery.of(context).size.width * 0.05);
}`
`void jumpToSelection(var context) { assert(_datePickerState != null, 'DatePickerController is not attached to any DatePicker View.');
// jump to the current Date _datePickerState._currentDate = DateTime.now(); _datePickerState._controller.jumpTo( _calculateDateOffset(_datePickerState._currentDate) - MediaQuery.of(context).size.width * 0.05);
}`
is it working for you? if yes then what could be inside this method _calculateDateOffset
?
and how to enable previous month days?
Yeah, it's working fine!!
double _calculateDateOffset(DateTime date) {
final startDate = new DateTime(
_datePickerState.widget.startDate.year,
_datePickerState.widget.startDate.month,
_datePickerState.widget.startDate.day);
int offset = date.difference(startDate).inDays;
return (offset * _datePickerState.widget.width) + (offset * 6);
}
Seems strange that it don't have a minimum date and a strange workaround instead ..
Thanks for the help on this @EduardoAirton! I found that if you don't add WidgetsBinding in initState, you will get the "DatePickerController is not attached to any DatePicker View" assertion error from the jumpToSelection() method because the DatePicker hasn't been added to the widget tree yet.
@override
void initState() {
super.initState();
WidgetsBinding.instance!.addPostFrameCallback((_) =>
_datePickerController.jumpToSelection(context));
}
With null-safety:
void jumpToSelection(context) {
assert(_datePickerState != null,
'DatePickerController is not attached to any DatePicker View.');
// jump to the current Date
_datePickerState?._currentDate = DateTime.now();
_datePickerState?._controller.jumpTo(
_calculateDateOffset(_datePickerState!._currentDate!) -
MediaQuery.of(context).size.width * 0.05);
}
Is it not possible to add a start and end date?
DatePicker( startDate: DateTime(2020, 07, 01), endDate: DateTime(2020, 12, 30), initialSelectedDate: DateTime(2020, 07, 24), onDateChange: (dateTime) { print(dateTime); }, )
Hoping to clarify some of this.
DatePicker( DateTime.now().subtract(const Duration(days: 365)), controller: _datePickerController, initialSelectedDate: DateTime.now().subtract(const Duration(days: 1)), selectionColor: Grid.green.withOpacity(.6), selectedTextColor: Colors.white, onDateChange: (date) { // New date selected setState(() { _selectedValue = date; }); }, ),
@override void initState() { super.initState(); WidgetsBinding.instance.addPostFrameCallback((timeStamp) { _datePickerController.animateToSelection(); }); }
Is it possible to implement that we can select the previous days by swiping right?