HOW TO CHECK IF USER IS ONLINE OR OFFLINE IN FLUTTER

In this tutorial, we’ll learn how to implement Online & Offline connectivity in flutter apps. Their time comes when we need to check the Internet connection. If the user doesn’t have an internet connection, then we’ll show them Drop Offline connection, which will be an Animated Container widget. It will also work both Wifi connectivity and mobile data.
Flutter is an Open Source project by Google Which allows us to Create App For Both Android & IOS Platform. So For this connectivity, we will use a Flutter package name flutter_offline. This plugin perfectly works for both Platform iOS and Android app. By using this Package You can Also check Connection for Android & IOS Platform, which is the Tinny Package.

💻 Installation

First, you will need to add package name flutter_offline:
In the dependencies: section of your pubspec.yaml, add the following lines as :
Now run Flutter package get in your terminal which we’ll install flutter_offline package.

⚡️ Import

we’re Going to Wrap offlineBuilder with Builder widget because with the help of Builder Widget so we can show OfflineBuilder
With the help of the Bool Variable, we can access Connectivity state, with the help of Connectivity State, we can implement online & offline connectivity in the flutter app.

  1. //main.dart
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_offline/flutter_offline.dart';
  4. void main() => runApp(new MyApp());
  5. class MyApp extends StatelessWidget {
  6. @override
  7. Widget build(BuildContext context) {
  8. return MaterialApp(
  9. debugShowCheckedModeBanner: false,
  10. home: Scaffold(
  11. appBar: AppBar(
  12. title: Text("Connection"),
  13. ),
  14. body: Builder(
  15. builder: (BuildContext context) {
  16. return OfflineBuilder(
  17. connectivityBuilder: (BuildContext context,
  18. ConnectivityResult connectivity, Widget child) {
  19. final bool connected =
  20. connectivity != ConnectivityResult.none;
  21. return Stack(
  22. fit: StackFit.expand,
  23. children: [
  24. child,
  25. Positioned(
  26. left: 0.0,
  27. right: 0.0,
  28. height: 32.0,
  29. child: AnimatedContainer(
  30. duration: const Duration(milliseconds: 300),
  31. color:
  32. connected ? Color(0xFF00EE44) : Color(0xFFEE4400),
  33. child: connected
  34. ? Row(
  35. mainAxisAlignment: MainAxisAlignment.center,
  36. children: <Widget>[
  37. Text(
  38. "OFFLINE",
  39. style: TextStyle(color: Colors.white),
  40. ),
  41. ],
  42. )
  43. : Row(
  44. mainAxisAlignment: MainAxisAlignment.center,
  45. children: <Widget>[
  46. Text(
  47. "OFFLINE",
  48. style: TextStyle(color: Colors.white),
  49. ),
  50. SizedBox(
  51. width: 8.0,
  52. ),
  53. SizedBox(
  54. width: 12.0,
  55. height: 12.0,
  56. child: CircularProgressIndicator(
  57. strokeWidth: 2.0,
  58. valueColor:
  59. AlwaysStoppedAnimation<Color>(
  60. Colors.white),
  61. ),
  62. ),
  63. ],
  64. ),
  65. ),
  66. ),
  67. ],
  68. );
  69. },
  70. child: Center(
  71. child: Text("ONLINE Or OFFLINE"),
  72. ),
  73. );
  74. },
  75. )),
  76. );
  77. }
  78. }