Kitcc
Kitcc

Reputation: 3178

Flutter Video screen not closing on completion when WillPopScope implemented

I recently implemented WillPopScope on the VideoPlayer package in my app:

child: WillPopScope(
                  onWillPop: () async => false,
                  child: VideoPlayer(_controller),
                ),

I use this to stop the swipe back, that was occurring before. Which works fine.

However it has resulted in another error. When the VideoPlayer completes it should close the screen and this is no longer happening. It worked fine before.

Here is my exit screen code:

void closeScreen() async {
  final _exercises = Provider.of<Exercises>(context, listen: false);
  _exercises.increasePlayCount(widget.exercise.id);
  print('VIDEO FINISHED ... CLOSE!!');
  widget.exercise.setRecentPlayed(widget.exercise);
  Navigator.of(context).maybePop();
  SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
    overlays: [SystemUiOverlay.top]);
}

This method is being called fine as the print statement is being executed as expected.

I hav tried changing the placement of the WillPopScope but that hasn't fixed the problem. Could someone please let me know how I can correct this.

Many thanks

Upvotes: 1

Views: 518

Answers (1)

Jared Anderton
Jared Anderton

Reputation: 1026

I suggest making sure that your widget is a StatefulWidget, and doing something like this:

Add a boolean to state indicating if the video has finished:

bool isFinished = false;

Alter your WillPopScope like this:

WillPopScope(
  onWillPop: () async => isFinished,
  child: VideoPlayer(_controller),
)

Finally, update your closeVideo to toggle the value of isFinished

void closeScreen() async {
  final _exercises = Provider.of<Exercises>(context, listen: false);
  _exercises.increasePlayCount(widget.exercise.id);
  isFinished = true;
  print('VIDEO FINISHED ... CLOSE!!');
  widget.exercise.setRecentPlayed(widget.exercise);
  Navigator.of(context).maybePop();
  SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
    overlays: [SystemUiOverlay.top]);
}

Upvotes: 1

Related Questions