main.dart 3.8 KB

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