Skip to content
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

height is not calculated correctly on android devices #56

Open
karanSman opened this issue Jun 30, 2024 · 2 comments
Open

height is not calculated correctly on android devices #56

karanSman opened this issue Jun 30, 2024 · 2 comments

Comments

@karanSman
Copy link

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
Copy link

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

@SEGVeenstra
Copy link

I'm experiencing the abnormal height on iOS simulator currently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants