Internet connection setup
This commit is contained in:
parent
58970a13d0
commit
a9d919fef7
@ -328,7 +328,7 @@ final GoRouter router = GoRouter(
|
||||
GoRoute(
|
||||
path: '/',
|
||||
//builder: (context, state) => LoginRoute(),
|
||||
builder: (context, state) => LoginRoute(),
|
||||
builder: (context, state) => MyHomePage(),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/internetcheck',
|
||||
|
||||
@ -1,32 +1,98 @@
|
||||
// // import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
// // import 'package:connectivity_plus/connectivity_plus.dart';
|
||||
// //
|
||||
// // class ConnectivityService {
|
||||
// // final Connectivity _connectivity = Connectivity();
|
||||
// //
|
||||
// // // This will hold the connectivity status
|
||||
// // Stream<List<ConnectivityResult>> get connectivityStream =>
|
||||
// // _connectivity.onConnectivityChanged;
|
||||
// // }
|
||||
// //
|
||||
// // // The provider that will provide the connectivity service
|
||||
// // final connectivityServiceProvider = Provider<ConnectivityService>((ref) {
|
||||
// // return ConnectivityService();
|
||||
// // });
|
||||
// //
|
||||
// // // StateNotifier to manage connectivity state
|
||||
// // class ConnectivityNotifier extends StateNotifier<bool> {
|
||||
// // final ConnectivityService _connectivityService;
|
||||
// //
|
||||
// // ConnectivityNotifier(this._connectivityService) : super(true) {
|
||||
// // _connectivityService.connectivityStream.listen((result) {
|
||||
// // state = result != ConnectivityResult.none;
|
||||
// // });
|
||||
// // }
|
||||
// // }
|
||||
// //
|
||||
// // // The provider for the connectivity state
|
||||
// // final connectivityProvider = StateNotifierProvider<ConnectivityNotifier, bool>((ref) {
|
||||
// // final connectivityService = ref.read(connectivityServiceProvider);
|
||||
// // return ConnectivityNotifier(connectivityService);
|
||||
// // });
|
||||
//
|
||||
//
|
||||
// import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
// import 'package:connectivity_plus/connectivity_plus.dart';
|
||||
//
|
||||
// final connectivityProvider = StateNotifierProvider<ConnectivityNotifier, bool>((ref) {
|
||||
// return ConnectivityNotifier();
|
||||
// });
|
||||
//
|
||||
// class ConnectivityNotifier extends StateNotifier<bool> {
|
||||
// ConnectivityNotifier() : super(true) {
|
||||
// _checkInitialConnection();
|
||||
// _listenToConnectivityChanges();
|
||||
// }
|
||||
//
|
||||
// void _checkInitialConnection() async {
|
||||
// final result = await Connectivity().checkConnectivity();
|
||||
// state = _hasInternet(result);
|
||||
// }
|
||||
//
|
||||
// void _listenToConnectivityChanges() {
|
||||
// Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
|
||||
// state = _hasInternet(result);
|
||||
// });
|
||||
// }
|
||||
//
|
||||
// bool _hasInternet(ConnectivityResult result) {
|
||||
// return result == ConnectivityResult.mobile || result == ConnectivityResult.wifi;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:connectivity_plus/connectivity_plus.dart';
|
||||
|
||||
class ConnectivityService {
|
||||
final Connectivity _connectivity = Connectivity();
|
||||
|
||||
// This will hold the connectivity status
|
||||
Stream<List<ConnectivityResult>> get connectivityStream =>
|
||||
_connectivity.onConnectivityChanged;
|
||||
}
|
||||
|
||||
// The provider that will provide the connectivity service
|
||||
final connectivityServiceProvider = Provider<ConnectivityService>((ref) {
|
||||
return ConnectivityService();
|
||||
final connectivityProvider = StateNotifierProvider<ConnectivityNotifier, bool>((ref) {
|
||||
return ConnectivityNotifier();
|
||||
});
|
||||
|
||||
// StateNotifier to manage connectivity state
|
||||
class ConnectivityNotifier extends StateNotifier<bool> {
|
||||
final ConnectivityService _connectivityService;
|
||||
ConnectivityNotifier() : super(true) {
|
||||
_checkInitialConnection();
|
||||
_listenToConnectivityChanges();
|
||||
}
|
||||
|
||||
ConnectivityNotifier(this._connectivityService) : super(true) {
|
||||
_connectivityService.connectivityStream.listen((result) {
|
||||
state = result != ConnectivityResult.none;
|
||||
void _checkInitialConnection() async {
|
||||
final result = await Connectivity().checkConnectivity();
|
||||
state = _hasInternet(result);
|
||||
}
|
||||
|
||||
void _listenToConnectivityChanges() {
|
||||
// For mobile platforms, use the connectivity plugin
|
||||
|
||||
// Listen for connectivity changes on web
|
||||
Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
|
||||
state = _hasInternet(result);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// The provider for the connectivity state
|
||||
final connectivityProvider = StateNotifierProvider<ConnectivityNotifier, bool>((ref) {
|
||||
final connectivityService = ref.read(connectivityServiceProvider);
|
||||
return ConnectivityNotifier(connectivityService);
|
||||
});
|
||||
|
||||
|
||||
bool _hasInternet(ConnectivityResult result) {
|
||||
return result == ConnectivityResult.mobile || result == ConnectivityResult.wifi;
|
||||
}
|
||||
}
|
||||
|
||||
170
lib/main.dart
170
lib/main.dart
@ -2,20 +2,95 @@ import 'package:external_repos/external_repos.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:uae_stat/config/injector.dart';
|
||||
import 'package:uae_stat/config/my_router.dart';
|
||||
import 'package:uae_stat/config/my_theme.dart';
|
||||
import 'package:uae_stat/config/old_theme.dart';
|
||||
import 'package:uae_stat/domain/entities/language_locale.dart';
|
||||
import 'package:uae_stat/domain/use_cases/auth_use_case.dart';
|
||||
import 'package:uae_stat/infrastructure/services/InternetConnectivity_Service/InternetConnectivityService.dart';
|
||||
import 'package:uae_stat/presentation/Screens/online_offline_verification/internet_check.dart';
|
||||
import 'package:uae_stat/presentation/Screens/profilepage.dart';
|
||||
import 'package:uae_stat/presentation/Screens/auth_verification/registration.dart';
|
||||
import 'package:uae_stat/presentation/components/indicators/locale_provider.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
|
||||
// void main() async {
|
||||
// WidgetsFlutterBinding.ensureInitialized();
|
||||
// // await (await SharedPreferences.getInstance()).clear();
|
||||
// configureDependencies();
|
||||
// runApp(
|
||||
// const ProviderScope(
|
||||
// child: MainApp(),
|
||||
// ),
|
||||
// );
|
||||
// }
|
||||
//
|
||||
// class MainApp extends ConsumerWidget {
|
||||
// const MainApp({super.key});
|
||||
//
|
||||
// @override
|
||||
// Widget build(BuildContext context, WidgetRef ref) {
|
||||
// final locale = ref.watch(localeProvider);
|
||||
//
|
||||
// return MaterialApp.router(
|
||||
// debugShowCheckedModeBanner: false,
|
||||
// routerConfig: router,
|
||||
// locale: locale,
|
||||
// localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||
// supportedLocales: AppLocalizations.supportedLocales,// Use the GoRouter instance from app_router.dart
|
||||
// );
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
|
||||
// void main() async {
|
||||
// WidgetsFlutterBinding.ensureInitialized();
|
||||
// // await (await SharedPreferences.getInstance()).clear();
|
||||
// configureDependencies();
|
||||
// runApp(
|
||||
// const ProviderScope(
|
||||
// child: MainApp(),
|
||||
// ),
|
||||
// );
|
||||
// }
|
||||
//
|
||||
// class MainApp extends ConsumerWidget {
|
||||
// const MainApp({super.key});
|
||||
//
|
||||
// @override
|
||||
// Widget build(BuildContext context, WidgetRef ref) {
|
||||
// final locale = ref.watch(localeProvider);
|
||||
//
|
||||
// // Listen to connectivity changes at the app level
|
||||
// ref.listen<bool>(connectivityProvider, (previous, hasInternet) {
|
||||
// if (previous != null && !hasInternet) {
|
||||
// // When no internet, navigate to the NoInternetScreen
|
||||
// print('No Internet Connection');
|
||||
// context.go('/internetcheck');
|
||||
//
|
||||
// } else if (previous != null && hasInternet) {
|
||||
// // If internet is back, navigate back to the HomeScreen
|
||||
// print("You have an Internet connection");
|
||||
// context.go('/myhomepage');
|
||||
//
|
||||
// }
|
||||
// });
|
||||
//
|
||||
// return MaterialApp.router(
|
||||
// debugShowCheckedModeBanner: false,
|
||||
// routerConfig: router, // Assuming you are using GoRouter
|
||||
// locale: locale,
|
||||
// localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||
// supportedLocales: AppLocalizations.supportedLocales,
|
||||
// );
|
||||
// }
|
||||
// }
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
// await (await SharedPreferences.getInstance()).clear();
|
||||
configureDependencies();
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
@ -31,86 +106,35 @@ class MainApp extends ConsumerWidget {
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final locale = ref.watch(localeProvider);
|
||||
|
||||
ref.listen<bool>(connectivityProvider, (previous, hasInternet) {
|
||||
print('Previous Internet Status: $previous');
|
||||
print('Current Internet Status: $hasInternet');
|
||||
if (previous != null && hasInternet != previous) {
|
||||
handleConnectivityChange(context, hasInternet);
|
||||
}
|
||||
});
|
||||
|
||||
return MaterialApp.router(
|
||||
debugShowCheckedModeBanner: false,
|
||||
routerConfig: router,
|
||||
locale: locale,
|
||||
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||
supportedLocales: AppLocalizations.supportedLocales,// Use the GoRouter instance from app_router.dart
|
||||
supportedLocales: AppLocalizations.supportedLocales,
|
||||
);
|
||||
}
|
||||
|
||||
void handleConnectivityChange(BuildContext context, bool hasInternet) {
|
||||
//final currentPath = GoRouter.of(context).location;
|
||||
final currentPath = GoRouter.of(context).routerDelegate.currentConfiguration.fullPath;
|
||||
if (!hasInternet && currentPath != '/internetcheck') {
|
||||
print("Status : Not connected");
|
||||
context.go('/internetcheck');
|
||||
} else if (hasInternet && currentPath != '/login') {
|
||||
print("Status : Internet connected");
|
||||
context.go('/login');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// class MainApp extends StatelessWidget {
|
||||
// const MainApp({super.key});
|
||||
//
|
||||
// @override
|
||||
// Widget build(BuildContext context) {
|
||||
// return MaterialApp(
|
||||
// home: RegisterScreen(),
|
||||
// //home: ProfileScreen(),
|
||||
// debugShowCheckedModeBanner: false,
|
||||
// );
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// class MainApp extends ConsumerWidget {
|
||||
// const MainApp({super.key});
|
||||
//
|
||||
// @override
|
||||
// Widget build(
|
||||
// BuildContext context,
|
||||
// WidgetRef ref,
|
||||
// ) {
|
||||
// const loading = Material(
|
||||
// child: Center(
|
||||
// child: CircularProgressIndicator(),
|
||||
// ),
|
||||
// );
|
||||
// // waits to load preferences (language)
|
||||
// // from local storage
|
||||
// final languageFinalizedWidget = ref
|
||||
// .watch(
|
||||
// routerProvider,
|
||||
// )
|
||||
// .when(
|
||||
// loading: () => loading,
|
||||
// error: Error.throwWithStackTrace,
|
||||
// data: (data) {
|
||||
// final materialApp = MaterialApp.router(
|
||||
// debugShowCheckedModeBanner: false,
|
||||
// routerConfig: data,
|
||||
// supportedLocales: LanguageLocale.values.map(
|
||||
// (e) => e.toLocale(),
|
||||
// ),
|
||||
// localizationsDelegates: GlobalMaterialLocalizations.delegates,
|
||||
// theme: ThemeData.from(
|
||||
// colorScheme: ColorScheme.fromSeed(
|
||||
// seedColor: MyTheme.topicColor(
|
||||
// IndicatorTopic.economy,
|
||||
// ),
|
||||
// ),
|
||||
// textTheme: OldTheme.textTheme,
|
||||
// ),
|
||||
// );
|
||||
// return ColoredBox(
|
||||
// color: Colors.white,
|
||||
// child: materialApp,
|
||||
// );
|
||||
// },
|
||||
// );
|
||||
// // waits to load auth from local storage
|
||||
// final authFinalizedWidget = ref
|
||||
// .watch(
|
||||
// authUseCaseProvider,
|
||||
// )
|
||||
// .when(
|
||||
// loading: () => loading,
|
||||
// error: Error.throwWithStackTrace,
|
||||
// data: (data) => languageFinalizedWidget,
|
||||
// );
|
||||
// return authFinalizedWidget;
|
||||
// }
|
||||
// }
|
||||
|
||||
@ -16,7 +16,7 @@ import video_player_avfoundation
|
||||
import webview_flutter_wkwebview
|
||||
|
||||
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
|
||||
ConnectivityPlusPlugin.register(with: registry.registrar(forPlugin: "ConnectivityPlusPlugin"))
|
||||
ConnectivityPlugin.register(with: registry.registrar(forPlugin: "ConnectivityPlugin"))
|
||||
FileSelectorPlugin.register(with: registry.registrar(forPlugin: "FileSelectorPlugin"))
|
||||
FlutterSecureStoragePlugin.register(with: registry.registrar(forPlugin: "FlutterSecureStoragePlugin"))
|
||||
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
|
||||
|
||||
@ -194,18 +194,18 @@ packages:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: connectivity_plus
|
||||
sha256: "8a68739d3ee113e51ad35583fdf9ab82c55d09d693d3c39da1aebab87c938412"
|
||||
sha256: "224a77051d52a11fbad53dd57827594d3bd24f945af28bd70bab376d68d437f0"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "6.1.2"
|
||||
version: "5.0.2"
|
||||
connectivity_plus_platform_interface:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: connectivity_plus_platform_interface
|
||||
sha256: "42657c1715d48b167930d5f34d00222ac100475f73d10162ddf43e714932f204"
|
||||
sha256: cf1d1c28f4416f8c654d7dc3cd638ec586076255d407cef3ddbdaf178272a71a
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.0.1"
|
||||
version: "1.2.4"
|
||||
convert:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
||||
@ -60,7 +60,7 @@ dependencies:
|
||||
material_charts: ^0.0.23
|
||||
flutter_staggered_grid_view: ^0.7.0
|
||||
syncfusion_flutter_charts: ^28.1.39
|
||||
connectivity_plus: ^6.1.2
|
||||
connectivity_plus: ^5.0.1
|
||||
|
||||
dependency_overrides:
|
||||
fading_edge_scrollview: ^4.1.1
|
||||
|
||||
Loading…
Reference in New Issue
Block a user