main.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import 'package:bot_toast/bot_toast.dart';
  2. import 'package:eitc_erm_dental_flutter/db_util.dart';
  3. import 'package:eitc_erm_dental_flutter/funcs.dart';
  4. import 'package:eitc_erm_dental_flutter/sp_util.dart';
  5. import 'package:flutter/foundation.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:flutter/services.dart';
  8. import 'package:flutter_inappwebview/flutter_inappwebview.dart';
  9. import 'package:flutter_localizations/flutter_localizations.dart';
  10. import 'package:flutter_riverpod/flutter_riverpod.dart';
  11. import 'package:flutter_screenutil/flutter_screenutil.dart';
  12. import 'package:image/image.dart' as img;
  13. import 'package:package_info_plus/package_info_plus.dart';
  14. import 'app_router.dart';
  15. import 'generated/assets.dart';
  16. import 'generated/l10n.dart';
  17. import 'global.dart';
  18. ///全局导航key
  19. final GlobalKey<NavigatorState> navigatorKey = GlobalKey();
  20. void main() async {
  21. ///提前实例化WidgetsBinding
  22. WidgetsFlutterBinding.ensureInitialized();
  23. ///初始化Token
  24. await _initSettings();
  25. ///初始化数据库
  26. await DbUtil.instance.init();
  27. ///加载字体
  28. _loadFonts();
  29. runApp(ProviderScope(child: MyApp()));
  30. }
  31. ///初始化设置
  32. Future _initSettings() async {
  33. //初始化日志
  34. await initLog();
  35. PackageInfo packageInfo = await PackageInfo.fromPlatform();
  36. logd("""
  37. ====================app启动====================
  38. appName=${packageInfo.appName}
  39. flavor=$appFlavor
  40. packageName=${packageInfo.packageName}
  41. version=${packageInfo.version}
  42. buildNumber=${packageInfo.buildNumber}
  43. ==============================================
  44. """);
  45. //更新token
  46. updateToken(await SpUtil.getToken());
  47. //更新选择的咨询人ID
  48. selectedPatientId = await SpUtil.getSelectedPatientId();
  49. if (!kIsWeb && defaultTargetPlatform == TargetPlatform.android) {
  50. await InAppWebViewController.setWebContentsDebuggingEnabled(kDebugMode);
  51. }
  52. //注册微信SDK
  53. /*bool wxResult =
  54. await fluwx.registerApi(appId: wxAppId, universalLink: wxUniversalLink);
  55. logd("微信SDK注册结果=$wxResult");*/
  56. }
  57. void _loadFonts() async {
  58. logd("加载字体");
  59. try {
  60. ByteData data = await rootBundle.load(Assets.fontsBmfont);
  61. photoFont = img.BitmapFont.fromZip(data.buffer.asUint8List());
  62. } catch (e) {
  63. loge("加载字体异常", error: e);
  64. }
  65. }
  66. class MyApp extends StatelessWidget {
  67. final AppRouter _appRouter = AppRouter(navigatorKey: navigatorKey);
  68. MyApp({super.key});
  69. @override
  70. Widget build(BuildContext context) {
  71. final botToastBuilder = BotToastInit();
  72. //使用这种方式处理旋转屏幕后ScreenUtil的数值问题,会导致这里多次build
  73. return OrientationBuilder(
  74. builder: (BuildContext context, Orientation orientation) {
  75. Size size = const Size(375, 812);
  76. if (MediaQuery.of(context).orientation == Orientation.landscape) {
  77. size = const Size(812, 375);
  78. }
  79. setFullScreen(orientation == Orientation.landscape);
  80. return ScreenUtilInit(
  81. designSize: size,
  82. child: MaterialApp.router(
  83. //debugShowCheckedModeBanner: false,
  84. onGenerateTitle: (ctx) => switch (appFlavor) {
  85. flavorHst => S.of(ctx).appNameHst,
  86. _ => S.of(ctx).appName
  87. },
  88. builder: (ctx, child) {
  89. return botToastBuilder(ctx, child);
  90. },
  91. routerConfig: _appRouter.config(
  92. navigatorObservers: () => [BotToastNavigatorObserver()]),
  93. localizationsDelegates: const [
  94. S.delegate,
  95. GlobalMaterialLocalizations.delegate,
  96. GlobalWidgetsLocalizations.delegate,
  97. GlobalCupertinoLocalizations.delegate
  98. ],
  99. supportedLocales: S.delegate.supportedLocales,
  100. theme: ThemeData(
  101. colorScheme:
  102. ColorScheme.fromSeed(seedColor: Colors.deepPurple).copyWith(
  103. primary: const Color(0xFF0A50B4),
  104. surface: Colors.white,
  105. surfaceContainerHigh: const Color(0xFFEDEDED),
  106. onSurface: const Color(0xFF333333),
  107. onSurfaceVariant: const Color(0xFF505050),
  108. ),
  109. useMaterial3: true,
  110. ),
  111. ),
  112. );
  113. },
  114. );
  115. }
  116. }