InheritedWidget confusion InheritedWidget confusion flutter flutter

InheritedWidget confusion


Presumably where this is done, a new instance of whatever is passed as a child will be created too, causing that child's descendants to also rebuild, creating new instances of its children etc..

Ending up with the whole tree rebuilt anyway.

That's where your confusion comes from

A widget rebuilding doesn't force its descendants to rebuild.

When a parent rebuild, the framework internally check if newChild == oldChild, in which case the child is not rebuilt.

As such, if the instance of a widget didn't change, or if it overridesoperator== then it is possible for a widget to not rebuild when its parent is updated.

This is also one of the reasons why AnimatedBuilder offer a child property:

AnimatedBuilder(  animation: animation,  builder: (context, child) {    return Container(child: child,);  },  child: Text('Hello world'),);

This ensures that when for the whole duration of the animation, child is preserved and therefore not rebuilt. Leading to a much more optimized UI.