iamvivekkaushik/DatePickerTimelineFlutter

Jump back to today's date

Opened this issue · 4 comments

I was wondering if we can add a button and on tap that button can make the selected date back to Today's date?
Or if this feature is already available? How can I implement that?

Vivek many thanks for the package! very useful!!

you can attach a controller controller: _datePickercontroller,, then animate to the date you want _datePickercontroller.animateToDate(date);

@chrisvidal I need this too.

I've find out and tried those already but, it's not selecting it in ui while animating to it.

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

I've already solved the issue with a very similar technique, my comment is much about if a library or package contains a feature it should work as expected on all scenarios and that is missing in this package. @alierdogan7 Thanks by the way for your comment.