app_widget.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import 'package:appflowy/plugins/document/presentation/more/cubit/document_appearance_cubit.dart';
  2. import 'package:appflowy_editor/appflowy_editor.dart' hide Log;
  3. import 'package:easy_localization/easy_localization.dart';
  4. import 'package:flowy_infra_ui/flowy_infra_ui.dart';
  5. import 'package:appflowy_backend/log.dart';
  6. import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:flutter_bloc/flutter_bloc.dart';
  9. import '../../user/application/user_settings_service.dart';
  10. import '../../workspace/application/appearance.dart';
  11. import '../startup.dart';
  12. class InitAppWidgetTask extends LaunchTask {
  13. const InitAppWidgetTask();
  14. @override
  15. LaunchTaskType get type => LaunchTaskType.appLauncher;
  16. @override
  17. Future<void> initialize(LaunchContext context) async {
  18. final widget = context.getIt<EntryPoint>().create(context.config);
  19. final appearanceSetting =
  20. await UserSettingsBackendService().getAppearanceSetting();
  21. // If the passed-in context is not the same as the context of the
  22. // application widget, the application widget will be rebuilt.
  23. final app = ApplicationWidget(
  24. key: ValueKey(context),
  25. appearanceSetting: appearanceSetting,
  26. child: widget,
  27. );
  28. Bloc.observer = ApplicationBlocObserver();
  29. runApp(
  30. EasyLocalization(
  31. supportedLocales: const [
  32. // In alphabetical order
  33. Locale('ar', 'SA'),
  34. Locale('ca', 'ES'),
  35. Locale('de', 'DE'),
  36. Locale('en'),
  37. Locale('es', 'VE'),
  38. Locale('eu', 'ES'),
  39. Locale('fr', 'FR'),
  40. Locale('fr', 'CA'),
  41. Locale('hu', 'HU'),
  42. Locale('id', 'ID'),
  43. Locale('it', 'IT'),
  44. Locale('ja', 'JP'),
  45. Locale('ko', 'KR'),
  46. Locale('pl', 'PL'),
  47. Locale('pt', 'BR'),
  48. Locale('ru', 'RU'),
  49. Locale('sv'),
  50. Locale('tr', 'TR'),
  51. Locale('zh', 'CN'),
  52. Locale('zh', 'TW'),
  53. ],
  54. path: 'assets/translations',
  55. fallbackLocale: const Locale('en'),
  56. useFallbackTranslations: true,
  57. saveLocale: false,
  58. child: app,
  59. ),
  60. );
  61. return Future(() => {});
  62. }
  63. }
  64. class ApplicationWidget extends StatelessWidget {
  65. final Widget child;
  66. final AppearanceSettingsPB appearanceSetting;
  67. const ApplicationWidget({
  68. Key? key,
  69. required this.child,
  70. required this.appearanceSetting,
  71. }) : super(key: key);
  72. @override
  73. Widget build(BuildContext context) {
  74. final cubit = AppearanceSettingsCubit(appearanceSetting)
  75. ..readLocaleWhenAppLaunch(context);
  76. return MultiBlocProvider(
  77. providers: [
  78. BlocProvider.value(value: cubit),
  79. BlocProvider<DocumentAppearanceCubit>(
  80. create: (_) => DocumentAppearanceCubit()..fetch(),
  81. ),
  82. ],
  83. child: BlocBuilder<AppearanceSettingsCubit, AppearanceSettingsState>(
  84. builder: (context, state) => MaterialApp(
  85. builder: overlayManagerBuilder(),
  86. debugShowCheckedModeBanner: false,
  87. theme: state.lightTheme,
  88. darkTheme: state.darkTheme,
  89. themeMode: state.themeMode,
  90. localizationsDelegates: context.localizationDelegates +
  91. [AppFlowyEditorLocalizations.delegate],
  92. supportedLocales: context.supportedLocales,
  93. locale: state.locale,
  94. navigatorKey: AppGlobals.rootNavKey,
  95. home: child,
  96. ),
  97. ),
  98. );
  99. }
  100. }
  101. class AppGlobals {
  102. static GlobalKey<NavigatorState> rootNavKey = GlobalKey();
  103. static NavigatorState get nav => rootNavKey.currentState!;
  104. }
  105. class ApplicationBlocObserver extends BlocObserver {
  106. @override
  107. // ignore: unnecessary_overrides
  108. void onTransition(Bloc bloc, Transition transition) {
  109. // Log.debug("[current]: ${transition.currentState} \n\n[next]: ${transition.nextState}");
  110. // Log.debug("${transition.nextState}");
  111. super.onTransition(bloc, transition);
  112. }
  113. @override
  114. void onError(BlocBase bloc, Object error, StackTrace stackTrace) {
  115. Log.debug(error);
  116. super.onError(bloc, error, stackTrace);
  117. }
  118. // @override
  119. // void onEvent(Bloc bloc, Object? event) {
  120. // Log.debug("$event");
  121. // super.onEvent(bloc, event);
  122. // }
  123. }