iamvivekkaushik / DatePickerTimelineFlutter

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

Using `notifyListeners()` inside `onDateChange` not working #17

Closed psygo closed 4 years ago

psygo commented 4 years ago

Describe the bug

Whenever the user changes the calendar date, something should happen in my program. At least a class I created to store my screen's data should change. In order to do so, I'm calling a method that changes the internal data and then calling notifyListeners() to update other fields. However, if I use notifyListeners(), the selected date on the calendar doesn't change; the data does change, but not the selected rectangle on the screen.

To Reproduce

The code in my app is quite long, but I believe the following should suffice...

The DatePickerTimeline call:

DatePickerTimeline(
  DateTime.now(),
  onDateChange: (newDate){
    data.changeDay(date: newDate);
  }

The method to update the data is something like:

void changeDay({DateTime date}){
  this.day = date;
  notifyListeners();
}

Expected behavior

Why is the selected rectangle not being selected? I've also tried to refactor the code to use the Provider package instead of setState() but it didn't work either.

Desktop (please complete the following information):

Smartphone (please complete the following information):

psygo commented 4 years ago

I'm leaning more and more on blaming the interaction of setState and notifyListeners. After wrapping the Container from the DatePickerTimeline widget source code and replacing widget.currentDate by a Consumer<> with data.currentDate (and adding notifyListeners), everything seems to work fine.

iamvivekkaushik commented 4 years ago

I am not sure why this is happening. onDateChange is called after data has been changed which should change the background to respective date.

iamvivekkaushik commented 4 years ago

This seems to be working fine make sure you are implementing it correctly.