flutter开发实战-webview插件flutter_inappwebview使用
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
class WebViewExampleScreen extends StatefulWidget {
@override
_WebViewExampleScreenState createState() => new _WebViewExampleScreenState();
}
class _WebViewExampleScreenState extends State<WebViewExampleScreen> {
InAppWebViewController webView;
String url = "https://www.example.com";
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("WebView"),
),
body: Container(
child: Column(
children: <Widget>[
Expanded(
child: InAppWebView(
initialUrl: url,
initialHeaders: {},
initialOptions: InAppWebViewWidgetOptions(
inAppWebViewOptions: InAppWebViewOptions(
debuggingEnabled: true,
),
),
onWebViewCreated: (InAppWebViewController controller) {
webView = controller;
},
onLoadStart: (InAppWebViewController controller, String url) {
},
onLoadStop: (InAppWebViewController controller, String url) {
},
),
),
],
),
),
);
}
}
这段代码展示了如何在Flutter应用中使用flutter_inappwebview
插件来加载和显示一个网页。它创建了一个InAppWebView
控件,并通过initialUrl
属性设置了要加载的网址。同时,它还提供了一些回调函数,如onWebViewCreated
、onLoadStart
和onLoadStop
,这些函数可以帮助你监听和响应不同的网页加载事件。
评论已关闭