Dilshan
Dilshan

Reputation: 3001

How to programmatically exit from full screen in WebView?

My Widget tree looks like this,

Widget build(context) {
 return Consumer<SocketModel>(
  builder: (ctx, model, snapshot) {
    if(model.forceLeave == true) {
      return Text("Your time is up")
    }
    return WebViewWithAHtmlPlayer()
  }
 )
}

The above code is not the real code but to keep this clear I have remove most of the parts. This is what happens basically. The html player inside the webview can play videos. I have a socket connection which tracks the time and after timeout forceLeave becomes true which cause to run the if condition.

This works when I don't make the html video player full screen. But If the forceLeave becomes true while I'm on fullscreen on the html player, the screen get blank with a white background. Since I disabled the back button, my application acts like it freeze ( But it actually not. )

How can I exit from full screen view ?

I have tried, SystemChrome.restoreSystemUIOverlays() inside the dispose of the WebView wrapper Widget. But still it does not exit from the fullscreen view.

How can I correctly exit from current full screen view within the above controlled structure ? For example, is there any way to do something like,

  builder: (ctx, model, snapshot) {
    if(model.forceLeave == true) {
      exitFullScreen() // <-- Do something like this ? 
      return Text("Your time is up")
    }
    return WebViewWithAHtmlPlayer()
  }

Upvotes: 0

Views: 1440

Answers (2)

insaneray
insaneray

Reputation: 83

import "dart:html"

document.exitFullscreen();

That's it :)

Upvotes: 0

Dilshan
Dilshan

Reputation: 3001

I did not found a way to exit full screen from Flutter code. So I did something like this. In the place where I set the timeout method,

Future.delayed(
  Duration(seconds: timeLeft
      ),
  () {
    // Here I force the webview to exit from full screen before I disconnect
    controller.evaluateJavascript(source: "document.exitFullscreen()");

    Future.delayed(Duration(milliseconds: 100), () {
      socketDao.socket.disconnect(); // this cause to `forceLeave` becomes true
    });
  },
);

Upvotes: 1

Related Questions