【Flutter学习】基本组件之Webview组件
1.添加依赖
dependencies:
flutter_webview_plugin: ^0.2.+
2.导入库
import 'import 'package:flutter_webview_plugin/flutter_webview_plugin.dart'; //导入前需要配置
3.属性
const WebviewScaffold({
Key key,
this.appBar,
@required this.url,
this.headers,//
this.withJavascript,//是否允许执行js代码
this.clearCache,//
this.clearCookies,//
this.enableAppScheme,//
this.userAgent,//
this.primary = true,//
this.persistentFooterButtons,//
this.bottomNavigationBar,//
this.withZoom,//是否允许网页缩放
this.withLocalStorage,//是否允许LocalStorage
this.withLocalUrl,//
this.scrollBar,//是否显示滚动条
this.supportMultipleWindows,//
this.appCacheEnabled,//
this.hidden = false,//
this.initialChild,//
this.allowFileURLs,//
this.resizeToAvoidBottomInset = false,//
this.invalidUrlRegex,//
this.geolocationEnabled//
})
4.使用方法
FlutterWebviewPlugin 插件提供一个链接到唯一webview的单一实例,这样你就可以在app中的任何地方控制webview
import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
import 'package:http/http.dart' as http; class Widget_WebView_Page extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return Widget_WebView_State();
}
} class Widget_WebView_State extends State<Widget_WebView_Page> with SingleTickerProviderStateMixin {
FlutterWebviewPlugin flutterWebviewPlugin = FlutterWebviewPlugin();
var title = "WebView组件";
var tabs;
TabController controller;
var choiceIndex = ; //获取h5页面标题
Future<String> getWebTitle() async {
String script = 'window.document.title';
var title = await
flutterWebviewPlugin.evalJavascript(script);
setState(() {
this.title = title;
print('#################### $title');
});
} //获取h5页面标题
Future<String> getWebTitle2({String url}) async {
var client = http.Client();
client.get(url).then(
(response) {
String title = RegExp( r"<[t|T]{1}[i|I]{1}[t|T]{1}[l|L]{1}[e|E]{1}(\s.*)?>([^<]*)</[t|T]{1}[i|I]{1}[t|T]{1}[l|L ]{1}[e|E]{1}>").stringMatch(response.body);
if (title != null) {
title = title.substring(title.indexOf('>') + , title.lastIndexOf("<"));
} else {
title = "";
}
print("#################### " + title);
}
).catchError(
(error) {
print(error);
}
).whenComplete(client.close,);
} @override
void initState() {
super.initState(); /**
* 监听web页加载状态
*/
flutterWebviewPlugin.onStateChanged.listen(
(WebViewStateChanged webViewState) async {
// setState(() {
// title = webViewState.type.toString();
// });
switch (webViewState.type) {
case WebViewState.finishLoad:{
handleJs();
getWebTitle();
}
break;
case WebViewState.shouldStart:
break;
case WebViewState.startLoad:
break;
case WebViewState.abortLoad:
break;
}
}); /**
* 监听页面加载url
*/
flutterWebviewPlugin.onUrlChanged.listen((String url) {
// getWebTitle(url: url); // setState(() {
// title = url;
// });
}); /**
* 监听x轴滑动距离
* 好像没什么用
*/
// flutterWebviewPlugin.onScrollXChanged.listen((double offsetX) {
// title = offsetX.toString();
// }); // flutterWebviewPlugin.onScrollYChanged.listen((double offsetY) {
// title = offsetY.toString();
// }); tabs = <Widget>[
Tab(
child: GestureDetector(
child: Text("刷新"),
onTap: () {
flutterWebviewPlugin.reload();
},
),
),
Tab(
child: GestureDetector(
child: Text("返回"),
onTap: () {
flutterWebviewPlugin.goBack();
},
),
),
Tab(
child: GestureDetector(
child: Text("加载指定url"),
onTap: () {
flutterWebviewPlugin.reloadUrl("https://www.360.com");
},
),
),
];
controller =
TabController(initialIndex: , length: tabs.length, vsync: this);
} @override
Widget build(BuildContext context) {
return WebviewScaffold(
url: "http://www.baidu.com",
//默认加载地址
appBar: AppBar(
title: Text(title),
backgroundColor: Colors.grey,
leading: GestureDetector(
child: Icon(Icons.arrow_back),
onTap: () {
flutterWebviewPlugin.close();
},
),
bottom: TabBar(
tabs: tabs,
controller: controller,
indicatorColor: Colors.red,
),
actions: <Widget>[],
),
bottomNavigationBar: BottomNavigationBar(
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text(
"首页", /*style: TextStyle(color: Color(0xff333333)),*/
),
activeIcon: Icon(
Icons.home,
color: Color(0xffDE331F),
// size: 30.0,
),
backgroundColor: Color(0xffff0000),
),
BottomNavigationBarItem(
icon: Icon(Icons.devices_other),
title: Text(
"其他", /*style: TextStyle(color: Color(0xff333333)),*/
),
activeIcon: Icon(
Icons.devices_other,
color: Color(0xffDE331F),
// size: 30.0,
),
backgroundColor: Color(0xffff0000),
),
],
currentIndex: choiceIndex,
fixedColor: Color(0xffDE331F),
// iconSize: 30.0,
// type: BottomNavigationBarType.fixed,
onTap: (index) {
if (index == ) {
setState(() {
choiceIndex = ;
flutterWebviewPlugin.reloadUrl("https://www.qq.com/");
});
} else {
setState(() {
flutterWebviewPlugin.reloadUrl("https://www.alipay.com/");
choiceIndex = ;
});
}
}
),
scrollBar: false,
withZoom: false,
);
} @override
void dispose() {
flutterWebviewPlugin.dispose();
super.dispose();
} void handleJs() {
flutterWebviewPlugin.evalJavascript(
"abc(${title}')"
).then((result) {});
}
}
五,webView其它用法
隐藏webview:
final flutterWebviewPlugin = new FlutterWebviewPlugin();
flutterWebviewPlugin.launch(url, hidden: true);
关闭webview:
flutterWebviewPlugin.close();
画一个内部矩形webview:
final flutterWebviewPlugin = new FlutterWebviewPlugin();
flutterWebviewPlugin.launch(url,
fullScreen: false,
rect: new Rect.fromLTWH(
0.0,
0.0,
MediaQuery.of(context).size.width,
300.0)
);
注意:webview并不存在于widget树中,所以你不能在webview中使用如snackbars, dialogs ...这些通知交互widget,更详细一些使用方法可以点击这里;
【Flutter学习】基本组件之Webview组件的更多相关文章
- Flutter学习笔记(9)--组件Widget
如需转载,请注明出处:Flutter学习笔记(9)--组件Widget 在Flutter中,所有的显示都是Widget,Widget是一切的基础,我们可以通过修改数据,再用setState设置数据(调 ...
- Flutter学习笔记(10)--容器组件、图片组件
如需转载,请注明出处:Flutter学习笔记(10)--容器组件.图片组件 上一篇Flutter学习笔记(9)--组件Widget我们说到了在Flutter中一个非常重要的理念"一切皆为组件 ...
- Flutter学习笔记(8)--Dart面向对象
如需转载,请注明出处:Flutter学习笔记(7)--Dart异常处理 Dart作为高级语言,支持面向对象的很多特性,并且支持基于mixin的继承方式,基于mixin的继承方式是指:一个类可以继承自多 ...
- Flutter学习笔记(11)--文本组件、图标及按钮组件
如需转载,请注明出处:Flutter学习笔记(10)--容器组件.图片组件 文本组件 文本组件(text)负责显示文本和定义显示样式,下表为text常见属性 Text组件属性及描述 属性名 类型 默认 ...
- Flutter学习笔记(12)--列表组件
如需转载,请注明出处:Flutter学习笔记(12)--列表组件 在日常的产品项目需求中,经常会有列表展示类的需求,在Android中常用的做法是收集数据源,然后创建列表适配器Adapter,将数据源 ...
- Flutter学习笔记(13)--表单组件
如需转载,请注明出处:Flutter学习笔记(13)--表单组件 表单组件是个包含表单元素的区域,表单元素允许用户输入内容,比如:文本区域,下拉表单,单选框.复选框等,常见的应用场景有:登陆.注册.输 ...
- Flutter学习笔记(15)--MaterialApp应用组件及routes路由详解
如需转载,请注明出处:Flutter学习笔记(15)--MaterialApp应用组件及routes路由详解 最近一段时间生病了,整天往医院跑,也没状态学东西了,现在是好了不少了,也该继续学习啦!!! ...
- Flutter学习笔记(16)--Scaffold脚手架、AppBar组件、BottomNavigationBar组件
如需转载,请注明出处:Flutter学习笔记(15)--MaterialApp应用组件及routes路由详解 今天的内容是Scaffold脚手架.AppBar组件.BottomNavigationBa ...
- Flutter学习笔记(18)--Drawer抽屉组件
如需转载,请注明出处:Flutter学习笔记(18)--Drawer抽屉组件 Drawer(抽屉组件)可以实现类似抽屉拉出和推入的效果,可以从侧边栏拉出导航面板.通常Drawer是和ListView组 ...
随机推荐
- 前端工具-调试压缩后的JS(Source Map的使用)
使用 Source Map 可以在 FF 的调试器. Chrome 的 Sources 和 VSCode 中给压缩前的文件下断点,也可以方便定位错误发生的位置(定位到压缩前的文件). 何为 Sourc ...
- 用SPSS做时间序列
用SPSS做时间序列 关于时间序列,有好多软件可以支持分析,大家比较熟悉的可能是EVIEWS.SPSS.还有STATA,具体用啥软件,结果都是一样的,但是SPSS作为一款学习简单,使用容易的软件还是值 ...
- js面向对象程序设计之属性和对象
写在博客之前的话,这是我这个刚毕业的菜鸟的第一篇博客.一口吃不成一个胖子,我也希望写的第一篇东西就让读的人醍醐灌顶.我会抱着怀疑的态度来看自己写的文章,如果有写错的地方,请大家不要被误导,如果有大神提 ...
- Python算法每日一题--002--求众数
给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3]输出: 3示 ...
- Python几行代码实现邮件发送
话不多说直接进入正题 首先我们需要安装一个名为'zmail'的包,终端执行'pip install zmail'即可实现安装. 直接上代码 import zmail mail = { 'subject ...
- 简单的C++11线程池实现
线程池的C++11简单实现,源代码来自Github上作者progschj,地址为:A simple C++11 Thread Pool implementation,具体博客可以参见Jakob's D ...
- parameterType和resultType配置错误
自己在写mapper.xml的时候 吧parameterType和resultType的两个类搞混了 对调了一下 以至于查询了半天查询不出结果 <select id="findPat ...
- P4929 【模板】舞蹈链(DLX)
题目背景 本题是舞蹈链模板——精确覆盖问题 题目描述 给定一个N行M列的矩阵,矩阵中每个元素要么是1,要么是0 你需要在矩阵中挑选出若干行,使得对于矩阵的每一列j,在你挑选的这些行中,有且仅有一行的第 ...
- 21、numpy—Matplotlib
NumPy Matplotlib Matplotlib 是 Python 的绘图库. 它可与 NumPy 一起使用,提供了一种有效的 MatLab 开源替代方案. 它也可以和图形工具包一起使用,如 P ...
- 04 volatile关键字实现原理
volatile关键字实现原理 1.volatile关键字的语义分析 作用:让其他线程能够马上感知到某个线程多某个变量的修改 保证可见性 对共享变量的修改,其他线程能够马上感知到 保证有序性 在重排序 ...