iamvivekkaushik / DatePickerTimelineFlutter

Flutter Date Picker Library that provides a calendar as a horizontal timeline
Apache License 2.0
282 stars 198 forks source link

why don't you do focus center when date clicked? #65

Open gamcho3 opened 2 years ago

gamcho3 commented 2 years ago

Is your feature request related to a problem? Please describe. I want to focus center when date clicked and focus initialSelectedDate when entered page.

alierdogan7 commented 1 year ago

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,
    );
  }
}