Hello, I have the following code which monitors internet connectivity:
import 'dart:async'; // Import for Completer
import 'package:internet_connection_checker_plus/internet_connection_checker_plus.dart';
import 'package:flutter/services.dart';
import 'dart:ui';
class InternetConnectionMonitor {
static final InternetConnectionMonitor _instance = InternetConnectionMonitor._internal();
factory InternetConnectionMonitor() {
return _instance;
}
InternetConnectionMonitor._internal();
OverlayEntry? _noInternetOverlayEntry;
OverlayEntry? _restoredInternetOverlayEntry;
OverlayEntry? _modalBarrierEntry;
bool _noInternetDialogShowing = false;
bool _restoredInternetDialogShowing = false;
bool _hasInternet = true;
BuildContext? _originalContext;
String? _routeName;
StreamSubscription<InternetStatus>? _internetStatusSubscription;
void initialize(BuildContext context, String language, String routeName) {
_originalContext = context;
_routeName = routeName;
_internetStatusSubscription = InternetConnection().onStatusChange.listen((InternetStatus status) {
switch (status) {
case InternetStatus.connected:
if (!_hasInternet) {
_hasInternet = true;
showRestoredInternetDialog(language);
}
break;
case InternetStatus.disconnected:
if (_hasInternet) {
_hasInternet = false;
showNoInternetDialog(language);
}
break;
}
});
}
void cancel() {
_internetStatusSubscription?.cancel();
_internetStatusSubscription = null;
}
void showNoInternetDialog(String language) {
if (_noInternetDialogShowing || _originalContext == null || !_originalContext!.mounted) return;
closeCurrentDialog();
_noInternetDialogShowing = true;
addModalBarrier();
_noInternetOverlayEntry = OverlayEntry(
builder: (BuildContext context) {
return AlertDialog(
title: Row(
children: [
Icon(Icons.wifi_off, color: Colors.red),
SizedBox(width: 10),
Text(language == 'en' ? 'No Internet' : 'Χωρίς σύνδεση'),
],
),
content: Text(language == 'en' ? 'You have no internet connection.' : 'Δεν υπάρχει σύνδεση στο διαδίκτυο.'),
actions: [
TextButton(
child: Text('OK'),
onPressed: () {
_noInternetDialogShowing = false;
removeDialogs();
removeModalBarrier();
if (Platform.isAndroid) {
SystemNavigator.pop();
} else if (Platform.isIOS) {
exit(0);
}
},
),
],
);
},
);
if (_originalContext != null && Overlay.maybeOf(_originalContext!) != null) {
Overlay.of(_originalContext!).insert(_noInternetOverlayEntry!);
}
}
void showRestoredInternetDialog(String language) {
if (_restoredInternetDialogShowing || _originalContext == null || !_originalContext!.mounted) return;
closeCurrentDialog();
_restoredInternetDialogShowing = true;
addModalBarrier();
_restoredInternetOverlayEntry = OverlayEntry(
builder: (BuildContext context) {
return AlertDialog(
title: Row(
children: [
Icon(Icons.wifi, color: Colors.green),
SizedBox(width: 10),
Expanded(
child: Text(
language == 'en' ? 'Connection Restored' : 'Η σύνδεση αποκαταστάθηκε',
style: TextStyle(fontSize: 18),
softWrap: true,
),
),
],
),
content: Text(language == 'en' ? 'Your internet connection has been restored.' : 'Η σύνδεση σας στο διαδίκτυο αποκαταστάθηκε.'),
actions: [
TextButton(
child: Text('OK'),
onPressed: () {
_restoredInternetDialogShowing = false;
removeDialogs();
removeModalBarrier();
},
),
],
);
},
);
if (_originalContext != null && Overlay.maybeOf(_originalContext!) != null) {
Overlay.of(_originalContext!).insert(_restoredInternetOverlayEntry!);
}
}
void closeCurrentDialog() {
if (_noInternetOverlayEntry != null) {
_noInternetOverlayEntry?.remove();
_noInternetOverlayEntry = null;
_noInternetDialogShowing = false;
}
if (_restoredInternetOverlayEntry != null) {
_restoredInternetOverlayEntry?.remove();
_restoredInternetOverlayEntry = null;
_restoredInternetDialogShowing = false;
}
}
void removeDialogs() {
_noInternetOverlayEntry?.remove();
_noInternetOverlayEntry = null;
_restoredInternetOverlayEntry?.remove();
_restoredInternetOverlayEntry = null;
_noInternetDialogShowing = false;
_restoredInternetDialogShowing = false;
}
void addModalBarrier() {
if (_modalBarrierEntry != null) {
removeModalBarrier();
}
_modalBarrierEntry = OverlayEntry(
builder: (BuildContext context) {
return Stack(
children: [
BackdropFilter(
filter: ImageFilter.blur(sigmaX: 10, sigmaY: 10),
child: Container(color: Colors.black.withOpacity(0.5)),
),
ModalBarrier(dismissible: false, color: Colors.transparent),
],
);
},
);
if (_originalContext != null && Overlay.maybeOf(_originalContext!) != null) {
Overlay.of(_originalContext!).insert(_modalBarrierEntry!);
}
}
void removeModalBarrier() {
_modalBarrierEntry?.remove();
_modalBarrierEntry = null;
}
}
// This is the function to call from Flutterflow
Future<void> initializeInternetStatus(BuildContext context, String language, String routeName) async {
InternetConnectionMonitor _monitor = InternetConnectionMonitor();
_monitor.initialize(context, language, routeName);
}
// This is the function to call when leaving the page to cancel the listener
Future<void> cancelInternetStatusListener() async {
InternetConnectionMonitor _monitor = InternetConnectionMonitor();
_monitor.cancel();
}
when the monitoring starts, I will be calling initializeInternetStatus. When I leave the page and I want the monitor to end, I will be calling cancelInternetStatusListener. Is it possible to somehow call the two functions from Flutterflow when I need them? How can I achieve that?