Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

flutter

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 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?

like image 913
Mike Miller Avatar asked May 28 '18 00:05

Mike Miller


People also ask

How do you call a function in stateless widget Flutter?

In Flutter this can be done using a stateful widget and calling your code in the initState function. What if you want to call it from a stateless widget? Well, that's possible too. Use a stateful widget as a your root widget that you can provide a callback function too to execute your startup logic.

How do you call a Flutter function?

In Flutter this can be done using a stateful widget and calling your code in the initState function. What if you want to call it from a stateless widget? Well, that's possible too. Use a stateful widget as a your root widget that you can provide a callback function too to execute your startup logic.

How do you call a function automatically in Flutter?

You should make the call of your function in initState rather then in widget tree as everytime the widget is rebuild, it will call the function to execute again and again(leading to more reads).


1 Answers

I know that I'm pretty late to the party, but I have something that I think might help. So, you need to do four (4) things in your VideoPlayerController class:
1. Create an instance of your state class.
2. Create a method (play) which will be accessible in your PlayerContainer class
3. In your method, use the VideoPlayerControllerState instance to call the method in your state class.
4. Finally, when you createState, do so using the instance that you already created.

class VideoPlayerController extends StatefulWidget {
  final VideoPlayerControllerState vpcs = VideoPlayerControllerState();

  void play() {
    vpcs.play();
  }

  @override
  State<StatefulWidget> createState() => vpcs;
}

As you see, the play method uses vpcs (the VideoPlayerControllerState instance) to call the play method already in your state class.

In your PlayerContainer class, use your member variable to call the play method.

class PlayerContainerState extends State<PlayerContainer> {
  VideoPlayerController _vpc;

  @override
  void initState() {
    super.initState();
    _vpc = VideoPlayerController();
  }
  ...

  void _handlePressPlay(){
    _vpc.play();
  } 
  ...

  @override
  Widget build(BuildContext context) {
    return ... //your video player widget using _vpc as your VideoPlayerController
      _vpc,
    );
  }
}

You can call _handlePressPlay() from the onPressed method of your play button. Alternatively, just put _vpc.play() in the onPressed method. Your choice :-).

like image 148
Darren Cole Avatar answered Sep 20 '22 01:09

Darren Cole