父组件重新构建不会导致子组件重新构建。

huangapple go评论63阅读模式
英文:

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"小部件中以监听特定数据更新的情况下,可以优化重建过程。

您可以采用一些策略来防止在父组件重建时重建子组件:

  1. 使用"const"来定义子组件。
  2. 使用带有自己状态的StatefulWidget。
  3. 利用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:

  1. When using 'const' for child widgets.
  2. Employ a StatefulWidget with a state of its own.
  3. 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.

huangapple
  • 本文由 发表于 2023年5月29日 14:43:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76355171.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定