ListView 中的 WebView 没有分配其尺寸 [英] WebView in ListView without assigning its dimensions

查看:18
本文介绍了ListView 中的 WebView 没有分配其尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能将 WebView (webview_flutter) 放在 ListView 中而不分配其尺寸但自动采用其高度.我找到的唯一可行的解​​决方案是通过分配高度将 WebView 放入容器中,但我不想分配任何高度,我只想让它根据其内容占据高度..

Is there any possibility to put WebView (webview_flutter) inside a ListView without assigning its dimensions but automatically adopt its height. Only working solution I found is to put WebView in Container by assigning its height but I dont want to assign any height I just want it to occupy height according to its content..

我为此尝试了很多解决方案,包括 Expanded ,但遇到关于无限高度的错误.有什么解决办法吗?

I have tried so many solutions including Expanded for this but getting error about infinite height . Any solution?

推荐答案

很抱歉来晚了,但现在它可以正常工作了.

I am sorry it's late but now it's working properly.

我将 WebView 包装成一个 SizedBox 并指定它的高度如下:

I wrapped WebView into a SizedBox and assigned it's height like this:

              _webViewController
                  .evaluateJavascript("document.body.clientHeight")
                  .then((height) {
                print("Height of Page is: $height}");
                setState(() {
                      sizedBoxHeight = double.parse(height);
                    });
              });

对于更高级的解决方案,我们可以像这样使用 JavascriptChannels:

For a more advanced solution we can use JavascriptChannels like this:

            javascriptChannels: Set.from([
              JavascriptChannel(
                  name: 'RenderedWebViewHeight',
                  onMessageReceived: (JavascriptMessage message) {
                      setState(() {
                        sizedBoxHeight = double.parse(message.message);
                      });

            ]),

在 JavaScript 中,我们可以做这样的事情.

In JavaScript, we can do something like this.

onload="RenderedWebViewHeight.postMessage(document.body.clientHeight);"

现在我们可以看到它正在动态计算 WebView 的高度,它被包裹在一个 SizedBox 中,高度为 WebView,因此我们可以将其放入 ListView.

Now we can see it's dynamically calculating the height of WebView which is wrapped in a SizedBox with the height WebView so we can put it into a ListView.

更详细的解决方法可以查看这个.

For a more detailed solution, you can look at this.

 document.documentElement.scrollHeight

是最准确的做事方式.这适用于身体边缘和身体内部的东西边缘向下推动它.如何使用 JavaScript 获取整个文档的高度

is the most accurate way of doing things. This works with both body margins and margins of stuff inside the body pushing it downwards. More info in How to get height of entire document with JavaScript

这篇关于ListView 中的 WebView 没有分配其尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆