selfprivacy.org.app/lib/main.dart

110 lines
3.8 KiB
Dart
Raw Normal View History

2022-02-10 09:50:37 +00:00
import 'package:easy_localization/easy_localization.dart';
2020-11-29 20:07:46 +00:00
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
2021-01-13 16:45:46 +00:00
import 'package:flutter_bloc/flutter_bloc.dart';
2022-05-03 10:45:10 +00:00
import 'package:selfprivacy/config/brand_colors.dart';
2021-01-06 17:35:57 +00:00
import 'package:selfprivacy/config/hive_config.dart';
2022-05-03 10:45:10 +00:00
import 'package:selfprivacy/theming/factory/app_theme_factory.dart';
import 'package:selfprivacy/ui/pages/setup/initializing.dart';
2020-11-29 20:07:46 +00:00
import 'package:selfprivacy/ui/pages/onboarding/onboarding.dart';
import 'package:selfprivacy/ui/pages/root_route.dart';
2021-01-19 12:05:40 +00:00
import 'package:wakelock/wakelock.dart';
2022-01-25 17:00:47 +00:00
import 'package:timezone/data/latest.dart' as tz;
2021-01-19 12:05:40 +00:00
2022-06-05 19:36:32 +00:00
import 'package:selfprivacy/config/bloc_config.dart';
import 'package:selfprivacy/config/bloc_observer.dart';
import 'package:selfprivacy/config/get_it_config.dart';
import 'package:selfprivacy/config/localization.dart';
import 'package:selfprivacy/logic/cubit/app_settings/app_settings_cubit.dart';
2020-11-29 20:07:46 +00:00
2021-01-06 17:35:57 +00:00
void main() async {
2021-08-29 09:50:24 +00:00
WidgetsFlutterBinding.ensureInitialized();
2021-01-06 17:35:57 +00:00
await HiveConfig.init();
2021-09-29 18:28:47 +00:00
await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
2022-04-29 10:45:15 +00:00
try {
2022-05-04 19:32:24 +00:00
/// Wakelock support for Linux
/// desktop is not yet implemented
2022-04-29 10:45:15 +00:00
await Wakelock.enable();
} on PlatformException catch (e) {
print(e);
}
2021-03-25 20:09:56 +00:00
await getItSetup();
2021-03-14 19:18:51 +00:00
await EasyLocalization.ensureInitialized();
2022-01-25 17:00:47 +00:00
tz.initializeTimeZones();
2020-12-01 12:26:29 +00:00
2022-06-05 19:36:32 +00:00
final ThemeData lightThemeData = await AppThemeFactory.create(
2022-05-03 10:45:10 +00:00
isDark: false,
fallbackColor: BrandColors.primary,
);
2022-06-05 19:36:32 +00:00
final ThemeData darkThemeData = await AppThemeFactory.create(
2022-05-03 10:45:10 +00:00
isDark: true,
fallbackColor: BrandColors.primary,
);
2022-01-25 17:00:47 +00:00
BlocOverrides.runZoned(
() => runApp(
Localization(
2022-05-16 22:41:00 +00:00
child: MyApp(
lightThemeData: lightThemeData,
darkThemeData: darkThemeData,
),
),
),
2022-01-25 17:00:47 +00:00
blocObserver: SimpleBlocObserver(),
);
2020-11-29 20:07:46 +00:00
}
class MyApp extends StatelessWidget {
2022-05-03 10:45:10 +00:00
const MyApp({
required this.lightThemeData,
required this.darkThemeData,
2022-06-05 19:36:32 +00:00
final super.key,
});
2022-05-03 10:45:10 +00:00
final ThemeData lightThemeData;
final ThemeData darkThemeData;
2020-11-29 20:07:46 +00:00
@override
2022-06-05 19:36:32 +00:00
Widget build(final BuildContext context) => Localization(
child: AnnotatedRegion<SystemUiOverlayStyle>(
value: SystemUiOverlayStyle.light, // Manually changing appbar color
child: BlocAndProviderConfig(
child: BlocBuilder<AppSettingsCubit, AppSettingsState>(
builder: (
final BuildContext context,
final AppSettingsState appSettings,
) =>
MaterialApp(
2022-03-23 14:07:52 +00:00
scaffoldMessengerKey:
getIt.get<NavigationService>().scaffoldMessengerKey,
navigatorKey: getIt.get<NavigationService>().navigatorKey,
localizationsDelegates: context.localizationDelegates,
supportedLocales: context.supportedLocales,
locale: context.locale,
debugShowCheckedModeBanner: false,
title: 'SelfPrivacy',
2022-05-03 10:45:10 +00:00
theme: lightThemeData,
darkTheme: darkThemeData,
2022-05-16 22:41:00 +00:00
themeMode:
appSettings.isDarkModeOn ? ThemeMode.dark : ThemeMode.light,
home: appSettings.isOnboardingShowing
? const OnboardingPage(nextPage: InitializingPage())
2022-05-24 18:55:39 +00:00
: const RootPage(),
2022-06-05 19:36:32 +00:00
builder: (final BuildContext context, final Widget? widget) {
2022-05-24 18:55:39 +00:00
Widget error = const Text('...rendering error...');
if (widget is Scaffold || widget is Navigator) {
2022-03-23 14:07:52 +00:00
error = Scaffold(body: Center(child: error));
}
2022-03-23 14:07:52 +00:00
ErrorWidget.builder =
2022-06-05 19:36:32 +00:00
(final FlutterErrorDetails errorDetails) => error;
2022-03-23 14:07:52 +00:00
return widget!;
},
2022-06-05 19:36:32 +00:00
),
),
2022-03-23 14:07:52 +00:00
),
),
);
2020-11-29 20:07:46 +00:00
}