Flutter WebView:如果没有互联网显示不同的内容
我有一个Flutter Web View应用程序。我想拥有一个带有文本的离线屏幕和“重试”按钮。
如果没有Internet连接(实时无需刷新并单击以检查连接),一旦他丢失了连接,请用文本显示屏幕,然后重试按钮。然后,如果连接成功地将用户带回他所在的页面。
我当前的主人员:
import 'package:webview_flutter/webview_flutter.dart';
void main() {
runApp(
const MaterialApp(
home: WebViewApp(),
),
);
}
class WebViewApp extends StatefulWidget {
const WebViewApp({Key? key}) : super(key: key);
@override
State<WebViewApp> createState() => _WebViewAppState();
}
class _WebViewAppState extends State<WebViewApp> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
elevation: 0,
toolbarHeight: 0,
),
body: const WebView(
javascriptMode: JavascriptMode.unrestricted,
initialUrl: 'https://google.com',
),
);
}
}
I have a flutter web view app. I would like to have an offline screen with text and the button "try again".
If there is no internet connection (in real-time, no need to refresh and click to check connection), once he lost connection, show the screen with text and try again button. Then if the connection is successfully get the user back to the page where he was.
My current main.dart:
import 'package:webview_flutter/webview_flutter.dart';
void main() {
runApp(
const MaterialApp(
home: WebViewApp(),
),
);
}
class WebViewApp extends StatefulWidget {
const WebViewApp({Key? key}) : super(key: key);
@override
State<WebViewApp> createState() => _WebViewAppState();
}
class _WebViewAppState extends State<WebViewApp> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
elevation: 0,
toolbarHeight: 0,
),
body: const WebView(
javascriptMode: JavascriptMode.unrestricted,
initialUrl: 'https://google.com',
),
);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 webview_flutter package,依靠 onwebresourceError 您可以做类似的事情:
然后有条件地显示您的“ Internet”内容:
Using webview_flutter package, relying on onWebResourceError you could do something like:
and then conditionally show your "no internet" content:
您可以使用Internet连接检查器之类的软件包。示例 this
一。并检查互联网状态
You can use a package like internet connection checker. Example this
One. And check for internet status like