Flutter 如何从 flutterWebviewPlugin.evalJavascript (addEventListener) 中检索 javascript 值

Gen*_*ene 3 flutter

我有一个颤振代码片段,它从我的 iframe 页面侦听 postMessage。(flutter_webview_plugin: ^0.3.9+1)

flutterWebviewPlugin.onStateChanged.listen((viewState) async {
    String script = 'window.addEventListener("message", receiveMessage, false);' +
            'function receiveMessage(event) {console.log(\'receiving data from child , data as follows: \',event.data)}';
    flutterWebviewPlugin.evalJavascript(script);
}
Run Code Online (Sandbox Code Playgroud)

如果event.data返回特定值,我想触发特定的颤振函数, 相机值将触发我调用相机插件等的特定函数。意思是说我的目标 iFrame 将尝试通过postMessage方法进行跨域通信。

对于科尔多瓦,我可以这样做:

window.addEventListener( "message" , function( event )
{
  else if( event.data.indexOf( "camera" ) >= 0 )
        {
           //Trigger Camera Function
Run Code Online (Sandbox Code Playgroud)

我该如何为颤振做这件事?

Trầ*_*Tâm 10

2个解决方案:

解决方案1:推荐

import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';

final Set<JavascriptChannel> jsChannels = [
  JavascriptChannel(
      name: 'Print',
      onMessageReceived: (JavascriptMessage message) {
        print('message.message: ${message.message}');
      }),
].toSet();

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      routes: {
        "/": (_) => WebviewScaffold(
              url: Uri.dataFromString(
                      '<html><button onclick="Print.postMessage(\'test\');">Click me</button></html>',
                      mimeType: 'text/html')
                  .toString(),
              appBar: new AppBar(title: new Text("Widget webview")),
              javascriptChannels: jsChannels,
            ),
      },
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

I/flutter (22119): message.message: test

解决方案 2:如果您想保留您的window.post. (也许您想使用iframe其他平台的)

import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';

final Set<JavascriptChannel> jsChannels = [
  JavascriptChannel(
      name: 'Print',
      onMessageReceived: (JavascriptMessage message) {
        print('message.message: ${message.message}');
      }),
].toSet();

void main() {
  final flutterWebviewPlugin = FlutterWebviewPlugin();
  flutterWebviewPlugin.onStateChanged.listen((state) async {
    if (state.type == WebViewState.finishLoad) {
      String script =
          'window.addEventListener("message", receiveMessage, false);' +
              'function receiveMessage(event) {Print.postMessage(event.data);}';
      flutterWebviewPlugin.evalJavascript(script);
    }
  });
  return runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      routes: {
        "/": (_) => WebviewScaffold(
              url: Uri.dataFromString(
                      '<html><button onclick="window.postMessage(\'test\', \'*\');">Click me</button></html>',
                      mimeType: 'text/html')
                  .toString(),
              appBar: new AppBar(title: new Text("Widget webview")),
              javascriptChannels: jsChannels,
            ),
      },
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

I/flutter (22119): message.message: test

pubspec.yaml

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^0.1.2
  flutter_webview_plugin: 0.3.9+1
Run Code Online (Sandbox Code Playgroud)

参考