main.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 'app_router.dart';
  14. import 'generated/assets.dart';
  15. import 'generated/l10n.dart';
  16. import 'global.dart';
  17. ///全局导航key
  18. final GlobalKey<NavigatorState> navigatorKey = GlobalKey();
  19. void main() async {
  20. logd("====================app启动,flavor=$appFlavor====================");
  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. //更新token
  34. updateToken(await SpUtil.getToken());
  35. //更新选择的咨询人ID
  36. selectedPatientId = await SpUtil.getSelectedPatientId();
  37. if (!kIsWeb && defaultTargetPlatform == TargetPlatform.android) {
  38. await InAppWebViewController.setWebContentsDebuggingEnabled(kDebugMode);
  39. }
  40. }
  41. void _loadFonts() async {
  42. logd("加载字体");
  43. try {
  44. ByteData data = await rootBundle.load(Assets.fontsBmfont);
  45. photoFont = img.BitmapFont.fromZip(data.buffer.asUint8List());
  46. } catch (e) {
  47. loge("加载字体异常", error: e);
  48. }
  49. }
  50. class MyApp extends StatelessWidget {
  51. final AppRouter _appRouter = AppRouter(navigatorKey: navigatorKey);
  52. MyApp({super.key});
  53. @override
  54. Widget build(BuildContext context) {
  55. final botToastBuilder = BotToastInit();
  56. //使用这种方式处理旋转屏幕后ScreenUtil的数值问题,会导致这里多次build
  57. return OrientationBuilder(
  58. builder: (BuildContext context, Orientation orientation) {
  59. Size size = const Size(375, 812);
  60. if (MediaQuery.of(context).orientation == Orientation.landscape) {
  61. size = const Size(812, 375);
  62. }
  63. setFullScreen(orientation == Orientation.landscape);
  64. return ScreenUtilInit(
  65. designSize: size,
  66. child: MaterialApp.router(
  67. //debugShowCheckedModeBanner: false,
  68. builder: (ctx, child) {
  69. return botToastBuilder(ctx, child);
  70. },
  71. routerConfig: _appRouter.config(
  72. navigatorObservers: () => [BotToastNavigatorObserver()]),
  73. localizationsDelegates: const [
  74. S.delegate,
  75. GlobalMaterialLocalizations.delegate,
  76. GlobalWidgetsLocalizations.delegate,
  77. GlobalCupertinoLocalizations.delegate
  78. ],
  79. supportedLocales: S.delegate.supportedLocales,
  80. theme: ThemeData(
  81. colorScheme:
  82. ColorScheme.fromSeed(seedColor: Colors.deepPurple).copyWith(
  83. primary: const Color(0xFF0A50B4),
  84. surface: Colors.white,
  85. surfaceContainerHigh: const Color(0xFFEDEDED),
  86. onSurface: const Color(0xFF333333),
  87. onSurfaceVariant: const Color(0xFF505050),
  88. ),
  89. useMaterial3: true,
  90. ),
  91. ),
  92. );
  93. },
  94. );
  95. }
  96. }