颤音再投入模式

发布于 2025-02-10 09:10:41 字数 168 浏览 3 评论 0原文

下面段落中谈到了什么是重新投入模式/成员变量?这是什么例子?来自Flutter官方文档: “因为小部件是不变的,如果元素没有将自己标记为脏,则该元素可以立即从构建中返回,切断步行,如果父母用相同的小部件重建元素。在两个小部件参考中,以确定新小部件与旧的小部件相同。 成员变量在其构建中。”

What is a reprojection pattern/member variable talked about in the paragraph below? What is an example of that? From Flutter official docs:
"Because widgets are immutable, if an element has not marked itself as dirty, the element can return immediately from build, cutting off the walk, if the parent rebuilds the element with an identical widget. Moreover, the element need only compare the object identity of the two widget references in order to establish that the new widget is the same as the old widget. Developers exploit this optimization to implement the reprojection pattern, in which a widget includes a prebuilt child widget stored as a member variable in its build."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

厌倦 2025-02-17 09:10:41

我能够在 this github问题。

...“ reprovention”,那里的小部件是从父母传递的,然后再次向孩子传递。例如,请参见AnimatedBuilder的儿童财产。

如果我们看一下 docs>是标题为“绩效优化”的标题,描述了以下内容的再投影:

如果构建器函数包含一个不依赖于传递给构造函数的动画的子树,则构建该子树一次而不是在每个动画上重新构建它更有效。

从中,我们可以看到“重新投入”是当我们预先建造小部件树的一部分并将其作为参数传递给经常重建的小部件时。这样,每次重建小部件时,它都可以捷径建造其小部件树的预先建造部分。

AnimatedBuilder(
  animation: _controller,
  child: <pre-built widget tree>,
  builder: (BuildContext context, Widget? child) {
    return <widget to rebuild> + child;
  },
);

I was able to find an answer to this thanks to @Hixie on this Github issue.

..."reprojection", where a widget was passed in from a parent and is being then passed down again to a child. See for example the child property of AnimatedBuilder.

If we take a look at the docs for AnimatedBuilder as recommended, there is a heading titled 'Performance optimizations' that describes reprojection as follows:

If the builder function contains a subtree that does not depend on the animation passed to the constructor, it's more efficient to build that subtree once instead of rebuilding it on every animation tick.

From this, we can see that 'reprojection' is when we pre-build a portion of the widget tree and pass it as a parameter to a widget that is frequently rebuilt. That way, each time the widget is rebuilt, it can shortcut building the pre-built portion of its widget tree.

AnimatedBuilder(
  animation: _controller,
  child: <pre-built widget tree>,
  builder: (BuildContext context, Widget? child) {
    return <widget to rebuild> + child;
  },
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文