flutter笔记:使用flutter webvie
1.安装
flutter pub add webview_flutter
2.导入
import 'package:webview_flutter/webview_flutter.dart';
3.设置android/app/build.gradle
android { defaultConfig { minSdkVersion 19 } }
然后打开android/app/src/debug/AndroidManifest.xml
添加:
<application android:usesCleartextTraffic="true"> </application>
(如果不设置则在调试或编译时会出现版本不兼容错误)
4. 可以将webView插件写成一个单独的文件
新建一个dart文件WebPage.dart
编辑以下内容
import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:webview_flutter/webview_flutter.dart'; class WebPage extends StatelessWidget { final String _url; WebPage(this._url); @override Widget build(BuildContext context) { return Scaffold( body: SafeArea( child: WebView( initialUrl: _url, ), ), ); } }
在main.dart中导入和使用:
import 'package:flutter/material.dart'; import 'pages/WebPage.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'jcstdio', theme: ThemeData( primarySwatch: Colors.blueGrey, ), home: WebPage("http://thispage.tech:8001/#/")); } }
5.解决net::ERR_CLEARTEXT_NOT_PERMITTED网络无权限:
编辑android/app/src/main/AndroidManifest.xml
文件,添加:
android:usesCleartextTraffic="true",