基础组件库
 
 
 
 
 

155 lines
5.5 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> {
  34. final GlobalKey navigatorKey = new GlobalKey();
  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. Timer(Duration(milliseconds: 300), (){
  54. NativeUtil.notifyInitSuccess();
  55. });
  56. PackageInfo packageInfo = await PackageInfo.fromPlatform();
  57. String versionNumber = await SharedPreferencesUtil.getStringValue(Constants.versionNumber);
  58. String currentVersionCode = packageInfo.buildNumber?.toString() ?? '';
  59. if (versionNumber == null || versionNumber == '' || versionNumber != currentVersionCode) {
  60. await _showGuideImage();
  61. SharedPreferencesUtil.setStringValue(Constants.versionNumber, currentVersionCode);
  62. }
  63. if (widget.builder != null) {
  64. Navigator.of(context).pushReplacement(CupertinoPageRoute(builder: widget.builder));
  65. } else {
  66. // Navigator.of(context).pushReplacement(
  67. // CupertinoPageRoute(builder: (context) => HomePage()));
  68. Navigator.of(context).pushReplacementNamed('/homePage');
  69. }
  70. }).catchError((err) async {
  71. // if (err.toString().contains("BaseSettingModel")) {
  72. // await showDialog(
  73. // context: context,
  74. // child: TipDialog(
  75. // content: "基本配置加载失败!请检查网络是否可用后再启动应用!",
  76. // ));
  77. // SystemNavigator.pop();
  78. // }
  79. if(showTime<3){
  80. showTime++;
  81. Fluttertoast.showToast(msg: "配置基础数据失败!请检查网络是否可用");
  82. }
  83. Timer(Duration(seconds: 5), () {
  84. validateInit();
  85. });
  86. Logger.error(err);
  87. });
  88. // } else {
  89. // ///在无网络情况下提示用户打开网络
  90. // if(showTime<3){
  91. // showTime++;
  92. // Fluttertoast.showToast(msg: "无法连接网络!请打开移动网络/连接WIFI后再尝试");
  93. // }
  94. //
  95. // Timer(Duration(seconds: 5), () {
  96. // validateInit();
  97. // });
  98. // Logger.error('未知网络,APP初始化失败');
  99. // print('unknow');
  100. // }
  101. // });
  102. }
  103. Future _showGuideImage() async {
  104. try {
  105. // 引导页
  106. AppConfigGuideModel guide = AppConfigModel.getConfig()?.guideImage;
  107. if (guide != null && guide.images.length > 0) {
  108. Dio dio = Dio();
  109. print("加载图片");
  110. List<Uint8List> guideImages = List();
  111. for (int i = 0; i < guide.images.length; i++) {
  112. Response response = await dio.get(guide.images[i], options: Options(responseType: ResponseType.bytes));
  113. if (response.statusCode == 200) {
  114. Uint8List data = Uint8List.fromList(response.data);
  115. guideImages.add(data);
  116. }
  117. }
  118. await Navigator.of(context).push(CupertinoPageRoute(builder: (context) => GuidePage(guide, guideImages)));
  119. }
  120. } catch (e) {
  121. throw "引导图加载失败";
  122. }
  123. }
  124. @override
  125. Widget build(BuildContext context) {
  126. return Container(
  127. color: Colors.white,
  128. // child: Image.asset(
  129. // 'assets/images/launch_image/launch_image.png',
  130. // package: 'zhiying_base_widget',
  131. // fit: BoxFit.fill,
  132. // ),
  133. );
  134. }
  135. }