shah-xad / webview_flutter_plus

An extension of webview_flutter to load HTML,CSS and Javascript even from Assets or Strings.
https://pub.dev/packages/webview_flutter_plus
Other
57 stars 49 forks source link

height is not calculated correctly on android devices #56

Open karanSman opened 4 weeks ago

karanSman commented 4 weeks ago

I used your package with latest version but it is giving the same issue which i am facing on webview_flutter: ^4.7.0. Only on android the height is coming very large but on ios calculation is correct.

here is my code class _WebviewHtmlTextState extends State { late final WebViewController _webController; double? webViewHeight;

@override void initState() { super.initState(); // #docregion platform_features late final PlatformWebViewControllerCreationParams params; if (WebViewPlatform.instance is WebKitWebViewPlatform) { params = WebKitWebViewControllerCreationParams( allowsInlineMediaPlayback: true, mediaTypesRequiringUserAction: const {}, ); } else { params = const PlatformWebViewControllerCreationParams(); }

final WebViewController webController =
    WebViewController.fromPlatformCreationParams(params);
if (webController.platform is AndroidWebViewController) {
  (webController.platform as AndroidWebViewController)
      .setMediaPlaybackRequiresUserGesture(true);
}

webController
  ..setJavaScriptMode(JavaScriptMode.unrestricted)
  ..setBackgroundColor(Color(0xffF5FAFF))
  ..loadRequest(Uri.dataFromString(
          IhwUtils.addMetaInHead(widget.htmlText!),
          mimeType: 'text/html',
          encoding: Encoding.getByName('utf-8'))
      ,)
  ..addJavaScriptChannel('messageHandler',
      onMessageReceived: (JavaScriptMessage message) async {
    print('message from the web view=\"${message.message}\"');
  })
  ..setNavigationDelegate(
    NavigationDelegate(
      onNavigationRequest: (request) {
        if (request.url.startsWith('https://www.youtube.com/')) {
          Get.toNamed<dynamic>(Routes.YOUTUBE_PLAYER.routePath,
              arguments: request.url);
          return NavigationDecision.prevent;
        } else if (Uri.tryParse(request.url)?.hasAbsolutePath ?? false) {
          Get.toNamed<dynamic>(Routes.WEB_VIEW.name, parameters: {
            'web_url': request.url,
            'title': '',
            'landscape': 'false',
            'fullscreen': 'false'
          });
          return NavigationDecision.prevent;
        }
        return NavigationDecision.navigate;
      },
      onPageFinished: (String url) async {
        // Add some delay before evaluate
        await Future<dynamic>.delayed(const Duration(milliseconds: 500));
        var _height =  await webController.runJavaScriptReturningResult(
              'document.body.offsetHeight;');
        setState(() {
          webViewHeight = (_height as num).toDouble();
        });
      },
    ),
  );

_webController = webController;

}

class IhwUtils { // add meta tag in head tag of html for ios static String addMetaInHead(String htmlString) {

  if (htmlString.contains('<head>')) {
    htmlString = htmlString.replaceAll(RegExp('<head>'),
        '<head><meta name="viewport" content="width=device-width, initial-scale=1.0">');
  } else {
    htmlString =
        '<html><head><meta name="viewport" content="width=device-width,user-scalable=no, initial-scale=1.0">' +
            htmlString +
            '</head></html>';
  }

return htmlString;

} }

antonioDolera commented 2 weeks ago

Has any progress been made regarding the use of the library for Android services?