main.dart 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import 'dart:ui';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_easyloading/flutter_easyloading.dart';
  4. import 'package:flutter_localizations/flutter_localizations.dart';
  5. import 'package:get/get.dart';
  6. import 'package:o2oa_all_platform/global.dart';
  7. import 'package:o2oa_all_platform/common/index.dart';
  8. import 'package:pull_to_refresh/pull_to_refresh.dart';
  9. Future<void> main() async {
  10. await Global.init();
  11. runApp(const MyApp());
  12. }
  13. class MyApp extends StatefulWidget {
  14. const MyApp({Key? key}) : super(key: key);
  15. @override
  16. State<StatefulWidget> createState() => MyAppState();
  17. }
  18. class MyAppState extends State<MyApp> {
  19. // 主题模式 light dark
  20. ThemeMode mode = ThemeMode.system;
  21. // 主题
  22. ThemeData lightTheme = AppTheme.defaultLightTheme;
  23. ThemeData darkTheme = AppTheme.defaultDarkTheme;
  24. Locale? _locale = Languages.locale;
  25. bool needGrey = false;
  26. final _eventMsgId = 'MyApp';
  27. @override
  28. void initState() {
  29. // 是否需要全局黑白
  30. needGrey = SharedPreferenceService.to.getBool(
  31. SharedPreferenceService.appStyleNeedGreyKey,
  32. defaultValue: false);
  33. loadThemeMode();
  34. _locale = LanguageListExt.getCurrentLanguage().getLocale();
  35. // 监听黑白变化
  36. EventBus().on(EventBus.greyColorChangeGlobalMsg, _eventMsgId, (arg) {
  37. OLogger.d('接收到了 全局黑白的变化');
  38. changeGreyColor();
  39. });
  40. super.initState();
  41. }
  42. /// 全局黑白 或者 还原
  43. void changeGreyColor() {
  44. setState(() {
  45. needGrey = SharedPreferenceService.to.getBool(
  46. SharedPreferenceService.appStyleNeedGreyKey,
  47. defaultValue: false);
  48. });
  49. }
  50. /// 主题皮肤相关信息
  51. void loadThemeMode() {
  52. mode = SharedPreferenceService.to.getThemeMode();
  53. OLogger.d('当前系统主题mode: ${mode.name}');
  54. final skin = SharedPreferenceService.to.getThemeSkin();
  55. lightTheme = AppTheme.getLightThemeBySkin(skin);
  56. darkTheme = AppTheme.getDarkThemeBySkin(skin);
  57. OLogger.d('当前系统主题皮肤: ${skin.name}');
  58. }
  59. // This widget is the root of your application.
  60. @override
  61. Widget build(BuildContext context) {
  62. OLogger.d('页面根 MyApp build 。。。。。。。。。。。。');
  63. return O2UI.greyWidget(
  64. RefreshConfiguration(
  65. headerBuilder: () => const ClassicHeader(),
  66. footerBuilder: () => const ClassicFooter(),
  67. hideFooterWhenNotFull: true,
  68. headerTriggerDistance: 80,
  69. maxOverScrollExtent: 100,
  70. footerTriggerDistance: 150,
  71. child: OrientationBuilder(builder: (context, orientation) {
  72. final isPad = SharedPreferenceService.to
  73. .getBool(SharedPreferenceService.isPadKey);
  74. final screenWidth = MediaQuery.of(context).size.width;
  75. OLogger.d(
  76. 'OrientationBuilder, orientation: $orientation isPad:$isPad screenWidth:$screenWidth');
  77. return isPad && orientation == Orientation.landscape
  78. ? blurBackground(screenWidth)
  79. : getMApp();
  80. }),
  81. ),
  82. needGrey);
  83. }
  84. Widget blurBackground(double screenWidth) {
  85. return Stack(alignment: Alignment.topLeft, children: [
  86. const Positioned.fill(
  87. child: AssetsImageView(
  88. 'launch_background.png',
  89. fit: BoxFit.fill,
  90. ),
  91. ),
  92. Positioned.fill(
  93. child: BackdropFilter(
  94. filter: ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0),
  95. child: FractionallySizedBox(
  96. widthFactor: screenWidth < 750 ? 1.0 : 750 / screenWidth,
  97. child: getMApp(),
  98. )),
  99. )
  100. ]);
  101. }
  102. GetMaterialApp getMApp() {
  103. return GetMaterialApp(
  104. title: 'appName'.tr,
  105. enableLog: true,
  106. themeMode: mode,
  107. theme: lightTheme, // 亮色主题
  108. darkTheme: darkTheme, // 暗色主题
  109. logWriterCallback: OLogger.writeFromGetx,
  110. defaultTransition: Transition.fade,
  111. initialRoute: O2OARoutes.splash,
  112. getPages: O2OAPages.pages,
  113. unknownRoute: O2OAPages.unknownRoute,
  114. // loading插件
  115. builder: EasyLoading.init(),
  116. // 多语言
  117. locale: _locale,
  118. fallbackLocale: Languages.fallbackLocale,
  119. translations: Languages(),
  120. localizationsDelegates: const [
  121. GlobalMaterialLocalizations.delegate,
  122. GlobalWidgetsLocalizations.delegate,
  123. GlobalCupertinoLocalizations.delegate,
  124. RefreshLocalizations.delegate,
  125. ],
  126. supportedLocales: const [
  127. Locale('zh', 'CN'),
  128. Locale('en', 'US'),
  129. Locale('zh', 'HK'),
  130. ],
  131. );
  132. }
  133. }