main.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import 'dart:async';
  2. import 'package:flutter/foundation.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:fis_common/logger/logger.dart';
  5. import 'package:flutter_easyloading/flutter_easyloading.dart';
  6. import 'package:flutter_localizations/flutter_localizations.dart';
  7. import 'package:get/get.dart';
  8. import 'package:vitalapp/architecture/network_connectivity.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. double designWidth = 1280.0; // 设计尺寸宽度:1280
  75. if (kIsWeb) designWidth = 1460.0;
  76. final size = MediaQuery.of(context).size;
  77. final scale = size.width / designWidth; // 计算缩放比例
  78. return FittedBox(
  79. fit: BoxFit.fitWidth,
  80. child: SizedBox(
  81. width: designWidth,
  82. height: size.height / scale,
  83. child: Center(child: widget ?? const SizedBox()),
  84. ),
  85. );
  86. },
  87. ),
  88. );
  89. }
  90. @override
  91. void initState() {
  92. super.initState();
  93. WidgetsBinding.instance.addObserver(this);
  94. }
  95. @override
  96. void dispose() {
  97. WidgetsBinding.instance.removeObserver(this);
  98. super.dispose();
  99. }
  100. @override
  101. void didChangeAppLifecycleState(AppLifecycleState state) {
  102. super.didChangeAppLifecycleState(state);
  103. switch (state) {
  104. case AppLifecycleState.resumed:
  105. _netCheckTimer?.cancel();
  106. _netCheckTimer = null;
  107. // 延迟300ms,等待其他APP状态恢复
  108. _netCheckTimer = Timer(const Duration(milliseconds: 300), () async {
  109. // 唤起后主动检查网络连接,防止状态不同步
  110. await netChecker.check();
  111. });
  112. break;
  113. case AppLifecycleState.inactive:
  114. break;
  115. case AppLifecycleState.paused:
  116. break;
  117. case AppLifecycleState.detached:
  118. break;
  119. }
  120. }
  121. }