Flutter: Call a function on a child widget's state

后端 未结 4 1681
小蘑菇
小蘑菇 2021-02-04 06:40

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

Specifically, I have a class PlayerContainer that create

4条回答
  •  你的背包
    2021-02-04 07:01

    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.

    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.

提交回复
热议问题