Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jump back to today's date #48

Open
vpxoxo opened this issue Feb 20, 2021 · 4 comments
Open

Jump back to today's date #48

vpxoxo opened this issue Feb 20, 2021 · 4 comments

Comments

@vpxoxo
Copy link

vpxoxo commented Feb 20, 2021

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!!

@chrisvidal
Copy link

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

@codewithmustafa
Copy link

@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.

@alierdogan7
Copy link

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

@codewithmustafa
Copy link

codewithmustafa commented Jun 21, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants