Flutter 加载url flutter_inappwebview
Posted 安果移不动
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flutter 加载url flutter_inappwebview相关的知识,希望对你有一定的参考价值。
效果:
flutter_inappwebview: ^5.3.2
main.dart
void main() async {
//webview加载
_initWebView();
await SpUtil.getInstance();
runApp(MyApp());
}
Future<void> _initWebView() async {
WidgetsFlutterBinding.ensureInitialized();
if (Platform.isandroid) {
await AndroidInAppWebViewController.setWebContentsDebuggingEnabled(true);
var swAvailable = await AndroidWebViewFeature.isFeatureSupported(
AndroidWebViewFeature.SERVICE_WORKER_BASIC_USAGE);
var swInterceptAvailable = await AndroidWebViewFeature.isFeatureSupported(
AndroidWebViewFeature.SERVICE_WORKER_SHOULD_INTERCEPT_REQUEST);
if (swAvailable && swInterceptAvailable) {
AndroidServiceWorkerController serviceWorkerController =
AndroidServiceWorkerController.instance();
serviceWorkerController.serviceWorkerClient = AndroidServiceWorkerClient(
shouldInterceptRequest: (request) async {
print(request);
return null;
},
);
}
}
}
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
class WebPage extends StatefulWidget {
final String url;
final String title;
const WebPage({Key key, this.url, this.title}) : super(key: key);
@override
_WebPageState createState() => _WebPageState();
}
class _WebPageState extends State<WebPage> {
// 是否显示加载动画
bool _flag = true;
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Container(
child: Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
children: <Widget>[
this._flag ? _getMoreWidget() : Text(""),
Expanded(
// 官方代码
child: InAppWebView(
initialUrlRequest: URLRequest(url: Uri.parse(widget.url)),
// 加载进度变化事件
onProgressChanged:
(InAppWebViewController controller, int progress) {
if ((progress / 100) > 0.999) {
setState(() {
this._flag = false;
});
}
},
),
)
],
)));
}
// 加载状态
Widget _getMoreWidget() {
return Center(
child: Padding(
padding: EdgeInsets.all(10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
'加载中...',
style: TextStyle(fontSize: 16.0),
),
CircularProgressIndicator(
strokeWidth: 1.0,
)
],
),
),
);
}
}
以上是关于Flutter 加载url flutter_inappwebview的主要内容,如果未能解决你的问题,请参考以下文章
即使使用 #/page 加载 URL,Flutter Web 也会首先加载初始页面