为什么要调用js
1. flutter–web
1. 引入js web/index.html
<!-- Add the required JS libraries -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.2.0/crypto-js.min.js" integrity="sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.2.0/md5.min.js" integrity="sha512-ENWhXy+lET8kWcArT6ijA6HpVEALRmvzYBayGL6oFWl96exmq8Fjgxe2K6TAblHLP75Sa/a1YjHpIZRt+9hGOQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<!-- Register the js file where the logic is written -->
<script src="CryptoEnc.js" type="application/javascript"></script>
2. 创建工具js web/CryptoEnc.js
function CryptoEnc() {}
CryptoEnc.prototype.encrypt = function(toEncObj){
var toEnc = toEncObj.value;
var encrypted = CryptoJS.MD5(toEnc);
return encrypted;
}
CryptoEnc.prototype.testFunc = function(toEncObj2){
var toEnc = toEncObj2.value;
return "========bbbbtestFunc"+toEnc;
}
3. 创建对应的lib/js/js_interop.dart
// #1
()
library js_interop;
// The above two lines are required
import 'package:js/js.dart';
// #2
()
class CryptoEnc {
external CryptoEnc();
external String encrypt(ToEncrypt toEncrypt);
external String testFunc(ToEncrypt2 toEncrypt2);
}
// #3
()
class ToEncrypt {
external String get value;
external factory ToEncrypt({String value});
}
()
class ToEncrypt2 {
external String get value;
external factory ToEncrypt2({String value});
}
4. 由于引入的js是针对web平台的,所以引入需要做引入处理
///encrypt.dart
class ToEncrypt {
final String value;
ToEncrypt({
required this.value,
});
}
class ToEncrypt2 {
final String value;
ToEncrypt2({
required this.value,
});
}
class CryptoEnc {
CryptoEnc();
String encrypt(ToEncrypt toEncrypt) {
// We are not implementing any encryption for mobile for now.
// This is just for demonstration.
throw UnimplementedError();
}
String testFunc(ToEncrypt2 toEncrypt) {
// We are not implementing any encryption for mobile for now.
// This is just for demonstration.
throw UnimplementedError();
}
}
///export_encrypt.dart
export 'encrypt.dart' if (dart.library.js) 'js_interop.dart';
5. 使用
var encVal = CryptoEnc().encrypt(
ToEncrypt(
value: "aaaaaa",
),
);
var encVal2 = CryptoEnc().testFunc(ToEncrypt2(
value: "cccc",
));
print(encVal);
print("testFunc=$encVal2");
2. Android
1. 引入依赖
webview_flutter: ^4.4.2
2. index.html
<!DOCTYPE html>
<html>
<head>
<title>Test js dart</title>
</head>
<body>
<script>
function inputClick (url) {
console.log('inputClick=>'+url);
}
function playUrl(url) {
console.log('playUrl=>'+url);
}
function startPlay() {
console.log('startPlay');
}
function postMsg() {
console.log('postMsg');
//向dart发送消息
Print.postMessage('postMsg');
}
</script>
</body>
</html>
3. dart
import 'package:flutter/material.dart';
import 'package:tvboxstudy/log_extensions.dart';
import 'package:webview_flutter/webview_flutter.dart';
class LocalHtmlWebView extends StatefulWidget {
const LocalHtmlWebView({super.key});
LocalHtmlWebViewState createState() => LocalHtmlWebViewState();
}
class LocalHtmlWebViewState extends State<LocalHtmlWebView> {
late String localHtmlContents;
late WebViewController controller;
ValueNotifier<bool> isShowLoading = ValueNotifier(true);
void initState() {
super.initState();
initController();
loadLocalHtml();
_registerJavascriptChannel();
}
void runJS(int type) {
if (type == 1) {
controller.runJavaScript(
'inputClick();',
);
} else if (type == 2) {
controller.runJavaScript(
'startPlay();',
);
}else if (type == 3) {
controller.runJavaScript(
'postMsg();',
);
} else if (type == 4) {
controller.runJavaScript(
"playUrl('https://media.w3.org/2010/05/sintel/trailer.mp4');",
);
}
}
void _registerJavascriptChannel() {
controller.addJavaScriptChannel(
'Print',
onMessageReceived: (JavaScriptMessage message) {
//收到消息做相应的处理
print("onMessageReceived=>${message.message}");
},
);
}
void initController() {
controller = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setBackgroundColor(Colors.transparent)
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setNavigationDelegate(
NavigationDelegate(
onProgress: (int progress) {
"progress=$progress".log();
if (progress == 100) {
isShowLoading.value = false;
}
},
onPageStarted: (String url) {},
onPageFinished: (String url) {
controller
.runJavaScriptReturningResult('document.body.scrollHeight')
.then((value) {
"scrollHeight=>$value".log();
});
},
onWebResourceError: (WebResourceError error) {},
onNavigationRequest: (NavigationRequest request) {
if (request.url.startsWith('https://www.youtube.com/')) {
return NavigationDecision.prevent;
}
return NavigationDecision.navigate;
},
),
);
}
void loadLocalHtml() async {
controller.loadFlutterAsset('assets/www/player/index2.html');
}
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Row(
children: [
TextButton(onPressed: (){
runJS(1);
}, child: Text("inputClick")),
TextButton(onPressed: (){
runJS(2);
}, child: Text("startPlay")),
TextButton(onPressed: (){
runJS(3);
}, child: Text("postMsg")),
TextButton(onPressed: (){
runJS(4);
}, child: Text("playUrl")),
],
),
Expanded(
child: Stack(
children: [
WebViewWidget(
controller: controller,
),
ValueListenableBuilder(
valueListenable: isShowLoading,
builder: (BuildContext context, bool value, Widget? child) {
return isShowLoading.value
? const Center(
child: CircularProgressIndicator(
color: Colors.white,
))
: const SizedBox.shrink();
},
)
],
),
),
],
),
);
}
}
引用
Utilizing JS Library for Flutter Web
原文地址:https://blog.csdn.net/yujunlong3919/article/details/134661019
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_31876.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。