首页 > 解决方案 > Flutter webview_flutter:无法使用 java 脚本代码检测平台

问题描述

我正在使用 webview_flutter 来显示一个网页。我正在使用下面的代码来检测平台,比如它是 android 还是 ios。我下面的代码不起作用。Flutter webview - https://pub.dev/packages/webview_flutter 我在正文中添加了以下类。

class - view-withKeyboard


function applyAfterResize() {
            console.log(getMobileOperatingSystem());
            if (getMobileOperatingSystem() == 'ios') {
                if (originalPotion !== false) {
                    var wasWithKeyboard = $('body').hasClass('view-withKeyboard');
                    var nowWithKeyboard = false;
                        var diff = Math.abs(originalPotion - ($(window).width() + $(window).height()));
                        if (diff > 100) nowWithKeyboard = true;
                    $('body').toggleClass('view-withKeyboard', nowWithKeyboard);
                    if (wasWithKeyboard != nowWithKeyboard) {
                        //onKeyboardOnOff(nowWithKeyboard);
                    }
                }
            }
        }
        $(document).on('focus blur', '.white-div input[type=text]', function(e){
            //alert('here');
            if (getMobileOperatingSystem() == 'ios') {
              var $obj = $(this);
              var nowWithKeyboard = (e.type == 'focusin');
              $('body').toggleClass('view-withKeyboard', nowWithKeyboard);
              onKeyboardOnOff(nowWithKeyboard);
            }
        });

标签: javascripthtmlcssflutter

解决方案


首先启用JavascriptMOde并使用evaluateJavascript

WebView(
    javascriptMode: JavascriptMode.unrestricted,
    initialUrl: '',
    onWebViewCreated:
        (WebViewController webViewController) async {
      webViewController.evaluateJavascript(
          "javascript:(function() { " +
              "console.log('Test Test Test');" +
              "})()");
    },
  ),

推荐阅读