Flutter:在子窗口小部件的状态上调用函数 [英] Flutter: Call a function on a child widget's state

查看:123
本文介绍了Flutter:在子窗口小部件的状态上调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个有状态的小部件,其父小部件需要调用一个处于子状态的函数.

I've created a stateful widget and its parent widget needs to call a function that lives in the child's state.

具体来说,我有一个类PlayerContainer,该类创建一个VideoPlayer并具有一个VideoPlayerController的成员变量.当我按下播放按钮时,我的主类需要在该州的VideoPlayerController上调用play(),因此我在State类内创建了一个函数,但是我不知道如何从父窗口小部件访问该函数.

Specifically, I have a class PlayerContainer that creates a VideoPlayer and has a member variable for the VideoPlayerController. When I press the play button, my main class needs to call play() on the state's VideoPlayerController, so I created a function inside the State class, but I don't know how to access that function from the parent widget.

那有可能吗?还是我要把这都弄错了?

Is that even possible? Or am I going about this all wrong?

推荐答案

使用简单的应用程序,您只需在与VideoPlayer相同的Widget中创建播放按钮即可.通过将PlayerContainer与它的父级结合使用,您可以增加Widget状态范围的大小,以便需要访问它的所有内容都成为单个较大Widget的一部分.

With a simple application you could simply create the play button in the same Widget as the VideoPlayer. By combining the PlayerContainer with its parent you are increasing the size of the scope of the Widget State so that everything that need access to it is part of the single larger Widget.

祖先影响子窗口小部件的主要方式是:通过使用不同的参数进行重建,或者通过听祖先更改的内容.对于后者,您可以在子级附近的某个地方使用InheritedWidget.如果子级引用InheritedWidget,则在IW更改时将对其进行重建.另一种方法是监听祖先生成的事件流.

The main ways that a child Widget can be influenced by an ancestor are: by being rebuilt with different parameters, or by listening to something that the ancestor changes. For the latter you can use an InheritedWidget somewhere about the child. If the child refers to the InheritedWidget it gets rebuilt when the IW changes. Another way is to listen to an event stream generated by the ancestor.

您可能会发现,最简单的方法是在单个构建中构建整个页面,直到变得笨拙为止.

You may find it easiest to just build your whole page in a single build until this becomes unwieldy.

这篇关于Flutter:在子窗口小部件的状态上调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆