基础组件库
 
 
 
 
 

154 lines
5.4 KiB

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