main.dart 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import 'dart:async';
  2. import 'package:fis_lib_business_components/index.dart';
  3. import 'package:flutter/foundation.dart';
  4. import 'package:flutter/gestures.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:fis_common/logger/logger.dart';
  7. import 'package:flutter_easyloading/flutter_easyloading.dart';
  8. import 'package:flutter_localizations/flutter_localizations.dart';
  9. import 'package:get/get.dart';
  10. import 'package:vitalapp/architecture/app_parameters.dart';
  11. import 'package:vitalapp/architecture/network_connectivity.dart';
  12. import 'package:vitalapp/components/android_scroll_wrapper/android_global_scroll_fix.dart';
  13. import 'global.dart';
  14. import 'routes/routes.dart';
  15. void main() async {
  16. runZonedGuarded(
  17. () async {
  18. try {
  19. WidgetsFlutterBinding.ensureInitialized();
  20. await Global.init();
  21. } catch (e) {
  22. logger.e('Global init Error', e);
  23. }
  24. runApp(const _App());
  25. },
  26. (error, stack) {
  27. logger.e('runZonedGuarded Error', error);
  28. // GlobalErrorHandler.handle(error, stack);
  29. },
  30. );
  31. }
  32. class _App extends StatefulWidget {
  33. const _App();
  34. @override
  35. State<StatefulWidget> createState() => _AppState();
  36. }
  37. class _AppState extends State<_App> with WidgetsBindingObserver {
  38. Timer? _netCheckTimer;
  39. @override
  40. void initState() {
  41. super.initState();
  42. WidgetsBinding.instance.addObserver(this);
  43. }
  44. @override
  45. Widget build(BuildContext context) {
  46. final Set<PointerDeviceKind> dragDevices = {
  47. PointerDeviceKind.touch,
  48. PointerDeviceKind.mouse,
  49. PointerDeviceKind.trackpad,
  50. };
  51. return AndroidGlobalScrollFix(
  52. child: GetMaterialApp(
  53. title: "杏聆荟健康平台",
  54. scrollBehavior: AppParameters.data.isLocalStation
  55. ? MaterialScrollBehavior().copyWith(dragDevices: dragDevices)
  56. : null,
  57. // locale: const Locale('zh'),
  58. localizationsDelegates: const [
  59. GlobalMaterialLocalizations.delegate,
  60. GlobalWidgetsLocalizations.delegate,
  61. GlobalCupertinoLocalizations.delegate,
  62. ],
  63. supportedLocales: const [
  64. Locale('en', 'US'), // English
  65. Locale('zh', 'CN'), // Chinese Simplified
  66. // other locales your app supports
  67. ],
  68. theme: ThemeData(
  69. // primaryColor: const Color.fromRGBO(0, 178, 237, 1),
  70. // TODO:
  71. primaryColor: Colors.blue,
  72. colorScheme:
  73. ColorScheme.fromSeed(seedColor: Color(Colors.blue.value - 40)),
  74. // colorScheme: ColorScheme.fromSeed(
  75. // // seedColor: const Color.fromRGBO(44, 119, 229, 1),
  76. // // seedColor: Colors.lightBlue,
  77. // seedColor: const Color.fromRGBO(0, 178, 237, 1),
  78. // ),
  79. // colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
  80. useMaterial3: true,
  81. fontFamily: "NotoSansSC",
  82. fontFamilyFallback: const ["NotoSansSC"],
  83. ),
  84. debugShowCheckedModeBanner: false,
  85. getPages: Routes.routes,
  86. initialRoute: "splash",
  87. // initialRoute: "/patient/create",
  88. // initialRoute: "/contract/signature",
  89. // initialRoute: "/",
  90. builder: EasyLoading.init(
  91. builder: (context, widget) {
  92. double designWidth = 1280.0; // 设计尺寸宽度:1280
  93. if (kIsWeb) designWidth = 1460.0;
  94. final size = MediaQuery.of(context).size;
  95. final scale = size.width / designWidth; // 计算缩放比例
  96. return FittedBox(
  97. fit: BoxFit.fitWidth,
  98. child: SizedBox(
  99. width: designWidth,
  100. height: size.height / scale,
  101. child: Center(child: widget ?? const SizedBox()),
  102. ),
  103. );
  104. },
  105. ),
  106. ),
  107. );
  108. }
  109. @override
  110. void dispose() {
  111. WidgetsBinding.instance.removeObserver(this);
  112. super.dispose();
  113. }
  114. @override
  115. void didChangeAppLifecycleState(AppLifecycleState state) {
  116. super.didChangeAppLifecycleState(state);
  117. switch (state) {
  118. case AppLifecycleState.resumed:
  119. _netCheckTimer?.cancel();
  120. _netCheckTimer = null;
  121. // 延迟300ms,等待其他APP状态恢复
  122. _netCheckTimer = Timer(const Duration(milliseconds: 300), () async {
  123. // 唤起后主动检查网络连接,防止状态不同步
  124. await netChecker.check();
  125. });
  126. break;
  127. case AppLifecycleState.inactive:
  128. break;
  129. case AppLifecycleState.paused:
  130. break;
  131. case AppLifecycleState.detached:
  132. break;
  133. }
  134. }
  135. }