o2_app.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import 'package:fluro/fluro.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:o2_flutter/common/routers/application.dart';
  4. import 'package:o2_flutter/common/routers/routers.dart';
  5. import 'package:o2_flutter/common/utils/o2_api_manager.dart';
  6. import 'package:o2_flutter/common/utils/o2_user_manager.dart';
  7. import 'package:o2_flutter/common/utils/shared_preference_manager.dart';
  8. import 'package:o2_flutter/common/utils/toast_util.dart';
  9. import 'package:o2_flutter/o2.dart';
  10. ///
  11. /// 根据路由跳转到对应的页面
  12. ///
  13. class O2App extends StatefulWidget {
  14. final String route;
  15. O2App(this.route, {Key? key}) : super(key: key) {
  16. // 初始化路由
  17. final router = FluroRouter();
  18. Routes.configureRoutes(router);
  19. AppRouterManager.instance.initRouter(router);
  20. // 创建一个通道 和 原生通信
  21. O2MethodChannelManager.instance.initMethodChannel();
  22. }
  23. @override
  24. State<StatefulWidget> createState() => _O2AppState();
  25. }
  26. class _O2AppState extends State<O2App> {
  27. MaterialColor primarySwatch = o2RedSwatch;
  28. bool isInit = false;
  29. String toRoute = '';
  30. void setTheme() {
  31. //初始化主题
  32. String? theme = SharedPreferenceManager.instance.theme;
  33. if (theme != null && theme == blueThemeKey) {
  34. primarySwatch = o2BlueSwatch;
  35. }
  36. }
  37. /// 读取从 android端 同步过来的服务器信息
  38. void initO2Config() async {
  39. try {
  40. var map = await O2MethodChannelManager.instance.methodChannel.invokeMethod(method_name_o2_config);
  41. if (map != null) {
  42. debugPrintStack(label: '找到了Native的频道。。。。。。。。');
  43. if (map is Map) {
  44. if (map.containsKey(param_name_o2_theme)) {
  45. await SharedPreferenceManager.instance
  46. .initTheme(map[param_name_o2_theme]);
  47. }
  48. if (map.containsKey(param_name_o2_user)) {
  49. await O2UserManager.instance.initUser(map[param_name_o2_user]);
  50. }
  51. if (map.containsKey(param_name_o2_unit)) {
  52. await O2ApiManager.instance.initO2Unit(map[param_name_o2_unit]);
  53. }
  54. if (map.containsKey(param_name_o2_center_server)) {
  55. await O2ApiManager.instance
  56. .initO2CenterServer(map[param_name_o2_center_server]);
  57. }
  58. } else {
  59. debugPrintStack(label: '.....不知道是啥。。。');
  60. }
  61. setState(() {
  62. setTheme();
  63. isInit = true;
  64. });
  65. } else {
  66. setState(() {
  67. setTheme();
  68. isInit = true;
  69. // 错误页面
  70. toRoute = Routes.errorLoad;
  71. });
  72. }
  73. } catch (e) {
  74. print(e);
  75. setState(() {
  76. setTheme();
  77. isInit = true;
  78. // 错误页面
  79. toRoute = Routes.errorLoad;
  80. });
  81. }
  82. }
  83. @override
  84. void initState() {
  85. super.initState();
  86. toRoute = widget.route;
  87. initO2Config();
  88. }
  89. @override
  90. Widget build(BuildContext context) {
  91. if (isInit) {
  92. return MaterialApp(
  93. title: '',
  94. theme: ThemeData(
  95. primarySwatch: primarySwatch,
  96. ),
  97. home: O2SplashPage(toRoute),
  98. onGenerateRoute: AppRouterManager.instance.router?.generator,
  99. );
  100. } else {
  101. return Container(
  102. color: Colors.white,
  103. child: const Center(child: CircularProgressIndicator()),
  104. );
  105. }
  106. }
  107. }
  108. class O2SplashPage extends StatefulWidget {
  109. final String route;
  110. const O2SplashPage(this.route, {Key? key}) : super(key: key);
  111. @override
  112. State<StatefulWidget> createState() {
  113. return _O2SplashPageState();
  114. }
  115. }
  116. class _O2SplashPageState extends State<O2SplashPage> {
  117. _O2SplashPageState();
  118. @override
  119. void initState() {
  120. super.initState();
  121. countDown();
  122. }
  123. @override
  124. Widget build(BuildContext context) {
  125. // 初始化
  126. ToastHelper.init(context);
  127. return Scaffold(
  128. body: Container(
  129. color: Colors.white,
  130. child: const Center(child: CircularProgressIndicator()),
  131. ),
  132. );
  133. }
  134. void countDown() {
  135. var duration = const Duration(milliseconds: 500);
  136. Future.delayed(duration, routeTo);
  137. }
  138. void routeTo() {
  139. debugPrintStack(label: 'route To ...route: $widget.route');
  140. AppRouterManager.instance.router?.navigateTo(context, widget.route, clearStack: true);
  141. }
  142. }