Flutter?webview與網(wǎng)頁通訊交互實(shí)現(xiàn)
前言
在app開發(fā)中我們有JSBridge來實(shí)現(xiàn)app和網(wǎng)頁端通訊,現(xiàn)參考JSBridge實(shí)現(xiàn)了Flutter webview和網(wǎng)頁端通訊。
預(yù)覽
flutter
import 'package:ds_bridge/ds_bridge.dart'; class JsBridgeUtil { // 向H5調(diào)用接口 static executeMethod(flutterWebViewPlugin, String jsonStr) async{ DsBridge dsBridge = DsBridge(flutterWebViewPlugin); Result result = dsBridge.dispatch(jsonStr); if(result.method == 'share'){ result.callBack('收到網(wǎng)頁端share事件,內(nèi)容為${result.data}并返回此文本給網(wǎng)頁'); } if(result.method == 'share1'){ result.callBack('收到網(wǎng)頁端share1事件'); } } }
網(wǎng)頁
import 'dsbridge_flutter' dsBridge.call('share', { name: 'woyehaizaixiang' }, function (res) { alert(res) })
具體實(shí)現(xiàn)
flutter中使用ds_bridge
此包是flutter_webview_pluginwebview與網(wǎng)頁交互的工具包,需要先添加flutter_webview_plugin組件,具體flutter_webview_plugin組件使用請前往官網(wǎng)查看 ,在添加flutter_webview_plugin組件后,添加ds_bridge組件
1.配置依賴包
dependencies: ds_bridge: ^0.0.1
2.在webview頁面添加JavascriptChannel
webview.dart
import 'dart:async'; import 'package:flutter/material.dart'; import 'package:yundk_app/routes/application.dart'; import 'package:flutter_webview_plugin/flutter_webview_plugin.dart'; import '../../utils/JsBridgeUtil.dart'; class WebviewPage extends StatefulWidget { final String url; final VoidCallback backCallback; WebviewPage({ Key key, @required this.url, this.backCallback, }) : super(key: key); @override _WebviewPageState createState() => _WebviewPageState(); } class _WebviewPageState extends State<WebviewPage> { String _title = ''; final flutterWebViewPlugin = FlutterWebviewPlugin(); final Set<JavascriptChannel> jsChannels = [ JavascriptChannel( name: 'DsBridgeApp', onMessageReceived: (JavascriptMessage msg) { String jsonStr = msg.message; JsBridgeUtil.executeMethod(FlutterWebviewPlugin(), jsonStr); }), ].toSet(); StreamSubscription<String> _onUrlChanged; @override void initState() { super.initState(); flutterWebViewPlugin.close(); // 監(jiān)聽 url changed _onUrlChanged = flutterWebViewPlugin.onUrlChanged.listen((String url) async { }); // 監(jiān)聽頁面onload flutterWebViewPlugin.onStateChanged.listen((viewState) async { if (viewState.type == WebViewState.finishLoad) { flutterWebViewPlugin.evalJavascript('document.title').then((result) => { setState(() { _title = result; }) }); } }); } @override void dispose() { _onUrlChanged.cancel(); flutterWebViewPlugin.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( body: WebviewScaffold( appBar: new AppBar( leading: IconButton( hoverColor: Colors.transparent, highlightColor: Colors.transparent, icon: Icon(const IconData(0xe61b, fontFamily: 'IconFont'), color: Color(0xff333333), size: 18), onPressed: (){ flutterWebViewPlugin.close(); Application.router.pop(context); }, ), title: new Text( _title, style: TextStyle(color: Color(0xff333333), fontSize: 17), ), actions: [ new IconButton( icon: new Icon( Icons.refresh_outlined, color: Color(0xff333333), size: 20 ), onPressed: () { flutterWebViewPlugin.reload(); } ), ], centerTitle: true, elevation: 0, ), url: widget.url, javascriptChannels: jsChannels, mediaPlaybackRequiresUserGesture: false, withZoom: true, withLocalStorage: true, hidden: true, ) ); } }
3.在JsBridgeUtil類中
utils/JsBridgeUtil.dart
import 'package:ds_bridge/ds_bridge.dart'; class JsBridgeUtil { // 向H5調(diào)用接口 static executeMethod(flutterWebViewPlugin, String jsonStr) async{ DsBridge dsBridge = DsBridge(flutterWebViewPlugin); Result result = dsBridge.dispatch(jsonStr); if(result.method == 'share'){ result.callBack('收到網(wǎng)頁端share事件,內(nèi)容為${result.data}并返回此文本給網(wǎng)頁'); } if(result.method == 'share1'){ result.callBack('收到網(wǎng)頁端share1事件'); } } }
網(wǎng)頁端使用dsbridge_flutter
此插件為網(wǎng)頁端插件,配合flutter端ds_bridge一起使用
1.安裝
npm install dsbridge_flutter
2.使用
import 'dsbridge_flutter' dsBridge.call(<String> method, <Object> data, <Function> callback)
3.例子
import 'dsbridge_flutter' dsBridge.call('share', { name: 'woyehaizaixiang' }, function (res) { alert(res) })
總結(jié)
本文參考JSBridge實(shí)現(xiàn)了Flutter webview和網(wǎng)頁端交互。具體用到的插件有Flutter端flutter_webview_plugin和ds_bridge、網(wǎng)頁端有dsbridge_flutter。
到此這篇關(guān)于Flutter webview與網(wǎng)頁通訊交互實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Flutter webview與網(wǎng)頁通訊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android開發(fā)中獲取View視圖寬與高的常用方法小結(jié)
這篇文章主要介紹了Android開發(fā)中獲取View視圖寬與高的常用方法,結(jié)合實(shí)例形式總結(jié)分析了Android獲取View視圖寬與高的三種常用方法及使用場景,需要的朋友可以參考下2017-10-10Android實(shí)現(xiàn)回彈ScrollView的原理
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)回彈ScrollView的原理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Android 線程之自定義帶消息循環(huán)Looper的實(shí)例
這篇文章主要介紹了Android 線程之自定義帶消息循環(huán)Looper的實(shí)例的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-10-10使用RecyclerView實(shí)現(xiàn)瀑布流高度自適應(yīng)
這篇文章主要為大家詳細(xì)介紹了使用RecyclerView實(shí)現(xiàn)瀑布流高度自適應(yīng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09Android中ViewPager獲取當(dāng)前顯示的Fragment
這篇文章主要介紹了Android中ViewPager獲取當(dāng)前顯示的Fragment的兩種方法,一種是使用 getSupportFragmentManager().findFragmentByTag()方法,另一種是重寫適配器的 setPrimaryItem()方法,有需要的朋友可以參考借鑒,下面來一起看看吧。2017-01-01kotlin實(shí)現(xiàn)通知欄提醒功能示例代碼
這篇文章主要給大家介紹了關(guān)于kotlin實(shí)現(xiàn)通知欄提醒功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用kotlin具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Android自定義通用標(biāo)題欄CustomTitleBar
這篇文章主要為大家詳細(xì)介紹了Android自定義通用標(biāo)題欄CustomTitleBar,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11打飛機(jī)游戲終極BOSS Android實(shí)戰(zhàn)打飛機(jī)游戲完結(jié)篇
打飛機(jī)游戲終極BOSS,Android實(shí)戰(zhàn)打飛機(jī)游戲完結(jié)篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07