-
Notifications
You must be signed in to change notification settings - Fork 26.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[webview_flutter] make height of WebView the height of webpage #34138
Comments
I wanted a similar behavior and came up with a new widget which wraps You can find source of my solution here. |
Hi @SPodjasek, great solution. Do you get the exact size for the webview with |
@ivtod This is the simplest possible solution that works for me. My WebView contents were 100% static and maybe because of that, I haven't noticed any extra white space. If your is in any kind dynamic, even when you use some lazy loading web fonts you would have to think of some other solution to fetch final body height, using event listeners on DOM - maybe snippet below will give you better results. document.body.onload = function() {
window.extents.postMessage(document.body.offsetHeight)
} And keep in mind that using this solution you can get really nasty results in production, scaling WebView anything above 3000px on Pixel 3 emulator results in depletion of resources and system soft-boot. This limit varies by platform. |
I add webview to scrollable. webview crashed when content height too large. how to resolve this? contentHeight=8455.0 |
@lorentz-wu You can't resolve this for now with WebView. Use some other solution, or if you really need to place |
me too, working fine on iOS |
I use document.body.scrollHeight with the exact same solution. I think it's our only option for now and it works well. window.onLoad works best because it waits for all the resources to finish loading. Those can affect the final size. There could be 2 functions, one for initial DOM ready (document.onLoad) and one for full page ready (window.onLoad) to adapt faster to size change. Meanwhile, @SPodjasek is the height resources limitation officially documented anywhere? Or do we go by trial and error? |
@shadysamir I haven't found any documentation about this limitation and it is probably hardware dependent limitation. And as for some recent flutter/engine changes you get a warning if you scale |
Hey, tried many solution but this seemed work for me. |
The problem is still exist. we need an urgent solution for this issue. i tried more ways like parse html and load it in list of webviews but it affect the performance. |
how to manage width of web page in flutter |
I just created an extension of webview_flutter which allows getting the height of Web content which will allow you to use it even in a listview, do check this webview_flutter_plus. |
I also encounter this problem. I use 'flutter_Inappwebview ` loads Amap and crashes while moving |
same problem |
I have created a package in extension to the |
This implementation partly solves this issue for me https://gist.github.com/PonnamKarthik/877a90917a576ecff613d5169680d02c See CarloTerracciano solution for URLs as in your case. https://gist.github.com/PonnamKarthik/877a90917a576ecff613d5169680d02c#gistcomment-3437222 |
@SPodjasek I know this is an old comment, but I need to implement what you described here in my app, as I am trying to embed a WebView in DraggableScrollableSheet. Could you maybe help me implement this or point me to the right direction? How can I sync the WebView internal scroll to Scrollable's ScrollController? |
You could always inject resize observer into body to adjust webview size in real time: <script>
const resizeObserver = new ResizeObserver(entries =>
Resize.postMessage("height" + (entries[0].target.clientHeight).toString()) )
resizeObserver.observe(document.body)
</script> And add a js channel javascriptChannels: Set.from([
JavascriptChannel(name: "Resize", onMessageReceived: (JavascriptMessage message) {
updateHeight();
})
]), Update height: void updateHeight() async {
double height = double.parse(await controller
.evaluateJavascript(
"document.documentElement.scrollHeight;"));
if(this.height != height) {
setState(() {
this.height = height;
});
}
} I'm using additional update height because resize observer gave me a wrong height metrics |
I already implement a height observer in my web page and I read it through JS channel. That's not the issue. The issue is that there seems to be a limit to the webview height, beyond it the app crashes (memory?) |
Sorry to barge in here. But i use a simular approach for obtaining the pixel height. double height = double.parse(await controller My problem is that the reported pixelheight on IOS (Iphone 12) looks like it is twice the flutter pixel height??? For example I get 1900 pixels back from the IOS callback. but is has to be 950 "flutter" pixels.. Is there some kind of multiplier on the ios device active? |
@dickverweij Did you find a solution? I Have the same problem, in Android the workaround works well but in IOS (SE 1º gen) the height is almost double. |
Hey there, did you or anyone here found the solution to this? I tried to divide the height by |
Nope.. Sadly IOS is currently not pixel perfect in our app. Next to the crash for big android webviews loaded in to flutter platform views, we can say that normal viewing web content in a flutter app is somewhat flawed. For anyone who has issues with the big size crash in android, we "solved" it to make the view in flutter a fixed height and used gestures to control the scrolling in the platform view. |
Hello, I don't know if this is late. I resolved getting the wrong height by adding some delay before evaluating scrollHeight. WebView(
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (controller) {
_controller = controller;
_controller?.loadHtmlString(_htmlString.trim());
},
onPageFinished: (url) async {
// Add some delay before evaluate
await Future.delayed(const Duration(milliseconds: 1000));
String heightStr = await _controller?.runJavascriptReturningResult(
"document.documentElement.scrollHeight") ??
"0";
setState(() {
_webviewHeight = double.parse(heightStr);
});
log(heightStr);
},
) |
@phyohtetarkar still I get double height of webview in iOS |
I solved this. This should work for everyone. I am using flutter_inappwebview, but, it should work for webview_flutter too. You have to use your controller for this:
|
Just add this line to |
I already did that:
The only thing what works for me is to wait a second, and then do the javascript call again, then it reports the actual (correct) size. |
Did you try the above solution by me, involving setting the width in the Flutter app by evaluating Javascript? |
No. Does setting the width somehow influences the height (?? ) Also I see that your script evaluates document.readyState into flutter variable "value" but does nothing with it? |
Why not just try it? You can ignore the value in readyState. |
Just a quick question to add in this thread. I mean, if a |
Still not fixed, after 5 years... |
P3 so ... |
I would like to have multiple WebViews (from the webview_flutter plugin) in my flutter app on the same page. I want to have each page be loaded so that the height of the container is the same as the height of the web page.
Here's what I have so far:
Obviously, this loads 3 webviews each with a height of 500. However, I don't want to have to define a height for each page, and instead have it set the height of the Container based on the height of the actual page. (ie. urlA has a page with a height of 300, urlB is 250, and urlC is 600, so each container (or some other widget) has a height of 300, 250, and 600 respectively. Each container fits to be exactly that size. If at any point they go off the page, you can scroll with ListView.)
The text was updated successfully, but these errors were encountered: