220 lines
6.9 KiB
Dart
220 lines
6.9 KiB
Dart
import 'dart:async';
|
|
import 'dart:developer';
|
|
|
|
import 'package:connectivity_plus/connectivity_plus.dart';
|
|
import 'package:device_info_plus/device_info_plus.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
import 'package:regroup/Utils/dialogs.dart';
|
|
import 'package:regroup/resources/routes/route_name.dart';
|
|
import 'package:regroup/resources/routes/routes.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
// void main() {
|
|
// runApp(const MyApp());
|
|
// }
|
|
|
|
Future<void> main() async {
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
// GlobalVariables globalVariables = GlobalVariables();
|
|
|
|
// token = prefs.getString('token');
|
|
// await Firebase.initializeApp(
|
|
// options: DefaultFirebaseOptions.currentPlatform,
|
|
// );
|
|
// FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterFatalError;
|
|
// PlatformDispatcher.instance.onError = (error, stack) {
|
|
// FirebaseCrashlytics.instance.recordError(error, stack, fatal: true);
|
|
// return true;
|
|
// };
|
|
// OnBoard = prefs.getBool("OnBoard");
|
|
SystemChrome.setPreferredOrientations([
|
|
DeviceOrientation.portraitUp,
|
|
]).then((value) => runApp(const MyApp()));
|
|
}
|
|
|
|
class MyApp extends StatefulWidget {
|
|
const MyApp({super.key});
|
|
|
|
@override
|
|
State<MyApp> createState() => _MyAppState();
|
|
}
|
|
|
|
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
|
|
// var _connectionStatus = ConnectivityResult.values.toString();
|
|
// late StreamSubscription<List<ConnectivityResult>> subscription;
|
|
// Connectivity connectivity = Connectivity();
|
|
|
|
List<ConnectivityResult> _connectionStatus = [ConnectivityResult.none];
|
|
final Connectivity _connectivity = Connectivity();
|
|
late StreamSubscription<List<ConnectivityResult>> _connectivitySubscription;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
WidgetsBinding.instance.addObserver(this);
|
|
initConnectivity();
|
|
|
|
_connectivitySubscription =
|
|
_connectivity.onConnectivityChanged.listen(_updateConnectionStatus);
|
|
|
|
// connectivity = Connectivity();
|
|
// checkInternet();
|
|
// if (Platform.isAndroid) {
|
|
// _getStoragePermission();
|
|
// }
|
|
// _getStoragePermission();
|
|
// subscription = connectivity.onConnectivityChanged
|
|
// .listen((List<ConnectivityResult> result) {
|
|
// _connectionStatus = result.toString();
|
|
// if (result == ConnectivityResult.wifi ||
|
|
// result == ConnectivityResult.mobile) {
|
|
// setState(() {
|
|
// _connectionStatus = result.toString();
|
|
|
|
// Get.back(result: true);
|
|
// });
|
|
// } else if (result == ConnectivityResult.none) {
|
|
// setState(() {
|
|
// _connectionStatus = result.toString();
|
|
// Get.toNamed(RouteName.nointernet);
|
|
// });
|
|
// }
|
|
// });
|
|
// print(_connectionStatus);
|
|
}
|
|
|
|
Future<void> initConnectivity() async {
|
|
late List<ConnectivityResult> result;
|
|
// Platform messages may fail, so we use a try/catch PlatformException.
|
|
try {
|
|
result = await _connectivity.checkConnectivity();
|
|
} on PlatformException catch (e) {
|
|
log('Couldn\'t check connectivity status', error: e);
|
|
return;
|
|
}
|
|
|
|
// If the widget was removed from the tree while the asynchronous platform
|
|
// message was in flight, we want to discard the reply rather than calling
|
|
// setState to update our non-existent appearance.
|
|
if (!mounted) {
|
|
return Future.value(null);
|
|
}
|
|
|
|
return _updateConnectionStatus(result);
|
|
}
|
|
|
|
Future<void> _updateConnectionStatus(List<ConnectivityResult> result) async {
|
|
// setState(() {
|
|
// _connectionStatus = result;
|
|
// });
|
|
|
|
if (result.contains(ConnectivityResult.wifi) || result.contains(ConnectivityResult.mobile)) {
|
|
setState(() {
|
|
_connectionStatus = result;
|
|
Get.back(result: true);
|
|
|
|
});
|
|
} else {
|
|
setState(() {
|
|
_connectionStatus = result;
|
|
Get.toNamed(RouteName.nointernet);
|
|
});
|
|
// Get.toNamed(RouteName.nointernet);
|
|
}
|
|
// ignore: avoid_print
|
|
print('Connectivity changed: $_connectionStatus');
|
|
}
|
|
|
|
Future<void> _getStoragePermission() async {
|
|
DeviceInfoPlugin plugin = DeviceInfoPlugin();
|
|
AndroidDeviceInfo android = await plugin.androidInfo;
|
|
if (android.version.sdkInt < 33) {
|
|
if (await Permission.storage.request().isGranted) {
|
|
// setState(() {
|
|
// permissionGranted = true;
|
|
// });
|
|
} else if (await Permission.storage.request().isPermanentlyDenied) {
|
|
await openAppSettings();
|
|
await utils.showToast("Permission denied.");
|
|
}
|
|
// else if (await Permission.audio.request().isDenied) {
|
|
// // setState(() {
|
|
// // permissionGranted = false;
|
|
// // });
|
|
// }
|
|
} else {
|
|
if (await Permission.photos.request().isGranted) {
|
|
// await utils.showToast("Permission granted.");
|
|
// setState(() {
|
|
// permissionGranted = true;
|
|
// });
|
|
} else if (await Permission.photos.request().isPermanentlyDenied) {
|
|
await openAppSettings();
|
|
await utils.showToast("Permission denied.");
|
|
} else if (await Permission.photos.request().isDenied) {
|
|
await openAppSettings();
|
|
await utils.showToast("Permission denied.");
|
|
// setState(() {
|
|
// permissionGranted = false;
|
|
// });
|
|
}
|
|
}
|
|
}
|
|
|
|
// Future<void> checkInternet() async {
|
|
// final connectivityResult = await (Connectivity().checkConnectivity());
|
|
|
|
// if (connectivityResult == ConnectivityResult.wifi ||
|
|
// connectivityResult == ConnectivityResult.mobile) {
|
|
// setState(() {
|
|
// _connectionStatus = connectivityResult.toString();
|
|
// });
|
|
// } else {
|
|
// setState(() {
|
|
// _connectionStatus = connectivityResult.toString();
|
|
// print(_connectionStatus.toString());
|
|
// Get.toNamed(RouteName.nointernet);
|
|
|
|
// // Navigator.pushReplacementNamed(context, "/noInternet");
|
|
// });
|
|
// }
|
|
// }
|
|
|
|
@override
|
|
void dispose() {
|
|
super.dispose();
|
|
WidgetsBinding.instance.removeObserver(this);
|
|
|
|
// subscription.cancel();
|
|
_connectivitySubscription.cancel();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return
|
|
ScreenUtilInit(
|
|
builder: (BuildContext context, Widget? child) => GetMaterialApp(
|
|
title: 'Regroup',
|
|
theme: ThemeData(
|
|
primarySwatch:
|
|
// createPrimarySwatch(Color(0xFF737373)),
|
|
Colors.grey,
|
|
fontFamily: 'Cambria',
|
|
),
|
|
debugShowCheckedModeBanner: false,
|
|
initialRoute: RouteName.splashScreen,
|
|
|
|
//initialRoute: RouteName.mainScreen,
|
|
getPages: AppRoutes.appRoutes(),
|
|
),
|
|
designSize: const Size(390, 848),
|
|
);
|
|
}
|
|
}
|
|
|