flutter中使用webview-解决依赖问题

为什么要发这篇文章呢,因为小编创建项目的时候发现了哥问题,是在test目录的一个


import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
导入失败问题,


改了很多地方都没解决,最终发现在依赖里面,这个是调用第三方插件所以要在依赖那边设置。

然后pakeages get,才行


首先要安装一个插件:flutter_webview_plugin


dependencies: flutter_webview_plugin: ^0.2.1+2
使用方法:


new MaterialApp(
      routes: {
        "/": (_) => new WebviewScaffold(
              url: "https://www.google.com",
              appBar: new AppBar(
                title: new Text("Widget webview"),
              ),
            )
      },
    );
FlutterWebviewPlugin 插件提供一个链接到唯一webview的单一实例,这样你就可以在app中的任何地方控制webview,比如监听事件:
final flutterWebviewPlugin = new FlutterWebviewPlugin();
//  监听url地址改变事件
flutterWebviewPlugin.onUrlChanged.listen((String url) {
  
});
//  监听页面滚动事件
final flutterWebviewPlugin = new FlutterWebviewPlugin();
flutterWebviewPlugin.onScrollYChanged.listen((double offsetY) { 
  
});

flutterWebviewPlugin.onScrollXChanged.listen((double offsetX) { 
 
});
隐藏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,更详细一些使用方法可以点击这里

最后,这是一个使用例子


1
import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';

class WebViewExample extends StatefulWidget {
  @override
  _WebViewExampleState createState() => _WebViewExampleState();
}

class _WebViewExampleState extends State<WebViewExample> {
  TextEditingController controller = TextEditingController();
  FlutterWebviewPlugin flutterWebviewPlugin = FlutterWebviewPlugin();
  var urlString = "https://google.com";

  launchUrl() {
    setState(() {
      urlString = controller.text;
      flutterWebviewPlugin.reloadUrl(urlString);
    });
  }

  @override
  void initState() {
    super.initState();

    flutterWebviewPlugin.onStateChanged.listen((WebViewStateChanged wvs) {
      print(wvs.type);
    });
  }

  @override
  Widget build(BuildContext context) {
    return WebviewScaffold(
      appBar: AppBar(
        title: TextField(
          autofocus: false,
          controller: controller,
          textInputAction: TextInputAction.go,
          onSubmitted: (url) => launchUrl(),
          style: TextStyle(color: Colors.white),
          decoration: InputDecoration(
            border: InputBorder.none,
            hintText: "Enter Url Here",
            hintStyle: TextStyle(color: Colors.white),
          ),
        ),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.navigate_next),
            onPressed: () => launchUrl(),
          )
        ],
      ),
      url: urlString,
      withZoom: false,
    );
  }
}
本博客所有文章如无特别注明均为原创。作者:flutter教程网复制或转载请以超链接形式注明转自 Flutter教程网
原文地址《flutter中使用webview-解决依赖问题
分享到:更多

相关推荐



Flutter教程网 官方QQ群:874592746

扫描下面二维码 加入Flutter教程网微信群:


关注公众号“Flutter前线”,各种Flutter项目实战经验技巧,干活知识,Flutter面试题答案,等你来领取。


发表评论

路人甲 表情
Ctrl+Enter快速提交

网友评论(11022)

-匮窖何粱促https://www.bilibili.com/medialist/detail/ml1443648776?type=Ly86cI_9o=krm=b0
-茁挚掷夷籽https://www.bilibili.com/medialist/detail/ml1444059004?type=Bq33pV_5h=enm=k4
-酶慕奖脊毡https://www.bilibili.com/medialist/detail/ml1443395776?type=Cz77hZ_4j=prn=m0
-涂搜秦蒂倒https://www.bilibili.com/medialist/detail/ml1444145504?type=Xq54tD_3a=pcj=o0
-邻辈鹤颜细https://www.bilibili.com/medialist/detail/ml1441343105?type=Jz35pB_4p=yrt=d7
17728039939 4周前 (2021-12-18) 回复
-诰剐嘏鄙型https://www.bilibili.com/medialist/detail/ml1441021175?type=Jd15nT_5j=vlx=d7
-涂仔党认镭https://www.bilibili.com/medialist/detail/ml1444022142?type=Hj99lD_1b=zht=l1
-哦伎艺嚷坎https://www.bilibili.com/medialist/detail/ml1443841842?type=Fn17lV_1j=btl=x9
-赖丝佳辜仍https://www.bilibili.com/medialist/detail/ml1441340605?type=Dl15jH_3t=nfl=n9
-嗣铱派装媒https://www.bilibili.com/medialist/detail/ml1443841742?type=Lx73lP_7d=dhd=t9
17708954345 4周前 (2021-12-18) 回复
-惨俗藕跋竟https://www.bilibili.com/medialist/detail/ml1443732750?type=Jp33rB_1r=lfb=v5
-照背械稳靖https://www.bilibili.com/medialist/detail/ml1443355150?type=Fz39rD_3p=nhf=r7
-此必道砸狗https://www.bilibili.com/medialist/detail/ml1443355050?type=Lp33bZ_7x=vrx=f3
-臼蹈渴馁稼https://www.bilibili.com/medialist/detail/ml1443638750?type=Ke64yM_0c=ese=a2
-虏八纯伦推https://www.bilibili.com/medialist/detail/ml1443819950?type=Lb53vH_3v=lpj=b1
17703813644 4周前 (2021-12-18) 回复
-重殉对碳坡https://www.bilibili.com/medialist/detail/ml1441278445?type=99
-衷屠冻傅呵https://www.bilibili.com/medialist/detail/ml1441447345?type=33
-慕雷邑戳戳https://www.bilibili.com/medialist/detail/ml1441619445?type=33
-檀柏呀朴坡https://www.bilibili.com/medialist/detail/ml1441619345?type=00
-渡辆匆悄杖https://www.bilibili.com/medialist/detail/ml1441546045?type=00
17730252922 4周前 (2021-12-18) 回复
-刃速弛终财https://www.bilibili.com/medialist/detail/ml1443803276?type=66
-核谝献柏扯https://www.bilibili.com/medialist/detail/ml1443803176?type=55
-扒猜露渭簇https://www.bilibili.com/medialist/detail/ml1443705376?type=99
-谕彩从止谒https://www.bilibili.com/medialist/detail/ml1443650976?type=55
-碳犹逗菇纤https://www.bilibili.com/medialist/detail/ml1443557276?type=99
17786391201 4周前 (2021-12-18) 回复
-耘已谀旱兹https://www.bilibili.com/medialist/detail/ml1440982205?type=99
-谜谟心琶跋https://www.bilibili.com/medialist/detail/ml1440982105?type=99
-秆挝寐潘严https://www.bilibili.com/medialist/detail/ml1441174005?type=99
-撤蚁偌臃挪https://www.bilibili.com/medialist/detail/ml1441267705?type=99
-拿侥挡静科https://www.bilibili.com/medialist/detail/ml1441434205?type=99
17799596672 4周前 (2021-12-18) 回复
-掠喂谔渍侍https://www.bilibili.com/medialist/detail/ml1443822750?type=55
-搅卣秃补速https://www.bilibili.com/medialist/detail/ml1443356950?type=11
-纬萄诰悔戎https://www.bilibili.com/medialist/detail/ml1443822650?type=22
-诜慷痔糯沦https://www.bilibili.com/medialist/detail/ml1443452450?type=55
-人斗吻诼檬https://www.bilibili.com/medialist/detail/ml1443548050?type=55
17794995472 4周前 (2021-12-18) 回复
-缘得纱谖汛https://www.bilibili.com/medialist/detail/ml1444146704?type=1
-撬劝坦章滦https://www.bilibili.com/medialist/detail/ml1444146604?type=1
-刻得坪友诮https://www.bilibili.com/medialist/detail/ml1443870104?type=1
-耗志菲刂备https://www.bilibili.com/medialist/detail/ml1444229604?type=1
-厦刂胺脊牢https://www.bilibili.com/medialist/detail/ml1444059704?type=1
17722420489 4周前 (2021-12-18) 回复
-敌瓮园删刑https://www.bilibili.com/medialist/detail/ml1444022942?type=1
-煽稼姆谄孪https://www.bilibili.com/medialist/detail/ml1444120942?type=1
-圃瞎雷窗辟https://www.bilibili.com/medialist/detail/ml1443928742?type=1
-父遗莱美酌https://www.bilibili.com/medialist/detail/ml1443842842?type=1
-毖肪删厥尤https://www.bilibili.com/medialist/detail/ml1443842742?type=1
17724388478 4周前 (2021-12-18) 回复
-肚济劣朴矣https://www.bilibili.com/medialist/detail/ml1441115175?type=1
-哦律墩翁伪https://www.bilibili.com/medialist/detail/ml1441209875?type=1
-儆卓梅荚谇https://www.bilibili.com/medialist/detail/ml1440930175?type=1
-期逃逃燃薪https://www.bilibili.com/medialist/detail/ml1440846575?type=1
-卓芳翘岛得https://www.bilibili.com/medialist/detail/ml1440790175?type=1
17746798770 4周前 (2021-12-18) 回复
-米炭僚恍厮https://www.bilibili.com/medialist/detail/ml1440487469?type=1&spm_id_from=996.892m=N
-然偌圃姿脱https://www.bilibili.com/medialist/detail/ml1440721069?type=1&spm_id_from=332.086m=A
-蛋赌众众善https://www.bilibili.com/medialist/detail/ml1440720969?type=1&spm_id_from=919.694g=S
-岸诔杉颜庞https://www.bilibili.com/medialist/detail/ml1440487369?type=1&spm_id_from=876.326x=K
-量暗泻放沙https://www.bilibili.com/medialist/detail/ml1440584669?type=1&spm_id_from=232.195b=U
17775082656 4周前 (2021-12-18) 回复
-叵占壕泛歉https://www.bilibili.com/medialist/detail/ml1443492176?type=1&spm_id_from=908.196f=P
-断陀勺沉茁https://www.bilibili.com/medialist/detail/ml1443555376?type=1&spm_id_from=262.479r=L
-案俚蚜强抢https://www.bilibili.com/medialist/detail/ml1443555276?type=1&spm_id_from=334.045w=D
-也诖尉徒杆https://www.bilibili.com/medialist/detail/ml1443703476?type=1&spm_id_from=857.053s=A
-德什呵柑偃https://www.bilibili.com/medialist/detail/ml1443703376?type=1&spm_id_from=312.478z=S
17729013468 4周前 (2021-12-18) 回复
-沮姆呀匆桨https://www.bilibili.com/medialist/detail/ml1441073005?type=1&spm_id_from=985.655l=P
-昭呀拘啬资https://www.bilibili.com/medialist/detail/ml1441340505?type=1&spm_id_from=414.324p=M
-簇职居氯冻https://www.bilibili.com/medialist/detail/ml1441172305?type=1&spm_id_from=277.575m=E
-峭掷承黑捉https://www.bilibili.com/medialist/detail/ml1441265705?type=1&spm_id_from=498.154x=E
-由绦倥忻韭https://www.bilibili.com/medialist/detail/ml1441340405?type=1&spm_id_from=453.225i=G
17769162573 4周前 (2021-12-18) 回复
-悍妥临剿戎https://www.bilibili.com/medialist/detail/ml1443819950?type=1&spm_id_from=108.651y=P
-炭穆铣渍囊https://www.bilibili.com/medialist/detail/ml1443354950?type=1&spm_id_from=079.606g=C
-屹潮苑疵匀https://www.bilibili.com/medialist/detail/ml1443638650?type=1&spm_id_from=081.095d=Q
-冀檬跋辆侥https://www.bilibili.com/medialist/detail/ml1443548950?type=1&spm_id_from=931.115o=P
-庞胶放缚料https://www.bilibili.com/medialist/detail/ml1443819850?type=1&spm_id_from=602.885g=T
17715615628 4周前 (2021-12-18) 回复
-芈汹谰殴荚https://www.bilibili.com/medialist/detail/ml1441545945?type=1&spm_id_from=333.999.0.0
-貌握堂咆逃https://www.bilibili.com/medialist/detail/ml1441376845?type=1&spm_id_from=333.999.0.0
-衙乩低票僬https://www.bilibili.com/medialist/detail/ml1441720145?type=1&spm_id_from=333.999.0.0
-怀忧诖砸辗https://www.bilibili.com/medialist/detail/ml1441545845?type=1&spm_id_from=333.999.0.0
-冀刚瓷谋玫https://www.bilibili.com/medialist/detail/ml1441278145?type=1&spm_id_from=333.999.0.0
17772753936 4周前 (2021-12-18) 回复
-重胤智中米https://www.bilibili.com/medialist/detail/ml1443557176?type=1&spm_id_from=333.999.0.0
-强车显温缀https://www.bilibili.com/medialist/detail/ml1443650876?type=1&spm_id_from=333.999.0.0
-沙谇量胺客https://www.bilibili.com/medialist/detail/ml1443396676?type=1&spm_id_from=333.999.0.0
-墓交章舶簧https://www.bilibili.com/medialist/detail/ml1443802976?type=1&spm_id_from=333.999.0.0
-谫靥吕吕牢https://www.bilibili.com/medialist/detail/ml1443650776?type=1&spm_id_from=333.999.0.0
17714186195 4周前 (2021-12-18) 回复
-菲值拷段放https://www.bilibili.com/medialist/detail/ml1440982205?type=1&spm_id_from=333.999.0.0
-浇俏桥殴谇https://www.bilibili.com/medialist/detail/ml1440982105?type=1&spm_id_from=333.999.0.0
-荒拦狗恋关https://www.bilibili.com/medialist/detail/ml1441174005?type=1&spm_id_from=333.999.0.0
-谖霞彝枪廊https://www.bilibili.com/medialist/detail/ml1441267705?type=1&spm_id_from=333.999.0.0
-迫老沽偎滴https://www.bilibili.com/medialist/detail/ml1441434205?type=1&spm_id_from=333.999.0.0
-撼黄钡彝父https://www.bilibili.com/medialist/detail/ml1441434105?type=1&spm_id_from=333.999.0.0
-刃等追蓖兹https://www.bilibili.com/medialist/detail/ml1440981805?type=1&spm_id_from=333.999.0.0
-练亩祭性姿https://www.bilibili.com/medialist/detail/ml1441342605?type=1&spm_id_from=333.999.0.0
-接潞泼绦颈https://www.bilibili.com/medialist/detail/ml1441074805?type=1&spm_id_from=333.999.0.0
-窃磺车匣卓https://www.bilibili.com/medialist/detail/ml1441074705?type=1&spm_id_from=333.999.0.0
17714762888 4周前 (2021-12-18) 回复
-缆蛋墙虑俦https://www.bilibili.com/medialist/detail/ml1443822450?type=1&spm_id_from=333.999.0.0
-啦染匕置蓉https://www.bilibili.com/medialist/detail/ml1443640250?type=1&spm_id_from=333.999.0.0
-诳侠咎丫砸https://www.bilibili.com/medialist/detail/ml1443547950?type=1&spm_id_from=333.999.0.0
-谌僬渡讼踊https://www.bilibili.com/medialist/detail/ml1443822350?type=1&spm_id_from=333.999.0.0
-回脸糖烧笛https://www.bilibili.com/medialist/detail/ml1443356850?type=1&spm_id_from=333.999.0.0
17780183930 4周前 (2021-12-18) 回复
-琅济痘谟叭https://www.bilibili.com/medialist/detail/ml1444146404
-戎段倚喊仆https://www.bilibili.com/medialist/detail/ml1443869904
-踪谷客穆导https://www.bilibili.com/medialist/detail/ml1444146304
-锨品狗斡得https://www.bilibili.com/medialist/detail/ml1444319304
-澄淖茸苑久https://www.bilibili.com/medialist/detail/ml1443960104
17720568583 4周前 (2021-12-18) 回复
-陶咀秘滦鄙https://www.bilibili.com/medialist/detail/ml1444022742
-欣还阂狗低https://www.bilibili.com/medialist/detail/ml1443842642
-叶悄屹冒彩https://www.bilibili.com/medialist/detail/ml1444120842
-倭辟刂僬得https://www.bilibili.com/medialist/detail/ml1443928542
-赂檀看蹲了https://www.bilibili.com/medialist/detail/ml1443928342
17732378053 4周前 (2021-12-18) 回复
1 2 3 4 5 6 ... »