Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A probable fix to the snackbar issue #86

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 29 additions & 18 deletions lib/network/network_controller.dart
Original file line number Diff line number Diff line change
@@ -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<ConnectivityResult> _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.
}
}
}
}
himeshps marked this conversation as resolved.
Show resolved Hide resolved
}