I want to check if my internet connection is active or not continuously with warning for whole app in flutter

huangapple go评论70阅读模式
英文:

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 &#39;dart:async&#39;;
import &#39;package:connectivity_plus/connectivity_plus.dart&#39;;
import &#39;package:flutter/material.dart&#39;;
import &#39;package:bot_toast/bot_toast.dart&#39;;
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
State&lt;StatefulWidget&gt; createState() =&gt; _MyAppState();
}
class _MyAppState extends State&lt;MyApp&gt; {
late StreamSubscription&lt;ConnectivityResult&gt; 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: &quot;You are not connected to any network&quot;);
}
});
}
@override
dispose() {
subscription.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: &quot;App Test&quot;,
builder: BotToastInit(),
home: FirstPage(),
);
}
}
class FirstPage extends StatelessWidget{
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(&quot;First page&quot;),
),
body: Center(
child: TextButton(
onPressed: () =&gt; Navigator.of(context).push(MaterialPageRoute(builder: (context) =&gt; SecondPage())),
child: Text(
&quot;second page&quot;
)
),
),
);
}
}
class SecondPage extends StatelessWidget{
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(&quot;second page&quot;),
),
body: Center(
child: TextButton(
onPressed: () =&gt; Navigator.of(context).push(MaterialPageRoute(builder: (context) =&gt; SecondPage())),
child: Text(
&quot;Back&quot;
)
),
),
);
}
}

Link to the article on the issue

huangapple
  • 本文由 发表于 2023年7月10日 20:07:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76653573.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定