main.dart 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. textStyle: TextStyle(color: Color(0XFFAECAE5), fontSize: 14),
  74. messageStyle: TextStyle(color: Color(0XFFAECAE5), fontSize: 12),
  75. iconTheme: IconThemeData(color: Color(0XFFAECAE5)),
  76. backgroundColor: Colors.transparent,
  77. );
  78. EasyRefresh.defaultFooterBuilder = () => const ClassicFooter(
  79. dragText: 'Pull to load',
  80. armedText: 'Release ready',
  81. readyText: 'Loading...',
  82. processingText: 'Loading...',
  83. processedText: 'Succeeded',
  84. noMoreText: 'No more',
  85. failedText: 'Failed',
  86. showMessage: false,
  87. triggerOffset: 50,
  88. iconDimension: 22,
  89. textStyle: TextStyle(color: Color(0XFFAECAE5), fontSize: 14),
  90. messageStyle: TextStyle(color: Color(0XFFAECAE5), fontSize: 12),
  91. iconTheme: IconThemeData(color: Color(0XFFAECAE5)),
  92. backgroundColor: Colors.transparent,
  93. );
  94. /// SmartDialog 配置
  95. SmartDialog.config
  96. ..custom = SmartConfigCustom(
  97. maskColor: Colors.black.withOpacity(0.35),
  98. useAnimation: true,
  99. )
  100. ..attach = SmartConfigAttach(
  101. animationType: SmartAnimationType.scale,
  102. usePenetrate: false,
  103. )
  104. ..loading = SmartConfigLoading(
  105. backDismiss: true,
  106. clickMaskDismiss: true,
  107. )
  108. ..toast = SmartConfigToast(
  109. intervalTime: const Duration(milliseconds: 100),
  110. displayTime: const Duration(milliseconds: 2000),
  111. );
  112. }
  113. @override
  114. Widget build(BuildContext context, WidgetRef ref) {
  115. final themeMode = ref.watch(themeProvider);
  116. //根据主题配置对应的状态栏
  117. int? darkModel = SPUtil.getInt(AppConstant.storageDarkModel, defValue: 0);
  118. late SystemUiOverlayStyle systemUiOverlayStyle;
  119. if (DeviceUtils.isAndroid) {
  120. //根据SP存入的暗色模式,指定全局页面的状态栏,导航栏等设置
  121. switch (darkModel) {
  122. case 1:
  123. Log.d("main.dart - 指定亮色模式");
  124. systemUiOverlayStyle = ThemeConfig.systemUiOverlayStyleLightThemeBlack;
  125. break;
  126. case 2:
  127. Log.d("main.dart - 指定暗色模式");
  128. systemUiOverlayStyle = ThemeConfig.systemUiOverlayStyleDarkTheme;
  129. break;
  130. default:
  131. systemUiOverlayStyle = ThemeConfig.getSystemUiOverlayStyleByTheme(context);
  132. break;
  133. }
  134. }
  135. SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  136. useEffect(() {
  137. // 组件挂载时执行
  138. WidgetsBinding.instance.addPostFrameCallback((_) {
  139. //控件加载完成时执行
  140. AppConfigService.getInstance().initSize(context);
  141. });
  142. return () {
  143. // 组件卸载时执行
  144. };
  145. }, []);
  146. return KeyboardVisibilityBuilder(builder: (context, isKeyboardVisible) {
  147. return KeyboardDismissOnTap(
  148. dismissOnCapturedTaps: false,
  149. child: MaterialApp.router(
  150. title: 'PropertyManagementSystem',
  151. debugShowCheckedModeBanner: true,
  152. //主题配置
  153. theme: ThemeData(
  154. colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xFF4161D0)),
  155. useMaterial3: false,
  156. ).copyWith(extensions: [
  157. AppColorsTheme.light(),
  158. ]),
  159. darkTheme: ThemeData(
  160. colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xFF4161D0), brightness: Brightness.dark),
  161. useMaterial3: false,
  162. ).copyWith(extensions: [
  163. AppColorsTheme.dark(),
  164. ]),
  165. themeMode: themeMode == ThemeMode.system ? ThemeMode.system : themeMode,
  166. //国际化配置
  167. localizationsDelegates: const [
  168. S.delegate,
  169. GlobalMaterialLocalizations.delegate,
  170. GlobalCupertinoLocalizations.delegate,
  171. GlobalWidgetsLocalizations.delegate,
  172. ],
  173. //国际化英语为首选项
  174. supportedLocales: [const Locale('en', ''), ...S.delegate.supportedLocales],
  175. localeResolutionCallback: (locale, supportLocales) {
  176. // 中文 简繁体处理
  177. if (locale?.languageCode == 'zh') {
  178. if (locale?.scriptCode == 'Hant') {
  179. return const Locale('zh', 'HK'); //繁体
  180. } else {
  181. return const Locale('zh', 'CN'); //简体
  182. }
  183. }
  184. return null;
  185. },
  186. //AutoRouter的配置
  187. routerConfig: appRouter.config(
  188. navigatorObservers: () => [
  189. MyNavigatorObserver(),
  190. ],
  191. ),
  192. //SmartDialog初始化默认Loading与Toast
  193. builder: FlutterSmartDialog.init(
  194. toastBuilder: (String msg) {
  195. return CustomToastWidget(msg: msg);
  196. },
  197. loadingBuilder: (String msg) {
  198. return CustomLoadingWidget(msg: msg == 'loading...' ? 'Loading...' : msg);
  199. },
  200. notifyStyle: FlutterSmartNotifyStyle(
  201. successBuilder: (String msg) => CustomSuccessWidget(msg: msg),
  202. failureBuilder: (String msg) => CustomFailureWidget(msg: msg),
  203. errorBuilder: (String msg) => CustomErrorWidget(msg: msg),
  204. alertBuilder: (String msg) => CustomErrorWidget(msg: msg),
  205. warningBuilder: (String msg) => CustomErrorWidget(msg: msg),
  206. ),
  207. ),
  208. ),
  209. );
  210. });
  211. }
  212. }