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

Current date without focus #78

Open
otaldopira opened this issue Apr 4, 2023 · 2 comments
Open

Current date without focus #78

otaldopira opened this issue Apr 4, 2023 · 2 comments

Comments

@otaldopira
Copy link

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.

image

I would like it to initialize with the focus on DateTime.now()

image

Animação

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

@juanpfloreez
Copy link

did this solution work for you ?

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

3 participants