admin 管理员组文章数量: 1086019
添加pubspec.yaml依赖
url_launcher: ^5.4.1
webview_flutter: ^0.3.18+1
--------------main.dart
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:webview_flutter/webview_flutter.dart';
void main() => runApp(MyApp());
const String TITLE='whq_test';
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: TITLE,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: TITLE),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _title = TITLE;
WebViewController _webViewController=null;
@override
void initState() {
super.initState();
}
void _openApp(openurl) async{
// Android
print("open app");
final url = 'vnd.'+openurl;
if (await canLaunch(url)) {
await launch(url);
} else {
// Ios
final url = openurl;
if(await canLaunch(url)){
await launch(url);
}else{
throw 'Could not launch $url';
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(_title),
),
body: WebView(
initialUrl: "http://whqtest/app/test.html",
javascriptMode: JavascriptMode.unrestricted,
navigationDelegate: (NavigationRequest request){
if (request.url.startsWith('js://webview')) {
print('blocking navigation to $request}');
_openApp(request.url.replaceAll("js://webview", "").replaceAll("?url=", ""));
return NavigationDecision.prevent;
}
print('allowing navigation to $request');
return NavigationDecision.navigate;
},
onWebViewCreated: (WebViewController webViewController){
_webViewController=webViewController;
},
onPageFinished: (url){
_webViewController.evaluateJavascript("document.title").then((result){
String title = result.substring(1, result.length - 1);
if (title.length > 0) {
print(title);
setState(() {
_title = title;
});
}
});
},
),
);
}
@override
void dispose(){
super.dispose();
_webViewController=null;
}
}
————————test.html
hello world!
<a href="https://www.baidu">百度</a>
<a href="js://webview?url=weixin://">微信</a>
本文标签: 第三方 器及 WebView Flutter JS
版权声明:本文标题:flutter webview浏览器及与js交互、打开第三方app 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/b/1740072280a2128985.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论