Inherited widgets – injecting the state at the root
We observed that by using setState
functionality we can tell the code which values to update. We also found out how to pass down the data within the widgets through constructors. This technique can turn into a real mess when you have a very deep widget hierarchy. You will have to pass down your counter value to every widget in order to be able to make it accessible by the deepest widget.
In order to save the effort of passing the value down to every widget for accessibility, we have something called InheritedWidget
. This inherited widget sits at the root of your widget tree and its values can be accessed using the .of
method. You might have stumbled on something like Theme.of(context).textTheme.title
. This is exactly what we will be doing with our counter example so that our counter value can be accessed...