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

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

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

以下是您要翻译的代码部分:

  1. 这是解决您问题的示例代码。
  2. import 'dart:async';
  3. import 'package:connectivity_plus/connectivity_plus.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:bot_toast/bot_toast.dart';
  6. void main() {
  7. runApp(MyApp());
  8. }
  9. class MyApp extends StatefulWidget {
  10. @override
  11. State<StatefulWidget> createState() => _MyAppState();
  12. }
  13. class _MyAppState extends State<MyApp> {
  14. late StreamSubscription<ConnectivityResult> subscription;
  15. @override
  16. initState() {
  17. super.initState();
  18. subscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
  19. //向用户显示网络状态
  20. if(result == ConnectivityResult.none){
  21. BotToast.showSimpleNotification(title: "您未连接到任何网络");
  22. }
  23. });
  24. }
  25. @override
  26. dispose() {
  27. subscription.cancel();
  28. super.dispose();
  29. }
  30. @override
  31. Widget build(BuildContext context) {
  32. return MaterialApp(
  33. title: "App Test",
  34. builder: BotToastInit(),
  35. home: FirstPage(),
  36. );
  37. }
  38. }
  39. class FirstPage extends StatelessWidget{
  40. @override
  41. Widget build(BuildContext context) {
  42. return Scaffold(
  43. appBar: AppBar(
  44. title: Text("第一页"),
  45. ),
  46. body: Center(
  47. child: TextButton(
  48. onPressed: () => Navigator.of(context).push(MaterialPageRoute(builder: (context) => SecondPage())),
  49. child: Text(
  50. "第二页"
  51. )
  52. ),
  53. ),
  54. );
  55. }
  56. }
  57. class SecondPage extends StatelessWidget{
  58. @override
  59. Widget build(BuildContext context) {
  60. return Scaffold(
  61. appBar: AppBar(
  62. title: Text("第二页"),
  63. ),
  64. body: Center(
  65. child: TextButton(
  66. onPressed: () => Navigator.of(context).push(MaterialPageRoute(builder: (context) => SecondPage())),
  67. child: Text(
  68. "返回"
  69. )
  70. ),
  71. ),
  72. );
  73. }
  74. }

问题文章链接

英文:

Here is an example code that solves your problem.

  1. import &#39;dart:async&#39;;
  2. import &#39;package:connectivity_plus/connectivity_plus.dart&#39;;
  3. import &#39;package:flutter/material.dart&#39;;
  4. import &#39;package:bot_toast/bot_toast.dart&#39;;
  5. void main() {
  6. runApp(MyApp());
  7. }
  8. class MyApp extends StatefulWidget {
  9. @override
  10. State&lt;StatefulWidget&gt; createState() =&gt; _MyAppState();
  11. }
  12. class _MyAppState extends State&lt;MyApp&gt; {
  13. late StreamSubscription&lt;ConnectivityResult&gt; subscription;
  14. @override
  15. initState() {
  16. super.initState();
  17. subscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
  18. //display the network status to the user
  19. if(result == ConnectivityResult.none){
  20. BotToast.showSimpleNotification(title: &quot;You are not connected to any network&quot;);
  21. }
  22. });
  23. }
  24. @override
  25. dispose() {
  26. subscription.cancel();
  27. super.dispose();
  28. }
  29. @override
  30. Widget build(BuildContext context) {
  31. return MaterialApp(
  32. title: &quot;App Test&quot;,
  33. builder: BotToastInit(),
  34. home: FirstPage(),
  35. );
  36. }
  37. }
  38. class FirstPage extends StatelessWidget{
  39. @override
  40. Widget build(BuildContext context) {
  41. return Scaffold(
  42. appBar: AppBar(
  43. title: Text(&quot;First page&quot;),
  44. ),
  45. body: Center(
  46. child: TextButton(
  47. onPressed: () =&gt; Navigator.of(context).push(MaterialPageRoute(builder: (context) =&gt; SecondPage())),
  48. child: Text(
  49. &quot;second page&quot;
  50. )
  51. ),
  52. ),
  53. );
  54. }
  55. }
  56. class SecondPage extends StatelessWidget{
  57. @override
  58. Widget build(BuildContext context) {
  59. return Scaffold(
  60. appBar: AppBar(
  61. title: Text(&quot;second page&quot;),
  62. ),
  63. body: Center(
  64. child: TextButton(
  65. onPressed: () =&gt; Navigator.of(context).push(MaterialPageRoute(builder: (context) =&gt; SecondPage())),
  66. child: Text(
  67. &quot;Back&quot;
  68. )
  69. ),
  70. ),
  71. );
  72. }
  73. }

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:

确定