如何将数据儿童传递给​​儿童小部件,而无需重建乱七八糟的父母小部件?

发布于 2025-02-06 14:23:57 字数 477 浏览 4 评论 0原文

我有一个parentwidget有两个孩子小部件。 我想从firstChild()将一些回调操作传递给secustchild() widget,而无需重建parentwidget()喜欢setState(( {})

我想这样做,因为parentwidget()有许多小部件。这种回调动作不断发生。

并且连续setState((){})对于parentwidget()

示例

setState(setState((){})从<<代码> firstChild()比我想重建secondchild()没有重建parent> parentwidget()

I have one ParentWidget having two child widget.
I want to pass some callback action from FirstChild() to SecondChild() Widget without rebuild ParentWidget()like setState((){}).

I want to do because ParentWidget() has many widget. And this callback action continuously happen.

And continuously setState((){}) is not viable option for ParentWidget()

For example

setState((){}) call from FirstChild() than I want to rebuild SecondChild() without rebuild ParentWidget()

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

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

发布评论

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

评论(2

虫児飞 2025-02-13 14:23:58

statefulwidget仅在非常简单的业务逻辑案例中有用。我强烈建议您查看 riverpod ,您可以在其中访问任何小部件的“状态”,无论其父母是谁。由于RiverPod的设计方式,它不会触发任何不必要的重建。

坚持使用statefulwidget,您将需要使用senasitedWidget作为两个小部件的父母,然后让它们都访问它。

StatefulWidget is only useful in very simple business logic cases. I highly recommend that you have a look at Riverpod, where you can access a "state" from any widget, regardless of who its parent widget is. And because of how Riverpod is designed, it will not trigger any unnecessary rebuilds.

Sticking with StatefulWidget, you will need to use something like InheritedWidget as a parent of both widgets, and then have them both access it.

离旧人 2025-02-13 14:23:58

您可以在提供商或RiverPod等州管理解决方案方面获得帮助。
如果您使用RiverPod,我可能会编写一个示例代码,但总的来说,在这种情况下,您需要将状态放置在父窗口外部的外部变量中。然后,您可以在不重建父母的情况下在两个孩子之间进行访问并共享。

You can get help with state management solutions like Provider or Riverpod.
I may write an example code if you use Riverpod, but generally speaking, in such case, you need to place the state outside the Parent widget into some external variable. Then you can access and share it between both Children without rebuilding Parent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文