fluttercommunity / flutter_webview_plugin

Community WebView Plugin - Allows Flutter to communicate with a native WebView.
https://pub.dev/packages/flutter_webview_plugin
Other
1.48k stars 933 forks source link

i want to call custom flutter function from javascription fuction. #757

Open vikasgupta4190 opened 4 years ago

vikasgupta4190 commented 4 years ago

Here is my code. MainActivity.kt override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) { GeneratedPluginRegistrant.registerWith(flutterEngine); }

WebviewManager.java webView.addJavascriptInterface(new WebAppInterface(), "Android"); ////////////// public class WebAppInterface { @JavascriptInterface public void getPostMessage(String value){ Map<String, Object> postMessageMap = new HashMap<>(); postMessageMap.put("postMessage", value); FlutterWebviewPlugin.channel.invokeMethod("onPostMessage", postMessageMap); } }

dart file : flutterWebViewPlugin.onStateChanged.listen((state) async {

    if(state.type == WebViewState.finishLoad) {

      String script = 'window.addEventListener("message", sendWhatsApp,false);' +
          'function sendWhatsApp(msg) {Android.postMessage("data");}';

      flutterWebViewPlugin.evalJavascript(script);

    }
vikasgupta4190 commented 4 years ago

Resolved. Thnx

shinriyo commented 1 year ago

@vikasgupta4190 did you resolve this?

How did you write in flutter code?

shinriyo commented 1 year ago

@vikasgupta4190 How about a Widget?

return WebviewScaffold(
  url: url,
  javascriptChannels: <JavascriptChannel>{
    JavascriptChannel(
      name: 'Android',
      onMessageReceived: (message) {
        print(message);
      },
    ),
  },
);
shinriyo commented 1 year ago
flutterWebViewPlugin.onStateChanged.listen((state) async {
    if(state.type == WebViewState.finishLoad) {

      String script = 'window.addEventListener("message", sendWhatsApp,false);' +
          'function sendWhatsApp(msg) {Android.postMessage("data");}';

      flutterWebViewPlugin.evalJavascript(script);

    }

It don't work. for addJavascriptInterface