main.dart 3.8 KB

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