I am using the following code to open and close dialogs depending on whether there is internet connectivity or not:
// Automatic FlutterFlow imports
import '/backend/backend.dart';
import '/backend/schema/structs/index.dart';
import '/backend/schema/enums/enums.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
import 'dart:io'; // Import Platform for checking the operating system
import 'package:internet_connection_checker_plus/internet_connection_checker_plus.dart';
import 'package:flutter/services.dart'; // Needed for closing the app
import 'dart:async';
import 'dart:ui';
class InternetConnectionMonitor {
Timer? _timer;
OverlayEntry? _noInternetOverlayEntry;
OverlayEntry? _restoredInternetOverlayEntry;
OverlayEntry? _modalBarrierEntry;
bool _hasInternet = true;
bool _modalBarrierShowing = false;
BuildContext? _originalContext;
String? _routeName;
Completer<String>? _completer;
Future<void> startMonitoring(BuildContext context, String language,
String routeName, Completer<String> completer) async {
debugPrint('Start monitoring internet connection');
_originalContext = context;
_routeName = routeName;
_completer = completer;
if (_timer == null) {
_timer = Timer.periodic(Duration(seconds: 5), (timer) async {
await checkInternetStatus(context, language);
});
await checkInternetStatus(context, language); // Initial check
}
}
void stopMonitoring() {
debugPrint('Stop monitoring internet connection');
if (_timer != null) {
_timer?.cancel();
_timer = null;
}
removeDialogs();
removeModalBarrier();
}
Future<void> checkInternetStatus(
BuildContext context, String language) async {
debugPrint('Checking internet status');
final InternetConnection internetConnection = InternetConnection();
final status = await internetConnection.hasInternetAccess;
debugPrint('Internet status: ${status ? "connected" : "disconnected"}');
if (!status && _hasInternet) {
_hasInternet = false;
showNoInternetDialog(context, language);
} else if (status && !_hasInternet) {
_hasInternet = true;
showRestoredInternetDialog(context, language);
}
// Complete the completer with 'false' if there is internet connection
if (status && _completer != null && !_completer!.isCompleted) {
_completer!.complete('false');
}
}
void showNoInternetDialog(BuildContext context, String language) {
debugPrint('Showing no internet dialog');
removeDialogs(); // Ensure all dialogs are removed before showing a new one
String title = language == 'el' ? 'Χωρίς σύνδεση' : 'No Internet';
String message = language == 'el'
? 'Δεν υπάρχει σύνδεση στο διαδίκτυο.'
: 'You have no internet connection.';
addModalBarrier(context);
_noInternetOverlayEntry = OverlayEntry(
builder: (BuildContext context) {
return AlertDialog(
title: Row(
children: [
Icon(Icons.wifi_off, color: Colors.red),
SizedBox(width: 10),
Text(title),
],
),
content: Text(message),
actions: [
TextButton(
child: Text('OK'),
onPressed: () {
debugPrint('No internet dialog OK pressed');
removeDialogs(); // Ensure this dialog is removed properly
removeModalBarrier();
if (Platform.isAndroid) {
SystemNavigator.pop();
} else if (Platform.isIOS) {
exit(0);
}
},
),
],
);
},
);
if (_noInternetOverlayEntry != null && Overlay.maybeOf(context) != null) {
Overlay.of(context).insert(_noInternetOverlayEntry!);
}
}
void showRestoredInternetDialog(BuildContext context, String language) {
debugPrint('Showing restored internet dialog');
removeDialogs(); // Ensure all dialogs are removed before showing a new one
String title = 'Connection Restored';
String message = 'Your internet connection has been restored.';
addModalBarrier(context);
_restoredInternetOverlayEntry = OverlayEntry(
builder: (BuildContext context) {
return AlertDialog(
title: Row(
children: [
Icon(Icons.wifi, color: Colors.green),
SizedBox(width: 10),
Text(title),
],
),
content: Text(message),
actions: [
TextButton(
child: Text('OK'),
onPressed: () {
debugPrint('Restored internet dialog OK pressed');
removeDialogs(); // Ensure this dialog is removed properly
removeModalBarrier();
// Complete the completer with 'true' when the button is pressed
if (_completer != null && !_completer!.isCompleted) {
_completer!.complete('true');
}
// Navigate back to the original route
if (_originalContext != null && _routeName != null) {
Navigator.of(_originalContext!).pushNamed(_routeName!);
}
},
),
],
);
},
);
if (_restoredInternetOverlayEntry != null &&
Overlay.maybeOf(context) != null) {
Overlay.of(context).insert(_restoredInternetOverlayEntry!);
}
}
void removeDialogs() {
debugPrint('Removing all dialogs');
if (_noInternetOverlayEntry != null) {
_noInternetOverlayEntry!.remove();
_noInternetOverlayEntry = null;
}
if (_restoredInternetOverlayEntry != null) {
_restoredInternetOverlayEntry!.remove();
_restoredInternetOverlayEntry = null;
}
}
void addModalBarrier(BuildContext context) {
if (_modalBarrierShowing) {
removeModalBarrier();
}
debugPrint('Adding modal barrier');
_modalBarrierShowing = true;
_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 (_modalBarrierEntry != null && Overlay.maybeOf(context) != null) {
Overlay.of(context)!.insert(_modalBarrierEntry!);
}
}
void removeModalBarrier() {
if (_modalBarrierEntry != null) {
debugPrint('Removing modal barrier');
if (_modalBarrierEntry!.mounted) {
_modalBarrierEntry!.remove();
}
_modalBarrierEntry = null;
_modalBarrierShowing = false;
}
}
Future<void> monitorInternetConnection(BuildContext context, String language,
String routeName, Completer<String> completer) async {
await startMonitoring(context, language, routeName, completer);
}
}
Future<String> monitorInternetConnection(
BuildContext context, String language, String routeName) async {
Completer<String> completer = Completer<String>();
InternetConnectionMonitor _monitor = InternetConnectionMonitor();
await _monitor.startMonitoring(context, language, routeName, completer);
return completer.future;
}
the problem I am having is that when the alert dialog ‘You have internet’ appears, I need to click on OK twice in order to close, which seems to be the case that 2 such dialogs open. Also, when the ‘You have no internet’ dialog appears, initially there is a blurred background which becomes even more blurred a little bit later, does this mean also 2 such dialogs are opened? How can I make sure 1 of each dialogs are opened, and that the ‘OK’ pressed once closes the dialog when there is internet?