From 0d5df1a08b390199e388e96a39769bc8c23b7928 Mon Sep 17 00:00:00 2001 From: semi__h <152438086+himeshps@users.noreply.github.com> Date: Thu, 10 Oct 2024 17:15:21 +0530 Subject: [PATCH 1/2] Updated network_controller with a variable fix --- lib/network/network_controller.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/network/network_controller.dart b/lib/network/network_controller.dart index 167cb94..d29cb4d 100644 --- a/lib/network/network_controller.dart +++ b/lib/network/network_controller.dart @@ -36,4 +36,4 @@ class NetworkController extends GetxController { } } } -} \ No newline at end of file +} From 309a0b3e244b56f2fc029872f48ef80fcb421d33 Mon Sep 17 00:00:00 2001 From: semi__h <152438086+himeshps@users.noreply.github.com> Date: Thu, 10 Oct 2024 17:31:00 +0530 Subject: [PATCH 2/2] Update network_controller.dart --- lib/network/network_controller.dart | 45 ++++++++++++++++++----------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/lib/network/network_controller.dart b/lib/network/network_controller.dart index d29cb4d..c1b94dd 100644 --- a/lib/network/network_controller.dart +++ b/lib/network/network_controller.dart @@ -1,39 +1,50 @@ import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; -///this class uses [connectivity_plus] package to check the internet and responds with a snack bar -/// the [_updateConnectionStatus] gets the [ConnectivtyResult] form [onConnectivityChanged.listen] to check the connectivity +import 'dart:async'; + +/// This class uses [connectivity_plus] package to check the internet and responds with a snackbar. +/// The [_updateConnectionStatus] gets the [ConnectivityResult] from [onConnectivityChanged.listen] to check the connectivity. class NetworkController extends GetxController { final Connectivity _connectivity = Connectivity(); + final RxBool _isSnackbarOpen = false.obs; // To manually track the Snackbar state. + + late final StreamSubscription _connectivitySubscription; - @override + @override void onInit() { super.onInit(); - _connectivity.onConnectivityChanged.listen(_updateConnectionStatus ); + _connectivitySubscription = _connectivity.onConnectivityChanged.listen(_updateConnectionStatus); + } + + @override + void onClose() { + _connectivitySubscription.cancel(); + super.onClose(); } void _updateConnectionStatus(ConnectivityResult connectivityResult) { - - if (connectivityResult == ConnectivityResult.none) { + if (connectivityResult == ConnectivityResult.none) { + if (!_isSnackbarOpen.value) { + _isSnackbarOpen.value = true; // Mark Snackbar as open. Get.rawSnackbar( messageText: const Text( 'PLEASE CONNECT TO THE INTERNET', - style: TextStyle( - color: Colors.white, - fontSize: 14 - ) + style: TextStyle(color: Colors.white, fontSize: 14), ), isDismissible: false, - duration: const Duration(days: 1), + duration: const Duration(days: 1), // Keeps the Snackbar open until manually dismissed. backgroundColor: Colors.red[400]!, - icon : const Icon(Icons.wifi_off, color: Colors.white, size: 35,), + icon: const Icon(Icons.wifi_off, color: Colors.white, size: 35), margin: EdgeInsets.zero, - snackStyle: SnackStyle.GROUNDED + snackStyle: SnackStyle.GROUNDED, ); - } else { - if (Get.isSnackbarOpen) { - Get.closeCurrentSnackbar(); - } } + } else { + if (_isSnackbarOpen.value) { + Get.closeCurrentSnackbar(); // Close the Snackbar. + _isSnackbarOpen.value = false; // Mark Snackbar as closed. + } + } } }