Inter widget

Is it possible to interact between widgets through something like a notification / event bus?

I need one widget to be able to respond to what happened in another and not want to create a hard link.

The notification listener will only fire if it is larger in the widget tree than both widgets, which is hardly a viable solution.

+9
source share
2 answers

There are many ways to do this, depending on your use case.

You could make them be AnimatedWidget , which are passed to the ValueNotifier or ChangeNotifier as listenable . This template can be seen in the example gallery.

You can use StreamBuilder so that your widgets are automatically rebuilt when new events appear on Stream . There are not many examples in the main Flutter repositories, but this is what you will probably need when you start using plugins or do network I / O.

You can use GlobalKey to get currentState and have one method of calling State - on the other. This is how snackbars ( example ) work.

You can also extend InheritedWidget to provide widgets with information that was not passed as an argument to the constructor, and they will automatically be marked for rebuilding when this information changes. For example, Theme .

If you can provide more details on what your widgets do / what their relationships are, or, ideally, a piece of code, I can help you decide which approach will make the most sense for your situation.

+14
source

Source: https://habr.com/ru/post/1268439/


All Articles