Current date without focus
Opened this issue · 2 comments
Describe the bug
I am trying to create a calendar, initializing with DateTime.now().add(Duration(days: -7))
, but it is focusing on the first date and not the current date.
Expected behavior
I would like it to start with the current date, with the possibility for the user to select earlier dates by scrolling to the left.
Screenshots
It is initializing this way, with the focus of the dates 7 days ago.
I would like it to initialize with the focus on DateTime.now()
DatePicker(
DateTime.now().add(Duration(days: -7)),
locale: 'pt_BR',
initialSelectedDate: DateTime.now(),
selectionColor: Colors.black,
selectedTextColor: Colors.white,
controller: _controller,
onDateChange: (date) {
setState(() {
_selectDate = date;
});
},
),
Make a stateful widget and then in initState
function call DatePickerController
's animation methods such as animateToDate
, animateToSelection
, etc.
For example, the code snippet below initializes a date picker and then immediately animates to two days back (in order for the selected date to appear at center):
class _HorizontalDatePickerState extends ConsumerState<HorizontalDatePicker> {
final controller = DatePickerController();
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback(
(_) => controller.animateToDate(widget.selectedDate.subtract(Duration(days: 2))));
}
@override
Widget build(BuildContext context) {
return DatePicker(
widget.startDate,
initialSelectedDate: widget.selectedDate,
selectionColor: Theme.of(context).colorScheme.primary,
selectedTextColor: Colors.white,
locale: ref.watch(appLocaleProvider.notifier).getLocaleString(),
onDateChange: (date) => widget.onDateChange(date),
controller: controller,
);
}
}
did this solution work for you ?