基础组件库
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.
 
 
 
 
 

162 line
5.9 KiB

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