iamvivekkaushik / DatePickerTimelineFlutter

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

previous days #45

Open ersinceylan26 opened 3 years ago

ersinceylan26 commented 3 years ago

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

iamvivekkaushik commented 3 years ago

No, not with the current implementation.

EduardoAirton commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

`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 commented 2 years ago

`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 commented 2 years ago

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 commented 2 years ago

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

blackbeario commented 2 years ago

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 commented 2 years ago

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 commented 1 year ago

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