【发布时间】:2020-04-03 07:20:15
【问题描述】:
我是新来的颤振。我已经开发了 js 来使用 webview_flutter 进行飞镖通信。像这样:
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example'),
actions: <Widget>[
MenuList(_controller.future),
],
),
body: Builder(builder: (BuildContext context) {
return WebView(
initialUrl: localServerUrl,
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (WebViewController webViewController) {
_controller.complete(webViewController);
},
javascriptChannels: <JavascriptChannel>[
_scanBarcode(context),
].toSet(),
onPageFinished: (String url) {
//TODO : events after page loading finished
},
);
}),
),
);
}
JavascriptChannel _scanBarcode(BuildContext context) {
return JavascriptChannel(
name: 'Barcode',
onMessageReceived: (JavascriptMessage message) {
String result = scanBarcode(context);
******I got result of scanned barcode in result variable******
});
}
服务器 html 文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="script.js"></script>
</head>
<body>
<button id="btnBarcode" style="margin-top:20px; height:30px;">Scan Barcode</button>
</body>
</html>
这是JS文件
function scanBarcode(message) {
if (window.Barcode && window.Barcode.postMessage) {
Barcode.postMessage(message);
}
}
window.onload = function () {
document.getElementById('btnBarcode').onclick = function () {
scanBarcode("Scan Barcode");
}
}
我已经成功获得了 Dart 文件中 javascript 通道 _scanBarcode 中的条码扫描结果。
现在我想在 scanBarcode 函数中将此条码结果回调回 JS 文件。
我研究了这么多时间,却一无所获。
我被困在这里。谁能帮我??非常感谢您。
【问题讨论】:
标签: javascript flutter dart callback flutter-web