在Flutter中,在我的webview加载之前,我想显示一张本地图片。 如果用户没有打开他们的wifi,则会显示一张图片,而不是一个空白屏幕(iOS)或错误消息(Android),提示无法连接到该网页。
我正在使用此应用程序的官方webview_flutter包。
以下是我尝试过的代码,但它在iOS上运行正常,但在Android上却无法正常工作。 在Android上,当我关闭WiFi并启动应用程序时,将显示一个错误消息,其中显示Webview链接。
编辑:在WiFi连接后按下重新加载按钮后,不会从图像切换到Webview。
我正在使用此应用程序的官方webview_flutter包。
以下是我尝试过的代码,但它在iOS上运行正常,但在Android上却无法正常工作。 在Android上,当我关闭WiFi并启动应用程序时,将显示一个错误消息,其中显示Webview链接。
编辑:在WiFi连接后按下重新加载按钮后,不会从图像切换到Webview。
final webViewKey1 = GlobalKey<WebViewContainerState>();
var _url = 'http://www.google.com';
final _key = UniqueKey();
bool _isLoadingPage;
class WebViewPage extends StatefulWidget {
@override
WebViewPageState createState() => WebViewPageState();
}
class WebViewPageState extends State<WebViewPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Screen 1'),
leading: IconButton(
icon: Icon(Icons.menu),
onPressed: (){
Scaffold.of(context).openDrawer();
},
),
actions: <Widget>[
IconButton(
icon: Icon(Icons.refresh),
color: Colors.white,
onPressed: () {
webViewKey1.currentState?.reloadWebView();
},
),
]
),
body:
WebViewContainer(key: webViewKey1),
);
}
}
class WebViewContainer extends StatefulWidget {
WebViewContainer({Key key}) : super(key: key);
@override
WebViewContainerState createState() => WebViewContainerState();
}
class WebViewContainerState extends State<WebViewContainer> {
WebViewController _webViewController;
void initState() {
super.initState();
_isLoadingPage = true;
}
@override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
Opacity(opacity: _isLoadingPage?0:1, child: WebView(
key: _key,
initialUrl: _url,
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (controller) {
_webViewController = controller;
},
onPageFinished: (finish) {
setState(() {
_isLoadingPage = false;
});
},
),
),
_isLoadingPage
? Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/fail.png'),
fit: BoxFit.fill,
),
),
)
: Container(
color: Colors.transparent,
),
],
);
}
void reloadWebView() {
_webViewController?.reload();
}
}