基础组件库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

launch_page.dart 6.2 KiB

4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import 'dart:convert';
  2. import 'dart:async';
  3. import 'dart:io';
  4. import 'dart:typed_data';
  5. import 'package:flutter/cupertino.dart';
  6. import 'package:flutter/foundation.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:mobpush_plugin/mobpush_custom_message.dart';
  9. import 'package:mobpush_plugin/mobpush_notify_message.dart';
  10. import 'package:mobpush_plugin/mobpush_plugin.dart';
  11. import 'package:flutter/services.dart';
  12. import 'package:zhiying_base_widget/dialog/global_dialog/policy_dialog/policy_dialog.dart';
  13. import 'package:zhiying_base_widget/dialog/tip_dialog/tip_dialog.dart';
  14. import 'package:mobpush_plugin/mobpush_custom_message.dart';
  15. import 'package:mobpush_plugin/mobpush_notify_message.dart';
  16. import 'package:mobpush_plugin/mobpush_plugin.dart';
  17. import 'package:zhiying_base_widget/models/app_config_model.dart';
  18. import 'package:zhiying_base_widget/pages/guide_page/guide_page.dart';
  19. import 'package:zhiying_base_widget/pages/home_page/home_page.dart';
  20. import 'package:zhiying_comm/util/application.dart';
  21. import 'package:zhiying_comm/zhiying_comm.dart';
  22. import 'package:dio/dio.dart';
  23. import 'package:zhiying_base_widget/utils/contants.dart';
  24. import 'package:zhiying_comm/util/shared_prefe_util.dart';
  25. import 'package:package_info/package_info.dart';
  26. import 'package:fluttertoast/fluttertoast.dart';
  27. // 启动页,页面初始化等操作~跟原生启动页保持一致,防止白屏问题
  28. class LaunchPage extends StatefulWidget {
  29. final WidgetBuilder builder;
  30. const LaunchPage({Key key, this.builder}) : super(key: key);
  31. @override
  32. _LaunchPageState createState() => _LaunchPageState();
  33. }
  34. class _LaunchPageState extends State<LaunchPage> with TickerProviderStateMixin {
  35. final GlobalKey navigatorKey = new GlobalKey();
  36. int showTime = 0;
  37. @override
  38. void initState() {
  39. validateInit();
  40. super.initState();
  41. }
  42. validateInit() async {
  43. // Connectivity().checkConnectivity().then((connectivityResult) async {
  44. // if (connectivityResult == ConnectivityResult.mobile || connectivityResult == ConnectivityResult.wifi) {
  45. // I am connected to a wifi network.
  46. Logger.debug('ConnectivityResult.wifi');
  47. print('ConnectivityResult.wifi');
  48. ///检查是否覆盖安装
  49. //取masterId
  50. String lasterMasterId = await SharedPreferencesUtil.getStringValue(Constants.masterId, defaultVal: "1");
  51. Map setting = await NativeUtil.getSetting();
  52. String masterId = setting['master_id'];
  53. if (lasterMasterId != "1") {
  54. if (masterId != lasterMasterId) {
  55. await SharedPreferencesUtil.clean();
  56. }
  57. }
  58. SharedPreferencesUtil.setStringValue(Constants.masterId, masterId);
  59. Application.init().then((_) async {
  60. ///引导页
  61. PackageInfo packageInfo = await PackageInfo.fromPlatform();
  62. String versionNumber = await SharedPreferencesUtil.getStringValue(Constants.versionNumber);
  63. String currentVersionCode = packageInfo.buildNumber?.toString() ?? '';
  64. if (versionNumber == null || versionNumber == '' || versionNumber != currentVersionCode) {
  65. await _showGuideImage();
  66. SharedPreferencesUtil.setStringValue(Constants.versionNumber, currentVersionCode);
  67. }
  68. if (widget.builder != null) {
  69. Navigator.of(context).pushReplacement(CupertinoPageRoute(builder: widget.builder));
  70. } else {
  71. // Navigator.of(context).pushReplacement(
  72. // CupertinoPageRoute(builder: (context) => HomePage()));
  73. Navigator.of(context).pushReplacementNamed('/homePage');
  74. }
  75. }).catchError((err, s) async {
  76. print(s);
  77. // if (err.toString().contains("BaseSettingModel")) {
  78. // await showDialog(
  79. // context: context,
  80. // child: TipDialog(
  81. // content: "基本配置加载失败!请检查网络是否可用后再启动应用!",
  82. // ));
  83. // SystemNavigator.pop();
  84. // }
  85. if (showTime < 3) {
  86. showTime++;
  87. Fluttertoast.showToast(msg: "配置基础数据失败!请检查网络是否可用");
  88. }
  89. Timer(Duration(seconds: 5), () async {
  90. await SharedPreferencesUtil.setStringValue(GlobalConfig.GUIDE, "1");
  91. await SharedPreferencesUtil.setStringValue(GlobalConfig.MAIN_CONFIG, "1");
  92. validateInit();
  93. });
  94. Logger.error(err);
  95. });
  96. // } else {
  97. // ///在无网络情况下提示用户打开网络
  98. // if(showTime<3){
  99. // showTime++;
  100. // Fluttertoast.showToast(msg: "无法连接网络!请打开移动网络/连接WIFI后再尝试");
  101. // }
  102. //
  103. // Timer(Duration(seconds: 5), () {
  104. // validateInit();
  105. // });
  106. // Logger.error('未知网络,APP初始化失败');
  107. // print('unknow');
  108. // }
  109. // });
  110. }
  111. Future _showGuideImage() async {
  112. try {
  113. // 引导页
  114. AppConfigModel guide = AppConfigModel.getConfig();
  115. if (guide != null && guide.guideData.guideCss.length > 0) {
  116. Dio dio = Dio();
  117. print("加载图片");
  118. List<Uint8List> guideImages = List();
  119. List<Uint8List> bgImages = List();
  120. for (int i = 0; i < guide.guideData.guideCss.length; i++) {
  121. try {
  122. Response response = await dio.get(guide.guideData.guideCss[i].contentImage, options: Options(responseType: ResponseType.bytes));
  123. if (response.statusCode == 200) {
  124. Uint8List data = Uint8List.fromList(response.data);
  125. guideImages.add(data);
  126. }
  127. } catch (e, s) {
  128. guideImages.add(null);
  129. }
  130. try {
  131. Response response = await dio.get(guide.guideData.guideCss[i].bgImage, options: Options(responseType: ResponseType.bytes));
  132. if (response.statusCode == 200) {
  133. Uint8List data = Uint8List.fromList(response.data);
  134. bgImages.add(data);
  135. }
  136. } catch (e, s) {
  137. bgImages.add(null);
  138. }
  139. }
  140. NativeUtil.notifyInitSuccess();
  141. await Navigator.of(context).push(CupertinoPageRoute(builder: (context) => GuidePage(guide.guideData, guideImages, bgImages)));
  142. }
  143. } catch (e) {
  144. throw "引导图加载失败";
  145. }
  146. }
  147. @override
  148. Widget build(BuildContext context) {
  149. return Container(
  150. color: Colors.white,
  151. );
  152. }
  153. }