I want to use InheritedWidget
to access and change its data from anywhere in application.
I've read many articles about InheritedWidget
, but I do not understand one of its behaviors.
Here it says that only widgets that are using InheritedWidget
get rebuilt when data changes(which is exactly what I want).
But here it says that InheritedWidget
is immutable and its data only changes when it is rebuilt itself!
So doesn't this make the whole widget tree below the InheritedWidget
get rebuilt when the data changes?
How can I wrap the MaterialApp
widget with InheritedWidget
so I can change its data from anywhere in app and only rebuild a small Widget that is using InheritedWidget
when the data changes?
I know I can implement this using provider package very easily, but in this part of application I want to use InheritedWidget
if it's possible :)
CodePudding user response:
An object or widget being immutable does not mean its values can't change.
You can declare a final List<widget> children
, which is immutable since it is final, and still add and remove widgets from it. What is immutable is the reference.
When you use the InheritedWidget is the same. If a value changes it wont rebuild itself and all the tree below. Even it is actually changed and rebuilt, it does not necessarily rebuild all tree below, it'd happen only if the widget type or its child/children reference changed also.