main.dart 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. Brightness currentBrightness = MediaQuery.of(context).platformBrightness;
  132. if (currentBrightness == Brightness.dark) {
  133. Log.d("main.dart - 跟随系统模式-暗色模式");
  134. systemUiOverlayStyle = ThemeConfig.systemUiOverlayStyleDarkTheme;
  135. } else {
  136. Log.d("main.dart - 跟随系统模式-亮色模式");
  137. systemUiOverlayStyle = ThemeConfig.systemUiOverlayStyleLightThemeBlack;
  138. }
  139. break;
  140. }
  141. }
  142. SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  143. useEffect(() {
  144. // 组件挂载时执行
  145. WidgetsBinding.instance.addPostFrameCallback((_) {
  146. //控件加载完成时执行
  147. AppConfigService.getInstance().initSize(context);
  148. });
  149. return () {
  150. // 组件卸载时执行
  151. };
  152. }, []);
  153. //全局入口
  154. // return AnnotatedRegion<SystemUiOverlayStyle>(
  155. // value: systemUiOverlayStyle,
  156. // child:
  157. return KeyboardVisibilityBuilder(builder: (context, isKeyboardVisible) {
  158. return KeyboardDismissOnTap(
  159. dismissOnCapturedTaps: false,
  160. child: MaterialApp.router(
  161. title: 'PropertyManagementSystem',
  162. debugShowCheckedModeBanner: true,
  163. //主题配置
  164. theme: ThemeData(
  165. colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xFF4161D0)),
  166. useMaterial3: false,
  167. ).copyWith(extensions: [
  168. AppColorsTheme.light(),
  169. ]),
  170. darkTheme: ThemeData(
  171. colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xFF4161D0), brightness: Brightness.dark),
  172. useMaterial3: false,
  173. ).copyWith(extensions: [
  174. AppColorsTheme.dark(),
  175. ]),
  176. themeMode: themeMode == ThemeMode.system ? ThemeMode.system : themeMode,
  177. //国际化配置
  178. localizationsDelegates: const [
  179. S.delegate,
  180. GlobalMaterialLocalizations.delegate,
  181. GlobalCupertinoLocalizations.delegate,
  182. GlobalWidgetsLocalizations.delegate,
  183. ],
  184. //国际化英语为首选项
  185. supportedLocales: [const Locale('en', ''), ...S.delegate.supportedLocales],
  186. localeResolutionCallback: (locale, supportLocales) {
  187. // 中文 简繁体处理
  188. if (locale?.languageCode == 'zh') {
  189. if (locale?.scriptCode == 'Hant') {
  190. return const Locale('zh', 'HK'); //繁体
  191. } else {
  192. return const Locale('zh', 'CN'); //简体
  193. }
  194. }
  195. return null;
  196. },
  197. //AutoRouter的配置
  198. routerConfig: appRouter.config(
  199. navigatorObservers: () => [
  200. MyNavigatorObserver(),
  201. ],
  202. ),
  203. //SmartDialog初始化默认Loading与Toast
  204. builder: FlutterSmartDialog.init(
  205. toastBuilder: (String msg) {
  206. return CustomToastWidget(msg: msg);
  207. },
  208. loadingBuilder: (String msg) {
  209. return CustomLoadingWidget(msg: msg == 'loading...' ? 'Loading...' : msg);
  210. },
  211. notifyStyle: FlutterSmartNotifyStyle(
  212. successBuilder: (String msg) => CustomSuccessWidget(msg: msg),
  213. failureBuilder: (String msg) => CustomFailureWidget(msg: msg),
  214. errorBuilder: (String msg) => CustomErrorWidget(msg: msg),
  215. alertBuilder: (String msg) => CustomErrorWidget(msg: msg),
  216. warningBuilder: (String msg) => CustomErrorWidget(msg: msg),
  217. ),
  218. ),
  219. ),
  220. );
  221. });
  222. // );
  223. }
  224. }