samedi 19 octobre 2019

flutter exchange data between two viewmodels

I've got two Widgets which should communicate with each other. Both have a ViewModel connected.

The Calendar Widget:

Consumer<CalendarViewModel>(
    builder: (context, model, child) {
        model.generateEvents(mainModel.activities);

        return calendar(...);
    },
),
class CalendarViewModel extends ChangeNotifier {

}

The Data Widget:

BaseWidget<MonthDataViewModel>(
    model: MonthDataViewModel(
        visibleMonth: mainModel.displayedMonth,
    ),
    builder: (context, monthDataModel, child) => 
        Text('${DateFormat('MMMM').format(monthDataModel.visibleMonth)}',),

    ),
),
class MonthDataViewModel extends ChangeNotifier {
  DateTime _visibleMonth;

  MonthDataViewModel({
    @required DateTime visibleMonth,
  }) {
    _visibleMonth = visibleMonth;
  }

  DateTime get visibleMonth => _visibleMonth;
  set visibleMonth(DateTime month) => {
        _visibleMonth = month,
        notifyListeners(),
      };
}

So now there is a Function inside the calendar widget which fires, when the visible month has changed. I then want to change the text inside the MonthDataWidget based on the month name. The Problem here is, that I don't know how to connect the two viewmodels (or do i exchange data via the widgets itself?) without breaking the pattern

I also use the provider pattern if this could help here

Aucun commentaire:

Enregistrer un commentaire