The official comment of updateShouldNotify says it can control the InheritedWidget's child build or not when the InheritedWidget is rebuild.
/// Whether the framework should notify widgets that inherit from this widget.
///
/// When this widget is rebuilt, sometimes we need to rebuild the widgets that
/// inherit from this widget but sometimes we do not. For example, if the data
/// held by this widget is the same as the data held by `oldWidget`, then we
/// do not need to rebuild the widgets that inherited the data held by
/// `oldWidget`.
///
/// The framework distinguishes these cases by calling this function with the
/// widget that previously occupied this location in the tree as an argument.
/// The given widget is guaranteed to have the same [runtimeType] as this
/// object.
@protected
bool updateShouldNotify(covariant InheritedWidget oldWidget);
But if the InheritedWidget is rebuilt , then all the widgets inherited from the this widget will rebuilt no matter this updateShouldNotify is return false or true. because these widgets are the children of the InheritedWidget .
Can anybody give an example of children do not rebuild with updateShouldNotify when the InheritedWidget is rebuilt?
I can give you a simple example: how can a
constchild widget of theInheritedWidgetever be rebuilt if it is never notified?Run this sample code to see the behavior. Change the return value of
updateShouldNotifyfromfalsetotrueand then tap on the button.The
InheritedWidgetallows you to filter rebuilds (calls tosetStateso that descendants are only rebuilt when any data that they depend on (from the parent InheritedWidget) changes.While the convenience idiom is to provide a static
of(context)method for your inherited widgets, remember that they can be used without one; to make it explicit:From the documentation: "Returns the nearest widget of the given type T and creates a dependency on it".
When a dependency is created, the child widget is registered for changes. When does it get notified of the changes?
When
updateShouldNotifyreturntrue.Therefore, the correct implementation of
Inherited.updateShouldNotifyis: