Is it possible to do inter widget communication via something like a notification/event bus?
I need to be able to tell one widget to react to something that happened in another and didn't want to create a hard link.
The notification listener will only fire is if it is higher in the widget tree than both of the widgets so that isn't probably a viable solution.
Widgets can communicate with each other in two ways: Communication Through Events. Communication Through URL.
There are lots of ways to do this depending on your use case.
You could have them be AnimatedWidget
s that are passed a ValueNotifier
or ChangeNotifier
as the listenable
. You can see this pattern in the Gallery's animation example.
You could use StreamBuilder
to have your widgets rebuild automatically when new events come in on a Stream
. There aren't a lot of examples of this in the main Flutter repo, but it's something that you're likely to need once you start using plugins or doing network I/O.
You could use a GlobalKey
to get currentState
and have one State
call methods on the other. This is how snackbars work (example).
You can also extend InheritedWidget
to provide widgets with information that wasn't passed as a constructor argument, and they'll automatically be marked for rebuild when that information changes. This is how Theme
s work, for example.
If you can provide more details on what your widgets do / what their relationship is, or ideally a code snippet I can help you decide which approach would make the most sense for your situation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With