简体   繁体   English

如何在Flutter中访问父级中的子控件的数据?

[英]How to access data of child widget in parent in flutter?

I want to know the value of a certain field in a child (or child of child) widget in the parent widget, only when I want to know it. 我想仅在我想知道父小部件中的子(或子小部件)小部件中某个字段的值时才知道。
Using a Callback or InheritedWidget/Provider doesn't work in this case as it provides me information every time there's a change in the value in the child. 在这种情况下,使用CallbackInheritedWidget / Provider无效,因为每次子项值发生变化时,它都会向我提供信息。 I only require the child's value, when there's an action in the parent. 当父母中有一个动作时,我只要求孩子的价值。 So, listening to all the changes is an overhead. 因此,倾听所有更改是一项开销。
Another option is using a key to fetch the information from the child, but the children can be multiple, so will have to create those many keys and somehow attaching them to the children. 另一种选择是使用密钥从子级中获取信息,但是子级可以是多个子级,因此必须创建那么多密钥,并以某种方式将其附加到子级上。 And keys are a bit expensive. 而且钥匙有点贵。
Another option would be to store instances of the children and getting value from it. 另一种选择是存储孩子的实例并从中获取价值。

Every option seems like overkill for a simple task. 对于一个简单的任务,每个选项都显得有些过分。 I am favoring on the key approach as it gives a feeling that they were created for these types of scenarios. 我赞成关键方法,因为它给人一种感觉,它们是针对此类情况创建的。 Does anyone know anything better for this situation? 有谁对这种情况有更好的了解?

A simple use case is: 一个简单的用例是:
Imagine a parent having a PageView in the middle and a next button. 想象一个父母在中间有一个PageView,还有一个下一个按钮。 The page in the middle contains a slider which changes its value. 中间的页面包含一个可更改其值的滑块。 When we press next, we wanna know what's the slider's final value is. 当我们按下一步时,我们想知道滑块的最终值是多少。

There's a reason why it seems so hard: you shouldn't. 为什么看起来如此困难是有原因的:您不应该这样。

Widgets are purposefully very limited to enforce an uni-directional data-flow, which is critical for maintainability of larger apps. 窗口小部件有目的地非常有限地强制执行单向数据流,这对于大型应用程序的可维护性至关重要。

Instead of reading the state of your descendants, you should "lift the state up" through inherited-widgets and similar patterns. 而不是读取子孙的状态,您应该通过继承的小部件和类似的模式来“提升状态”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据从子状态小部件传递到 Flutter 中的父小部件 - How to pass data from a child Stateful widget to Parent Widget in Flutter 如何从 flutter 中的父小部件访问所有孩子的 state? - How to access all of child's state from Parent Widget in flutter? 如何在不重建父小部件的情况下将数据子小部件传递给子小部件? - How to pass data child to child widget without rebuild parent widget in flutter? 在父窗口小部件中访问子窗口小部件的变量(Flutter with Dart) - Access child widget's variable in parent widget (Flutter with Dart) 如何使用 flutter 访问小部件的子 class 中小部件的父 class 的属性? - How access properties of Parent class of widget in Child class of widget using flutter? 如何使用 flutter 中的提供程序或 valuenotifier 将数据从子小部件传递到父小部件 - How to pass data from child widget to parent widget using provider or valuenotifier in flutter 如何监听父小部件中的事件或数据更改并将其传播到 flutter 中的子小部件? - How to listen to events or data change in parent widget and propagate it to child widget in flutter? Flutter - 如何访问父事件或动作的子数据 - Flutter - How to access child data on parent event or action 如何将父小部件的高度传递给 flutter 中的子小部件? - How to pass the height of parent widget to child widget in flutter? 如何从子小部件调用父小部件中的 function | Flutter - How to Call a function in parent widget from child widget | Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM