main.dart 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import 'package:cpt_main/router/component/main_service_provider.dart';
  2. import 'package:cs_resources/generated/l10n.dart';
  3. import 'package:cs_resources/theme/app_colors_theme.dart';
  4. import 'package:cs_resources/theme/theme_notifier.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter/services.dart';
  7. import 'package:flutter_hooks/flutter_hooks.dart';
  8. import 'package:flutter_localizations/flutter_localizations.dart';
  9. import 'package:hooks_riverpod/hooks_riverpod.dart';
  10. import 'package:initializer/app_initializer.dart';
  11. import 'package:plugin_basic/basic_export.dart';
  12. import 'package:plugin_basic/constants/app_constant.dart';
  13. import 'package:plugin_basic/provider/app_config/app_config_service.dart';
  14. import 'package:plugin_basic/provider/global_provider_container.dart';
  15. import 'package:plugin_platform/engine/sp/sp_util.dart';
  16. import 'package:router/componentRouter/component_service_manager.dart';
  17. import 'package:cpt_profile/router/component/profile_service_provider.dart';
  18. import 'package:plugin_basic/obs/my_navigator_observer.dart';
  19. import 'package:shared/utils/device_utils.dart';
  20. import 'package:shared/utils/log_utils.dart';
  21. import 'package:widgets/dialog/custom_toast_widget.dart';
  22. import 'package:widgets/dialog/custom_error_widget.dart';
  23. import 'package:widgets/dialog/custom_failure_widget.dart';
  24. import 'package:widgets/dialog/custom_success_widget.dart';
  25. import 'package:widgets/dialog/custom_loading_widget.dart';
  26. import 'package:widgets/widget_export.dart';
  27. import 'package:cs_resources/theme/theme_config.dart';
  28. import 'router/page/app_page_router.dart';
  29. import 'router/component/app_service_provider.dart';
  30. import 'package:cpt_auth/router/component/auth_component_service.dart';
  31. import 'package:cpt_community/router/component/community_component_service.dart';
  32. import 'package:cpt_facility/router/component/facility_component_service.dart';
  33. import 'package:cpt_form/router/component/form_component_service.dart';
  34. import 'package:cpt_notice_board/router/component/notice_board_component_service.dart';
  35. import 'package:cpt_payment/router/component/payment_component_service.dart';
  36. import 'package:cpt_property/router/component/property_component_service.dart';
  37. import 'package:cpt_rewards/router/component/rewards_component_service.dart';
  38. import 'package:cpt_services/router/component/services_component_service.dart';
  39. void main() async {
  40. //交给初始化构造器去统一初始化
  41. await AppInitializer.initializeRunalone();
  42. //组件路由的注入
  43. final serviceManager = ComponentServiceManager()
  44. ..addServiceProvider(mainServiceProvider)
  45. ..addServiceProvider(authServiceProvider)
  46. ..addServiceProvider(appServiceProvider)
  47. ..addServiceProvider(communityServiceProvider)
  48. ..addServiceProvider(facilityServiceProvider)
  49. ..addServiceProvider(formServiceProvider)
  50. ..addServiceProvider(noticeBoardServiceProvider)
  51. ..addServiceProvider(paymentServiceProvider)
  52. ..addServiceProvider(propertyServiceProvider)
  53. ..addServiceProvider(rewardsServiceProvider)
  54. ..addServiceProvider(servicesServiceProvider)
  55. ..addServiceProvider(profileServiceProvider);
  56. runApp(ProviderScope(
  57. parent: globalContainer,
  58. child: MyApp(),
  59. ));
  60. }
  61. class MyApp extends HookConsumerWidget {
  62. MyApp({Key? key}) : super(key: key) {
  63. /// 全局设置 EasyRefresh 的样式
  64. EasyRefresh.defaultHeaderBuilder = () => const ClassicHeader(
  65. dragText: 'Pull to refresh',
  66. armedText: 'Release ready',
  67. readyText: 'Refreshing...',
  68. processingText: 'Refreshing...',
  69. processedText: 'Succeeded',
  70. noMoreText: 'No more',
  71. failedText: 'Failed',
  72. messageText: 'Last updated at %T',
  73. backgroundColor: Colors.transparent,
  74. );
  75. EasyRefresh.defaultFooterBuilder = () => const ClassicFooter(
  76. dragText: 'Pull to load',
  77. armedText: 'Release ready',
  78. readyText: 'Loading...',
  79. processingText: 'Loading...',
  80. processedText: 'Succeeded',
  81. noMoreText: 'No more',
  82. failedText: 'Failed',
  83. showMessage: false,
  84. triggerOffset: 50,
  85. iconDimension: 22,
  86. backgroundColor: Colors.transparent,
  87. );
  88. /// SmartDialog 配置
  89. SmartDialog.config
  90. ..custom = SmartConfigCustom(
  91. maskColor: Colors.black.withOpacity(0.35),
  92. useAnimation: true,
  93. )
  94. ..attach = SmartConfigAttach(
  95. animationType: SmartAnimationType.scale,
  96. usePenetrate: false,
  97. )
  98. ..loading = SmartConfigLoading(
  99. // backDismiss: true,
  100. clickMaskDismiss: true,
  101. )
  102. ..toast = SmartConfigToast(
  103. intervalTime: const Duration(milliseconds: 100),
  104. displayTime: const Duration(milliseconds: 2000),
  105. );
  106. }
  107. @override
  108. Widget build(BuildContext context, WidgetRef ref) {
  109. final themeMode = ref.watch(themeProvider);
  110. //根据主题配置对应的状态栏
  111. int? darkModel = SPUtil.getInt(AppConstant.storageDarkModel, defValue: 0);
  112. late SystemUiOverlayStyle systemUiOverlayStyle;
  113. if (DeviceUtils.isAndroid) {
  114. //根据SP存入的暗色模式,指定全局页面的状态栏,导航栏等设置
  115. switch (darkModel) {
  116. case 1:
  117. Log.d("main.dart - 指定亮色模式");
  118. systemUiOverlayStyle = ThemeConfig.systemUiOverlayStyleLightThemeBlack;
  119. break;
  120. case 2:
  121. Log.d("main.dart - 指定暗色模式");
  122. systemUiOverlayStyle = ThemeConfig.systemUiOverlayStyleDarkTheme;
  123. break;
  124. default:
  125. systemUiOverlayStyle = ThemeConfig.getSystemUiOverlayStyleByTheme(context);
  126. break;
  127. }
  128. }
  129. SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  130. useEffect(() {
  131. // 组件挂载时执行
  132. WidgetsBinding.instance.addPostFrameCallback((_) {
  133. //控件加载完成时执行
  134. AppConfigService.getInstance().initSize(context);
  135. });
  136. return () {
  137. // 组件卸载时执行
  138. };
  139. }, []);
  140. return KeyboardVisibilityBuilder(builder: (context, isKeyboardVisible) {
  141. return KeyboardDismissOnTap(
  142. dismissOnCapturedTaps: false,
  143. child: MaterialApp.router(
  144. title: 'PropertyManagementSystem',
  145. debugShowCheckedModeBanner: true,
  146. //主题配置
  147. theme: ThemeData(
  148. colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xFF4161D0)),
  149. useMaterial3: false,
  150. ).copyWith(extensions: [
  151. AppColorsTheme.light(),
  152. ]),
  153. darkTheme: ThemeData(
  154. colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xFF4161D0), brightness: Brightness.dark),
  155. useMaterial3: false,
  156. ).copyWith(extensions: [
  157. AppColorsTheme.dark(),
  158. ]),
  159. themeMode: themeMode == ThemeMode.system ? ThemeMode.system : themeMode,
  160. //国际化配置
  161. localizationsDelegates: const [
  162. S.delegate,
  163. GlobalMaterialLocalizations.delegate,
  164. GlobalCupertinoLocalizations.delegate,
  165. GlobalWidgetsLocalizations.delegate,
  166. ],
  167. //国际化英语为首选项
  168. supportedLocales: [const Locale('en', ''), ...S.delegate.supportedLocales],
  169. localeResolutionCallback: (locale, supportLocales) {
  170. // 中文 简繁体处理
  171. if (locale?.languageCode == 'zh') {
  172. if (locale?.scriptCode == 'Hant') {
  173. return const Locale('zh', 'HK'); //繁体
  174. } else {
  175. return const Locale('zh', 'CN'); //简体
  176. }
  177. }
  178. return null;
  179. },
  180. //AutoRouter的配置
  181. routerConfig: appRouter.config(
  182. navigatorObservers: () => [
  183. MyNavigatorObserver(),
  184. ],
  185. ),
  186. //SmartDialog初始化默认Loading与Toast
  187. builder: FlutterSmartDialog.init(
  188. toastBuilder: (String msg) {
  189. return CustomToastWidget(msg: msg);
  190. },
  191. loadingBuilder: (String msg) {
  192. return CustomLoadingWidget(msg: msg == 'loading...' ? 'Loading...' : msg);
  193. },
  194. notifyStyle: FlutterSmartNotifyStyle(
  195. successBuilder: (String msg) => CustomSuccessWidget(msg: msg),
  196. failureBuilder: (String msg) => CustomFailureWidget(msg: msg),
  197. errorBuilder: (String msg) => CustomErrorWidget(msg: msg),
  198. alertBuilder: (String msg) => CustomErrorWidget(msg: msg),
  199. warningBuilder: (String msg) => CustomErrorWidget(msg: msg),
  200. ),
  201. ),
  202. ),
  203. );
  204. });
  205. }
  206. }