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

previous days #45

Open
ersinceylan26 opened this issue Jan 15, 2021 · 10 comments
Open

previous days #45

ersinceylan26 opened this issue Jan 15, 2021 · 10 comments

Comments

@ersinceylan26
Copy link

Is it possible to implement that we can select the previous days by swiping right?

@iamvivekkaushik
Copy link
Owner

No, not with the current implementation.

@EduardoAirton
Copy link

actually, it is, the initial date you can set as 'DateTime.now().subtract(Duration(days: 30)),' this will show the last month, the initialSelectedDate you set as DateTime.now(), in the jumpToSelection function you add this line _datePickerState._currentDate = DateTime.now(); and add the function to your init state _controller.jumpToSelection(context), its gonna show to last 30 days that you can select but it always gonna start with today selected.

@EduardoAirton
Copy link

DatePicker( DateTime.now().subtract(Duration(days: 30)), height: height * 0.1, width: width * 0.19, controller: _controller, initialSelectedDate: DateTime.now(), locale: "pt-BR", onDateChange: (date) { setState(() { print("Data selecionada $date"); _selectedValue = date; }); }, ),

@EduardoAirton
Copy link

`void jumpToSelection(var context) {
assert(_datePickerState != null,
'DatePickerController is not attached to any DatePicker View.');

// jump to the current Date
_datePickerState._currentDate = DateTime.now();
_datePickerState._controller.jumpTo(
    _calculateDateOffset(_datePickerState._currentDate) -
        MediaQuery.of(context).size.width * 0.05);

}`

@snehamadda
Copy link

snehamadda commented Jul 15, 2021

`void jumpToSelection(var context) {
assert(_datePickerState != null,
'DatePickerController is not attached to any DatePicker View.');

// jump to the current Date
_datePickerState._currentDate = DateTime.now();
_datePickerState._controller.jumpTo(
    _calculateDateOffset(_datePickerState._currentDate) -
        MediaQuery.of(context).size.width * 0.05);

}`

is it working for you? if yes then what could be inside this method _calculateDateOffset?
and how to enable previous month days?

@EduardoAirton
Copy link

EduardoAirton commented Jul 15, 2021

Yeah, it's working fine!!


double _calculateDateOffset(DateTime date) {
    final startDate = new DateTime(
        _datePickerState.widget.startDate.year,
        _datePickerState.widget.startDate.month,
        _datePickerState.widget.startDate.day);
    int offset = date.difference(startDate).inDays;
    return (offset * _datePickerState.widget.width) + (offset * 6);
  }

@pedromorgan
Copy link

Seems strange that it don't have a minimum date and a strange workaround instead ..

@blackbeario
Copy link

Thanks for the help on this @EduardoAirton! I found that if you don't add WidgetsBinding in initState, you will get the "DatePickerController is not attached to any DatePicker View" assertion error from the jumpToSelection() method because the DatePicker hasn't been added to the widget tree yet.

@override
  void initState() {
    super.initState();
    WidgetsBinding.instance!.addPostFrameCallback((_) => 
      _datePickerController.jumpToSelection(context));
  }

With null-safety:

void jumpToSelection(context) {
    assert(_datePickerState != null,
        'DatePickerController is not attached to any DatePicker View.');

    // jump to the current Date
    _datePickerState?._currentDate = DateTime.now();
    _datePickerState?._controller.jumpTo(
    _calculateDateOffset(_datePickerState!._currentDate!) -
        MediaQuery.of(context).size.width * 0.05);
  }

@dosjota
Copy link

dosjota commented Mar 29, 2022

Is it not possible to add a start and end date?
DatePicker( startDate: DateTime(2020, 07, 01), endDate: DateTime(2020, 12, 30), initialSelectedDate: DateTime(2020, 07, 24), onDateChange: (dateTime) { print(dateTime); }, )

@jbryanh
Copy link

jbryanh commented Aug 10, 2022

Hoping to clarify some of this.

  1. Example of a DatePicker that scrolls backwards 365 days and initial selected date is yesterday:

DatePicker( DateTime.now().subtract(const Duration(days: 365)), controller: _datePickerController, initialSelectedDate: DateTime.now().subtract(const Duration(days: 1)), selectionColor: Grid.green.withOpacity(.6), selectedTextColor: Colors.white, onDateChange: (date) { // New date selected setState(() { _selectedValue = date; }); }, ),

  1. At initial build, jump to your selected date:
    @override void initState() { super.initState(); WidgetsBinding.instance.addPostFrameCallback((timeStamp) { _datePickerController.animateToSelection(); }); }

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

8 participants