英文:
Parent widget rebuilding will not cause sub-widgets rubuild?
问题
@protected
bool updateShouldNotify(covariant InheritedWidget oldWidget);
InheritedWidget的官方注释说:当重新构建InheritedWidget时,依赖的小部件可以选择是否重新构建。
但在我理解中,这个InheritedWidget是所有依赖小部件的父小部件,如果父小部件重新构建,它将会递归地使所有子小部件重新构建。
我对重新构建的理解是否正确?
英文:
/// 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);
The official comment of InheritedWidget says that : when the InheritedWidget is rebuilt, the dependant widgets can choose to rebuild or not.
But in my understanding, this InheritedWidget is the parent widgets to all the dependant widgets ,and if the parent widgets is rebuild , it will make all the child widgets rebuild recursively.
Is my understanding of rebuild wrong?
答案1
得分: 1
在Flutter中,子组件的实现以及父组件如何利用它们决定了父组件的重建是否会导致子组件的重建。
当父组件重建时,如果子组件是使用"const"构造函数定义的并且它们的属性保持不变,那么子组件不需要重建。
然而,如果子组件依赖于可变数据或具有非常量属性(例如由"setState"使用的属性),那么当父组件重建时,子组件将会重建。在某些情况下,使用"Keys"、将子组件包装在"ValueListenableBuilder"或"StreamBuilder"小部件中以监听特定数据更新的情况下,可以优化重建过程。
您可以采用一些策略来防止在父组件重建时重建子组件:
- 使用"const"来定义子组件。
- 使用带有自己状态的StatefulWidget。
- 利用Flutter默认包含的优化小部件,如"ValueListenableBuilder"、"StreamBuilder"或"Provider"。
请记住,Flutter只会重新构建已更改的部分小部件树,通常会有效地执行此操作。大多数情况下,更改父组件不会显著影响性能,但优化总是一个好主意。
英文:
In Flutter, the implementation of the child widgets and how the parent widget utilises them determine whether a parent widget's rebuild will cause the child widgets to rebuild.
When the parent widget rebuilds, the child widgets do not need to be rebuilt if they are defined using 'const' constructors and their properties remain constant.
The child widgets will rebuild when the parent widget does, though, if they depend on mutable data or have non-const attributes (such as those used by'setState'). In certain circumstances, employing "Keys" or encasing the child widget in a "ValueListenableBuilder" or "StreamBuilder" widget that listens for particular data updates will optimise the rebuild process.
You can employ a few strategies to prevent rebuilding child widgets when the parent widget rebuilds:
- When using 'const' for child widgets.
- Employ a StatefulWidget with a state of its own.
- Make use of the optimisation widgets that Flutter includes by default, such as "ValueListenableBuilder," "StreamBuilder," or "Provider."
Remember that only the sections of a widget tree that have changed are rebuilt by Flutter, which is often effective at doing so. The most of the time, changing a parent widget won't significantly affect performance, but it's always a good idea to optimise.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论