second commit
This commit is contained in:
74
lib/Controller/Internetcheck/check_internet.dart
Normal file
74
lib/Controller/Internetcheck/check_internet.dart
Normal file
@@ -0,0 +1,74 @@
|
||||
import 'dart:async';
|
||||
import 'package:connectivity_plus/connectivity_plus.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import '../../Globalwidgets/textwidget.dart';
|
||||
import '../../Helper/Constants/Colorconstants.dart';
|
||||
|
||||
class ConnectivityController extends GetxController {
|
||||
final isOffline = false.obs;
|
||||
final Connectivity _connectivity = Connectivity();
|
||||
late StreamSubscription<List<ConnectivityResult>> _subscription;
|
||||
bool _wasOffline = false;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
_checkInitialConnection();
|
||||
_subscription = _connectivity.onConnectivityChanged.listen(_updateStatus);
|
||||
}
|
||||
|
||||
Future<void> _checkInitialConnection() async {
|
||||
final result = await _connectivity.checkConnectivity();
|
||||
_updateStatus([result.first]);
|
||||
}
|
||||
|
||||
void _updateStatus(List<ConnectivityResult> results) {
|
||||
final isCurrentlyOffline = !results.any((r) => r == ConnectivityResult.mobile || r == ConnectivityResult.wifi);
|
||||
|
||||
if (isCurrentlyOffline != isOffline.value) {
|
||||
isOffline.value = isCurrentlyOffline;
|
||||
|
||||
final context = Get.context!;
|
||||
|
||||
if (isCurrentlyOffline) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
|
||||
SnackBar(
|
||||
content: TextWidget(
|
||||
color: ColorConstants.secondaryColor,
|
||||
text: "You are currently offline",
|
||||
),
|
||||
backgroundColor: Theme.of(context).colorScheme.error,
|
||||
duration: Duration(days: 1), // Until dismissed or back online
|
||||
behavior: SnackBarBehavior.floating,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
if (_wasOffline) {
|
||||
ScaffoldMessenger.of(context).clearSnackBars();
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: TextWidget(
|
||||
text: "You are connected to the internet",
|
||||
color: ColorConstants.secondaryColor,
|
||||
),
|
||||
backgroundColor: Colors.green,
|
||||
behavior: SnackBarBehavior.floating,
|
||||
duration: const Duration(seconds: 3),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
_wasOffline = isCurrentlyOffline;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@override
|
||||
void onClose() {
|
||||
_subscription.cancel();
|
||||
super.onClose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user