import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:dynamic_color/dynamic_color.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_displaymode/flutter_displaymode.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_timezone/flutter_timezone.dart'; import 'package:get/get.dart'; import 'package:internet_connection_checker/internet_connection_checker.dart'; import 'package:isar/isar.dart'; import 'package:path_provider/path_provider.dart'; import 'package:rain/app/modules/home.dart'; import 'package:rain/app/modules/onboarding.dart'; import 'package:rain/theme/theme.dart'; import 'app/data/weather.dart'; import 'translation/translation.dart'; import 'theme/theme_controller.dart'; import 'package:timezone/data/latest_all.dart' as tz; import 'package:timezone/timezone.dart' as tz; late Isar isar; late Settings settings; final ValueNotifier> isDeviceConnectedNotifier = ValueNotifier(InternetConnectionChecker().hasConnection); FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); bool amoledTheme = false; bool materialColor = false; void main() async { WidgetsFlutterBinding.ensureInitialized(); SystemChrome.setSystemUIOverlayStyle( const SystemUiOverlayStyle( systemNavigationBarColor: Colors.black, ), ); await isarInit(); await setOptimalDisplayMode(); Connectivity() .onConnectivityChanged .listen((ConnectivityResult result) async { if (result != ConnectivityResult.none) { isDeviceConnectedNotifier.value = InternetConnectionChecker().hasConnection; } else { isDeviceConnectedNotifier.value = Future(() => false); } }); final String timeZoneName = await FlutterTimezone.getLocalTimezone(); const AndroidInitializationSettings initializationSettingsAndroid = AndroidInitializationSettings('@mipmap/ic_launcher'); const InitializationSettings initializationSettings = InitializationSettings(android: initializationSettingsAndroid); await flutterLocalNotificationsPlugin.initialize(initializationSettings); tz.initializeTimeZones(); tz.setLocalLocation(tz.getLocation(timeZoneName)); runApp(const MyApp()); } Future setOptimalDisplayMode() async { final List supported = await FlutterDisplayMode.supported; final DisplayMode active = await FlutterDisplayMode.active; final List sameResolution = supported .where((DisplayMode m) => m.width == active.width && m.height == active.height) .toList() ..sort((DisplayMode a, DisplayMode b) => b.refreshRate.compareTo(a.refreshRate)); final DisplayMode mostOptimalMode = sameResolution.isNotEmpty ? sameResolution.first : active; await FlutterDisplayMode.setPreferredMode(mostOptimalMode); } Future isarInit() async { isar = await Isar.open([ SettingsSchema, MainWeatherCacheSchema, LocationCacheSchema, WeatherCardSchema, ], directory: (await getApplicationSupportDirectory()).path); settings = await isar.settings.where().findFirst() ?? Settings(); } class MyApp extends StatefulWidget { const MyApp({Key? key}) : super(key: key); static Future updateAppState( BuildContext context, { bool? newAmoledTheme, bool? newMaterialColor, }) async { final state = context.findAncestorStateOfType<_MyAppState>()!; if (newAmoledTheme != null) { state.changeAmoledTheme(newAmoledTheme); } if (newMaterialColor != null) { state.changeMarerialTheme(newMaterialColor); } } @override State createState() => _MyAppState(); } class _MyAppState extends State { final themeController = Get.put(ThemeController()); void changeAmoledTheme(bool newAmoledTheme) { setState(() { amoledTheme = newAmoledTheme; }); } void changeMarerialTheme(bool newMaterialColor) { setState(() { materialColor = newMaterialColor; }); } @override void initState() { amoledTheme = settings.amoledTheme; materialColor = settings.materialColor; super.initState(); } @override Widget build(BuildContext context) { return DynamicColorBuilder( builder: (lightColorScheme, darkColorScheme) { // if (lightColorScheme != null && darkColorScheme != null) { // colorScheme = themeController.theme == ThemeMode.light // ? lightColorScheme // : darkColorScheme; // } // final lightTheme = RainTheme.lightTheme.copyWith(); // final darkTheme = RainTheme.darkTheme.copyWith(); return GetMaterialApp( themeMode: themeController.theme, theme: RainTheme.lightTheme, darkTheme: amoledTheme ? RainTheme.oledTheme : RainTheme.darkTheme, localizationsDelegates: const [ GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, GlobalCupertinoLocalizations.delegate, ], translations: Translation(), locale: Get.deviceLocale, fallbackLocale: const Locale('en', 'US'), supportedLocales: const [ Locale('en', 'US'), Locale('ru', 'RU'), Locale('it', 'IT'), Locale('de', 'DE'), Locale('fr', 'FR'), Locale('tr', 'TR'), Locale('pt', 'BR'), Locale('es', 'ES'), Locale('sk', 'SK'), Locale('nl', 'NL'), Locale('hi', 'IN') ], localeResolutionCallback: (locale, supportedLocales) { for (var supportedLocale in supportedLocales) { if (supportedLocale.languageCode == locale?.languageCode) { return supportedLocale; } } return supportedLocales.first; }, debugShowCheckedModeBanner: false, home: settings.onboard == false ? const OnboardingPage() : const HomePage(), ); }, ); } }