InAppWebView在LayoutBuilder中全屏问题

huangapple go评论52阅读模式
英文:

InAppWebView within LayoutBuilder fullscreen issue

问题

I'm trying to use InAppWebView (v6 latest beta because I'm trying to get this to work on the web platform) to display an embedded vimeo player. In a new flutter app, this works fine:

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    final GlobalKey webViewKey = GlobalKey();

    InAppWebViewSettings settings = InAppWebViewSettings(
        useShouldOverrideUrlLoading: true,
        mediaPlaybackRequiresUserGesture: false,
        allowsInlineMediaPlayback: true,
        iframeAllowFullscreen: true);

    return Scaffold(
      body: InAppWebView(
          key: webViewKey,
          initialSettings: settings,
          initialUrlRequest: URLRequest(
              url: WebUri(Uri.dataFromString(
                      '<html><div style="position:relative;padding-top:56.25%;"><iframe src="https://player.vimeo.com/video/689902544?h=1c8590a277" frameborder="0" allow="autoplay; fullscreen; picture-in-picture" style="position:absolute;top:0;left:0;width:100%;height:100%; allowfullscreen></iframe><p><a href="https://vimeo.com/689902544">Eternal Spring</a> from <a href="https://vimeo.com/christopherdormoy">Christopher Dormoy</a> on <a href="https://vimeo.com">Vimeo</a>.</p></div></html>',
                      mimeType: 'text/html')
                  .toString())
              //url: WebUri("https://orf.at")
              )),
    );
  }
}

When I click on the fullscreen icon in the Vimeo player, my browser correctly switches to fullscreen.

However, in my actual application, my widget is wrapped inside a LayoutBuilder and it seems, whenever the InAppWebView enters fullscreen, the LayoutBuilder triggers a rebuild of the child widget tree and thus immediately I'm thrown out of the fullscreen again.

What can I do to make fullscreen work also within a LayoutBuilder?

英文:

I'm trying to use InAppWebView (v6 latest beta because I'm trying to get this to work on the web platform) to display an embedded vimeo player. In a new flutter app, this works fine:

class _MyHomePageState extends State&lt;MyHomePage&gt; {
  @override
  Widget build(BuildContext context) {
    final GlobalKey webViewKey = GlobalKey();

    InAppWebViewSettings settings = InAppWebViewSettings(
        useShouldOverrideUrlLoading: true,
        mediaPlaybackRequiresUserGesture: false,
        allowsInlineMediaPlayback: true,
        iframeAllowFullscreen: true);

    return Scaffold(
      body: InAppWebView(
          key: webViewKey,
          initialSettings: settings,
          initialUrlRequest: URLRequest(
              url: WebUri(Uri.dataFromString(
                      &#39;&lt;html&gt;&lt;div style=&quot;position:relative;padding-top:56.25%;&quot;&gt;&lt;iframe src=&quot;https://player.vimeo.com/video/689902544?h=1c8590a277&quot; frameborder=&quot;0&quot; allow=&quot;autoplay; fullscreen; picture-in-picture&quot; style=&quot;position:absolute;top:0;left:0;width:100%;height:100%; allowfullscreen&gt;&lt;/iframe&gt;&lt;p&gt;&lt;a href=&quot;https://vimeo.com/689902544&quot;&gt;Eternal Spring&lt;/a&gt; from &lt;a href=&quot;https://vimeo.com/christopherdormoy&quot;&gt;Christopher Dormoy&lt;/a&gt; on &lt;a href=&quot;https://vimeo.com&quot;&gt;Vimeo&lt;/a&gt;.&lt;/p&gt;&lt;/div&gt;&lt;/html&gt;&#39;,
                      mimeType: &#39;text/html&#39;)
                  .toString())
              //url: WebUri(&quot;https://orf.at&quot;)
              )),
    );
  }
}

When I click on the fullscreen icon in the Vimeo player, my browser correctly switches to fullscreen.

However, in my actual application, my widget is wrapped inside a LayoutBuilder and it seems, whenever the InAppWebView enters fullscreen, the LayoutBuilder triggers a rebuild of the child widget tree and thus immediately I'm thrown out of the fullscreen again.

What can I do to make fullscreen work also within a LayoutBuilder?

答案1

得分: 1

问题似乎出在你对GlobalKey的使用上 - 每次调用build函数时都会创建它,这反过来会导致InAppWebView的重新初始化,可能导致它退出全屏。

简单的解决方法是只创建一次GlobalKey(或者如果不需要它做其他用途,可以完全省略它):

class _MyHomePageState extends State<MyHomePage> {
  final webViewKey = GlobalKey();

  @override
  Widget build(BuildContext context) {
     ...
  }
}
英文:

The problem seems to be in your usage of the GlobalKey - it's created every time build function is called
This, in turn causes re-initialization of the InAppWebView, which will probably cause it to exit full-screen.

Simple solution is to create the GlobalKey once (or just completely omit it if you don't need it for anything else):

class _MyHomePageState extends State&lt;MyHomePage&gt; {
  final webViewKey = GlobalKey();

  @override
  Widget build(BuildContext context) {
     ...
  }
}

huangapple
  • 本文由 发表于 2023年4月20日 05:22:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058909.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定