main.dart 3.9 KB

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