Skip to content

Commit

Permalink
feat: add internet connectivity check and no internet screen
Browse files Browse the repository at this point in the history
  • Loading branch information
sharjeelyunus committed Feb 10, 2025
1 parent 948fd86 commit 819473a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
56 changes: 55 additions & 1 deletion starter/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,32 @@ import 'package:ensemble/framework/error_handling.dart';
import 'package:ensemble/framework/widget/error_screen.dart';
import 'package:ensemble_starter/generated/ensemble_modules.dart';
import 'package:flutter/material.dart';
import 'package:connectivity_plus/connectivity_plus.dart';

/// this demonstrates an App running exclusively with Ensemble
void main() async {
WidgetsFlutterBinding.ensureInitialized();
initErrorHandler();
await EnsembleModules().init();
runApp(EnsembleApp());

// Check if there is an active internet connection
bool hasInternet = await checkInternet();

if (hasInternet) {
runApp(EnsembleApp());
} else {
runApp(const NoInternetApp());
}
}

/// Checks if the device is connected to the internet.
Future<bool> checkInternet() async {
final connectivityResults = await Connectivity().checkConnectivity();

return connectivityResults != ConnectivityResult.none;
}

/// Initializes custom error handling.
void initErrorHandler() {
ErrorWidget.builder = (FlutterErrorDetails errorDetails) {
return ErrorScreen(errorDetails);
Expand All @@ -34,3 +51,40 @@ void initErrorHandler() {
return true;
};
}

// "No Internet"
class NoInternetApp extends StatelessWidget {
const NoInternetApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.wifi_off, size: 100, color: Colors.red),
const SizedBox(height: 20),
const Text(
'No Internet Connection',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () async {
// Retry checking for internet connectivity.
bool hasInternet = await checkInternet();
if (hasInternet) {
runApp(EnsembleApp());
}
},
child: const Text("Retry"),
),
],
),
),
),
);
}
}
6 changes: 4 additions & 2 deletions starter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ dependencies:
flutter_localizations: # Add this line
sdk: flutter

connectivity_plus: ^6.1.3

# embed Ensemble as a sibling project
ensemble:
git:
Expand Down Expand Up @@ -160,7 +162,7 @@ flutter:
- .env
- ensemble/

# put your app's images in this folder
# put your app's images in this folder
- ensemble/assets/

# list all your Apps directories here. It's a Flutter requirement
Expand All @@ -172,4 +174,4 @@ flutter:
- ensemble/apps/helloApp/translations/

# # config folder contains appConfig.json and secrets.json
- ensemble/apps/helloApp/config/
- ensemble/apps/helloApp/config/

0 comments on commit 819473a

Please sign in to comment.