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组件的更多相关文章

  1. Flutter学习笔记(9)--组件Widget

    如需转载,请注明出处:Flutter学习笔记(9)--组件Widget 在Flutter中,所有的显示都是Widget,Widget是一切的基础,我们可以通过修改数据,再用setState设置数据(调 ...

  2. Flutter学习笔记(10)--容器组件、图片组件

    如需转载,请注明出处:Flutter学习笔记(10)--容器组件.图片组件 上一篇Flutter学习笔记(9)--组件Widget我们说到了在Flutter中一个非常重要的理念"一切皆为组件 ...

  3. Flutter学习笔记(8)--Dart面向对象

    如需转载,请注明出处:Flutter学习笔记(7)--Dart异常处理 Dart作为高级语言,支持面向对象的很多特性,并且支持基于mixin的继承方式,基于mixin的继承方式是指:一个类可以继承自多 ...

  4. Flutter学习笔记(11)--文本组件、图标及按钮组件

    如需转载,请注明出处:Flutter学习笔记(10)--容器组件.图片组件 文本组件 文本组件(text)负责显示文本和定义显示样式,下表为text常见属性 Text组件属性及描述 属性名 类型 默认 ...

  5. Flutter学习笔记(12)--列表组件

    如需转载,请注明出处:Flutter学习笔记(12)--列表组件 在日常的产品项目需求中,经常会有列表展示类的需求,在Android中常用的做法是收集数据源,然后创建列表适配器Adapter,将数据源 ...

  6. Flutter学习笔记(13)--表单组件

    如需转载,请注明出处:Flutter学习笔记(13)--表单组件 表单组件是个包含表单元素的区域,表单元素允许用户输入内容,比如:文本区域,下拉表单,单选框.复选框等,常见的应用场景有:登陆.注册.输 ...

  7. Flutter学习笔记(15)--MaterialApp应用组件及routes路由详解

    如需转载,请注明出处:Flutter学习笔记(15)--MaterialApp应用组件及routes路由详解 最近一段时间生病了,整天往医院跑,也没状态学东西了,现在是好了不少了,也该继续学习啦!!! ...

  8. Flutter学习笔记(16)--Scaffold脚手架、AppBar组件、BottomNavigationBar组件

    如需转载,请注明出处:Flutter学习笔记(15)--MaterialApp应用组件及routes路由详解 今天的内容是Scaffold脚手架.AppBar组件.BottomNavigationBa ...

  9. Flutter学习笔记(18)--Drawer抽屉组件

    如需转载,请注明出处:Flutter学习笔记(18)--Drawer抽屉组件 Drawer(抽屉组件)可以实现类似抽屉拉出和推入的效果,可以从侧边栏拉出导航面板.通常Drawer是和ListView组 ...

随机推荐

  1. R 保存包含中文的 eps 图片--showtext

    来自统计之都,感谢 Ihavenothing(http://cos.name/cn/profile/81532) 详情参考:http://cos.name/cn/topic/151358?replie ...

  2. webService接口的py文件打包成exe

    (一)webService接口的py文件打包成exe,在python3.5版本.pyInstaller3.2版本.pywin32-219.win-amd64-py3.5版本打包时报错,原因可能是pyi ...

  3. 非典型T_SQL的总结

      ------over的两种常用的用法--- --第一种分组 当然要注意了,这里的分组并不是实际的分组,而是根据你的业务需求而坐的临时分组   select roomguid,Room, avg(t ...

  4. crontab自动执行任务,失败原因记录

    服务器上使用crontab部署这两个每分钟自动执行的命令.首先,这两个命令是之前的人部署的,在我接手之前,就一直在了的.根据命令,实际上应该是做到每分钟都执行一次脚本.但是实际操作中,却发现,其实并没 ...

  5. poj2010 Moo University - Financial Aid 优先队列

    Description Bessie noted that although humans have many universities they can attend, cows have none ...

  6. Go-内存To Be

    做一个快乐的互联网搬运工- 逃逸分析 逃逸分析的概念 在编译程序优化理论中,逃逸分析是一种确定指针动态范围的方法——分析在程序的哪些地方可以访问到指针. 它涉及到指针分析和形状分析. 当一个变量(或对 ...

  7. U盘修复教程--使用量产工具和芯片检测工具

    U盘修复教程(金士顿DT101) 本教程使用量产工具(量产可理解为强制恢复出厂设置),量产有风险(U盘彻底报废),根据教程所造成所有损失均与本文作者无关. ·第一步:下载ChipGenius_v4_0 ...

  8. Arrays工具类使用与源码分析(1)

    Arrays工具类主要是方便数组操作的,学习好该类可以让我们在编程过程中轻松解决数组相关的问题,简化代码的开发. Arrays类有一个私有的构造函数,没有对外提供实例化的方法,因此无法实例化对象.因为 ...

  9. 简单谈谈Netty的高性能之道

    传统RPC 调用性能差的三宗罪 网络传输方式问题:传统的RPC 框架或者基于RMI 等方式的远程服务(过程)调用采用了同步阻塞IO,当客户端的并发压力或者网络时延增大之后,同步阻塞IO 会由于频繁的w ...

  10. TCP协议-流量控制

    流量控制是通过滑动窗口来实现控制的.使用了坚持定时器,防止发送失败导致死锁.