英文:
I want to check if my internet connection is active or not continuously with warning for whole app in flutter
问题
我想在Flutter应用程序中检查我的互联网连接。我想在整个应用程序上显示警告,无论在哪个页面。是否可以在互联网不活动时显示底部或顶部的警告,当互联网再次活动时,警告应变为绿色,并显示文本“互联网已激活”,然后在几秒钟后消失。我不想在每个页面上都使用connectivity_plus
并在每次有人访问该页面时都检查连接。如果可以有一个中央解决方案,可以在主要部分内实现,这将会很有帮助。
英文:
I want to check my internet connection in flutter app. I want to show a warning throughout the app, on any page. Is it possible to show a bottom or top warning when internet is not active, and when it becomes active again, warning should turn green with text "Internet is Active" and disappear after a few seconds. I don't want to use connectivity_plus
on each and every page and check connection every time someone lands on that page. If there can be a central solution, which can be implemented inside main, it would be helpful.
答案1
得分: 1
以下是您要翻译的代码部分:
这是解决您问题的示例代码。
import 'dart:async';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/material.dart';
import 'package:bot_toast/bot_toast.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late StreamSubscription<ConnectivityResult> subscription;
@override
initState() {
super.initState();
subscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
//向用户显示网络状态
if(result == ConnectivityResult.none){
BotToast.showSimpleNotification(title: "您未连接到任何网络");
}
});
}
@override
dispose() {
subscription.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "App Test",
builder: BotToastInit(),
home: FirstPage(),
);
}
}
class FirstPage extends StatelessWidget{
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("第一页"),
),
body: Center(
child: TextButton(
onPressed: () => Navigator.of(context).push(MaterialPageRoute(builder: (context) => SecondPage())),
child: Text(
"第二页"
)
),
),
);
}
}
class SecondPage extends StatelessWidget{
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("第二页"),
),
body: Center(
child: TextButton(
onPressed: () => Navigator.of(context).push(MaterialPageRoute(builder: (context) => SecondPage())),
child: Text(
"返回"
)
),
),
);
}
}
英文:
Here is an example code that solves your problem.
import 'dart:async';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/material.dart';
import 'package:bot_toast/bot_toast.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late StreamSubscription<ConnectivityResult> subscription;
@override
initState() {
super.initState();
subscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
//display the network status to the user
if(result == ConnectivityResult.none){
BotToast.showSimpleNotification(title: "You are not connected to any network");
}
});
}
@override
dispose() {
subscription.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "App Test",
builder: BotToastInit(),
home: FirstPage(),
);
}
}
class FirstPage extends StatelessWidget{
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("First page"),
),
body: Center(
child: TextButton(
onPressed: () => Navigator.of(context).push(MaterialPageRoute(builder: (context) => SecondPage())),
child: Text(
"second page"
)
),
),
);
}
}
class SecondPage extends StatelessWidget{
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("second page"),
),
body: Center(
child: TextButton(
onPressed: () => Navigator.of(context).push(MaterialPageRoute(builder: (context) => SecondPage())),
child: Text(
"Back"
)
),
),
);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论