main.dart 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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/provider/app_config/app_config_service.dart';
  13. import 'package:plugin_basic/provider/global_provider_container.dart';
  14. import 'package:router/componentRouter/component_service_manager.dart';
  15. import 'package:cpt_profile/router/component/profile_service_provider.dart';
  16. import 'package:plugin_basic/obs/my_navigator_observer.dart';
  17. import 'package:shared/utils/log_utils.dart';
  18. import 'package:widgets/dialog/custom_toast_widget.dart';
  19. import 'package:widgets/dialog/custom_error_widget.dart';
  20. import 'package:widgets/dialog/custom_failure_widget.dart';
  21. import 'package:widgets/dialog/custom_success_widget.dart';
  22. import 'package:widgets/dialog/custom_loading_widget.dart';
  23. import 'package:widgets/widget_export.dart';
  24. import 'package:cs_resources/theme/theme_config.dart';
  25. import 'router/page/app_page_router.dart';
  26. import 'router/component/app_service_provider.dart';
  27. import 'package:cpt_auth/router/component/auth_component_service.dart';
  28. import 'package:cpt_community/router/component/community_component_service.dart';
  29. import 'package:cpt_facility/router/component/facility_component_service.dart';
  30. import 'package:cpt_form/router/component/form_component_service.dart';
  31. import 'package:cpt_notice_board/router/component/notice_board_component_service.dart';
  32. import 'package:cpt_payment/router/component/payment_component_service.dart';
  33. import 'package:cpt_property/router/component/property_component_service.dart';
  34. import 'package:cpt_rewards/router/component/rewards_component_service.dart';
  35. import 'package:cpt_services/router/component/services_component_service.dart';
  36. void main() async {
  37. //交给初始化构造器去统一初始化
  38. await AppInitializer.initializeRunalone();
  39. //组件路由的注入
  40. final serviceManager = ComponentServiceManager()
  41. ..addServiceProvider(mainServiceProvider)
  42. ..addServiceProvider(authServiceProvider)
  43. ..addServiceProvider(appServiceProvider)
  44. ..addServiceProvider(communityServiceProvider)
  45. ..addServiceProvider(facilityServiceProvider)
  46. ..addServiceProvider(formServiceProvider)
  47. ..addServiceProvider(noticeBoardServiceProvider)
  48. ..addServiceProvider(paymentServiceProvider)
  49. ..addServiceProvider(propertyServiceProvider)
  50. ..addServiceProvider(rewardsServiceProvider)
  51. ..addServiceProvider(servicesServiceProvider)
  52. ..addServiceProvider(profileServiceProvider);
  53. runApp(ProviderScope(
  54. parent: globalContainer,
  55. child: MyApp(),
  56. ));
  57. }
  58. class MyApp extends HookConsumerWidget {
  59. MyApp({Key? key}) : super(key: key) {
  60. /// 全局设置 EasyRefresh 的样式
  61. EasyRefresh.defaultHeaderBuilder = () => const ClassicHeader(
  62. dragText: 'Pull to refresh',
  63. armedText: 'Release ready',
  64. readyText: 'Refreshing...',
  65. processingText: 'Refreshing...',
  66. processedText: 'Succeeded',
  67. noMoreText: 'No more',
  68. failedText: 'Failed',
  69. messageText: 'Last updated at %T',
  70. textStyle: TextStyle(color: Color(0XFFAECAE5), fontSize: 14),
  71. messageStyle: TextStyle(color: Color(0XFFAECAE5), fontSize: 12),
  72. iconTheme: IconThemeData(color: Color(0XFFAECAE5)),
  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. textStyle: TextStyle(color: Color(0XFFAECAE5), fontSize: 14),
  87. messageStyle: TextStyle(color: Color(0XFFAECAE5), fontSize: 12),
  88. iconTheme: IconThemeData(color: Color(0XFFAECAE5)),
  89. backgroundColor: Colors.transparent,
  90. );
  91. /// SmartDialog 配置
  92. SmartDialog.config
  93. ..custom = SmartConfigCustom(
  94. maskColor: Colors.black.withOpacity(0.35),
  95. useAnimation: true,
  96. )
  97. ..attach = SmartConfigAttach(
  98. animationType: SmartAnimationType.scale,
  99. usePenetrate: false,
  100. )
  101. ..loading = SmartConfigLoading(
  102. backDismiss: true,
  103. clickMaskDismiss: true,
  104. )
  105. ..toast = SmartConfigToast(
  106. intervalTime: const Duration(milliseconds: 100),
  107. displayTime: const Duration(milliseconds: 2000),
  108. );
  109. }
  110. @override
  111. Widget build(BuildContext context, WidgetRef ref) {
  112. final themeMode = ref.watch(themeProvider);
  113. useEffect(() {
  114. // 组件挂载时执行
  115. WidgetsBinding.instance.addPostFrameCallback((_) {
  116. //控件加载完成时执行
  117. AppConfigService.getInstance().initSize(context);
  118. });
  119. return () {
  120. // 组件卸载时执行
  121. };
  122. }, []);
  123. //设置全局的状态栏文本样式
  124. SystemChrome.setSystemUIOverlayStyle(ThemeConfig.systemUiOverlayStyleLightThemeWhite);
  125. //路由管理,状态管理,依赖管理一切都始于GetMaterialApp
  126. return KeyboardVisibilityBuilder(builder: (context, isKeyboardVisible) {
  127. return KeyboardDismissOnTap(
  128. dismissOnCapturedTaps: false,
  129. child: MaterialApp.router(
  130. title: 'PropertyManagementSystem',
  131. debugShowCheckedModeBanner: true,
  132. //主题配置
  133. theme: ThemeData(
  134. colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
  135. useMaterial3: false,
  136. ).copyWith(extensions: [
  137. AppColorsTheme.light(),
  138. ]),
  139. darkTheme: ThemeData(
  140. colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple, brightness: Brightness.dark),
  141. useMaterial3: false,
  142. ).copyWith(extensions: [
  143. AppColorsTheme.dark(),
  144. ]),
  145. themeMode: themeMode == ThemeMode.system ? ThemeMode.system : themeMode,
  146. //国际化配置
  147. localizationsDelegates: const [
  148. S.delegate,
  149. GlobalMaterialLocalizations.delegate,
  150. GlobalCupertinoLocalizations.delegate,
  151. GlobalWidgetsLocalizations.delegate,
  152. ],
  153. //国际化英语为首选项
  154. supportedLocales: [const Locale('en', ''), ...S.delegate.supportedLocales],
  155. localeResolutionCallback: (locale, supportLocales) {
  156. // 中文 简繁体处理
  157. if (locale?.languageCode == 'zh') {
  158. if (locale?.scriptCode == 'Hant') {
  159. return const Locale('zh', 'HK'); //繁体
  160. } else {
  161. return const Locale('zh', 'CN'); //简体
  162. }
  163. }
  164. return null;
  165. },
  166. //AutoRouter的配置
  167. routerConfig: appRouter.config(
  168. navigatorObservers: () => [
  169. MyNavigatorObserver(),
  170. ],
  171. ),
  172. //SmartDialog初始化默认Loading与Toast
  173. builder: FlutterSmartDialog.init(
  174. toastBuilder: (String msg) {
  175. return CustomToastWidget(msg: msg);
  176. },
  177. loadingBuilder: (String msg) {
  178. return CustomLoadingWidget(msg: msg == 'loading...' ? 'Loading...' : msg);
  179. },
  180. notifyStyle: FlutterSmartNotifyStyle(
  181. successBuilder: (String msg) => CustomSuccessWidget(msg: msg),
  182. failureBuilder: (String msg) => CustomFailureWidget(msg: msg),
  183. errorBuilder: (String msg) => CustomErrorWidget(msg: msg),
  184. alertBuilder: (String msg) => CustomErrorWidget(msg: msg),
  185. warningBuilder: (String msg) => CustomErrorWidget(msg: msg),
  186. ),
  187. ),
  188. ),
  189. );
  190. });
  191. }
  192. }