main.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. fluwx.addSubscriber((response) {
  57. logd(
  58. "微信SDK回调,isSuccess=${response.isSuccessful},data=${response.toRecord()}");
  59. });
  60. }
  61. void _loadFonts() async {
  62. logd("加载字体");
  63. try {
  64. ByteData data = await rootBundle.load(Assets.fontsBmfont);
  65. photoFont = img.BitmapFont.fromZip(data.buffer.asUint8List());
  66. } catch (e) {
  67. loge("加载字体异常", error: e);
  68. }
  69. }
  70. class MyApp extends StatelessWidget {
  71. final AppRouter _appRouter = AppRouter(navigatorKey: navigatorKey);
  72. MyApp({super.key});
  73. @override
  74. Widget build(BuildContext context) {
  75. final botToastBuilder = BotToastInit();
  76. //使用这种方式处理旋转屏幕后ScreenUtil的数值问题,会导致这里多次build
  77. return OrientationBuilder(
  78. builder: (BuildContext context, Orientation orientation) {
  79. Size size = const Size(375, 812);
  80. if (MediaQuery.of(context).orientation == Orientation.landscape) {
  81. size = const Size(812, 375);
  82. }
  83. setFullScreen(orientation == Orientation.landscape);
  84. return ScreenUtilInit(
  85. designSize: size,
  86. child: MaterialApp.router(
  87. //debugShowCheckedModeBanner: false,
  88. onGenerateTitle: (ctx) => switch (appFlavor) {
  89. flavorHst => S.of(ctx).appNameHst,
  90. _ => S.of(ctx).appName
  91. },
  92. builder: (ctx, child) {
  93. return botToastBuilder(ctx, child);
  94. },
  95. routerConfig: _appRouter.config(
  96. navigatorObservers: () => [BotToastNavigatorObserver()]),
  97. localizationsDelegates: const [
  98. S.delegate,
  99. GlobalMaterialLocalizations.delegate,
  100. GlobalWidgetsLocalizations.delegate,
  101. GlobalCupertinoLocalizations.delegate
  102. ],
  103. supportedLocales: S.delegate.supportedLocales,
  104. theme: ThemeData(
  105. colorScheme:
  106. ColorScheme.fromSeed(seedColor: Colors.deepPurple).copyWith(
  107. primary: const Color(0xFF0A50B4),
  108. surface: Colors.white,
  109. surfaceContainerHigh: const Color(0xFFEDEDED),
  110. onSurface: const Color(0xFF333333),
  111. onSurfaceVariant: const Color(0xFF505050),
  112. ),
  113. useMaterial3: true,
  114. ),
  115. ),
  116. );
  117. },
  118. );
  119. }
  120. }