基础库
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

151 righe
5.2 KiB

  1. import 'dart:io';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_update_dialog/flutter_update_dialog.dart';
  5. // import 'package:flutter_xupdate/flutter_xupdate.dart';
  6. import 'package:package_info/package_info.dart';
  7. import 'package:zhiying_comm/util/update/app_update_model.dart';
  8. import 'package:zhiying_comm/zhiying_comm.dart';
  9. import 'package:fluttertoast/fluttertoast.dart';
  10. import '../log/let_log.dart';
  11. ///
  12. /// App更新工具类
  13. ///
  14. class AppUpdateUtil {
  15. ///
  16. /// 初始化更新插件
  17. ///
  18. static void initXUpdate() {
  19. if (Platform.isAndroid) {
  20. // FlutterXUpdate.init(
  21. //
  22. // ///是否输出日志
  23. // debug: true,
  24. //
  25. // ///是否使用post请求
  26. // isPost: false,
  27. //
  28. // ///post请求是否是上传json
  29. // isPostJson: false,
  30. //
  31. // ///是否开启自动模式
  32. // isWifiOnly: false,
  33. //
  34. // ///是否开启自动模式
  35. // isAutoMode: false,
  36. //
  37. // ///需要设置的公共参数
  38. // supportSilentInstall: false,
  39. //
  40. // ///在下载过程中,如果点击了取消的话,是否弹出切换下载方式的重试提示弹窗
  41. // enableRetry: false)
  42. // .then((value) {
  43. // // updateMessage("初始化成功: $value");
  44. // Logger.log('初始化成功: $value');
  45. // }).catchError((error) {
  46. // print(error);
  47. // });
  48. // FlutterXUpdate.setErrorHandler(onUpdateError: (Map<String, dynamic> message) async {
  49. // Logger.warn(message);
  50. // // setState(() {
  51. // // _message = "$message";
  52. // // });
  53. // });
  54. } else {
  55. // updateMessage("ios暂不支持XUpdate更新");
  56. Logger.log('ios暂不支持XUpdate更新');
  57. }
  58. }
  59. ///
  60. /// 检查并且更新app
  61. ///
  62. static void updateApp(BuildContext context) async {
  63. PackageInfo packageInfo = await PackageInfo.fromPlatform();
  64. Logger.log('version = ${packageInfo.version}, buildNum = ${packageInfo.buildNumber}');
  65. var result = await NetUtil.post('/api/v1/appcheck', params: {'app_version': packageInfo.buildNumber}, method: NetMethod.GET);
  66. customStyle(context, onUpdate: (){
  67. Navigator.pop(context);
  68. });
  69. // UpdateEntity updateEntity = await _checkAppUpdate();
  70. // // 有新版本,进行更新
  71. // if (!EmptyUtil.isEmpty(updateEntity) && updateEntity.hasUpdate) {
  72. // if (Platform.isAndroid) {
  73. // FlutterXUpdate.updateByInfo(
  74. // updateEntity: updateEntity,
  75. // supportBackgroundUpdate: true,
  76. // );
  77. // } else if (Platform.isIOS) {
  78. // // TODO 完成IOS更新
  79. // String updateUrl = updateEntity.downloadUrl;
  80. // customStyle(context, onUpdate: (){
  81. //
  82. //
  83. //
  84. // Navigator.pop(context);
  85. // });
  86. // }
  87. // } else {
  88. // Fluttertoast.showToast(msg: '已经是最新版本了~');
  89. // }
  90. }
  91. /// 检查是否有新版本
  92. // static Future<UpdateEntity> _checkAppUpdate() async {
  93. // try {
  94. // PackageInfo packageInfo = await PackageInfo.fromPlatform();
  95. // // var result = await NetUtil.post('/api/v1/appcheck', params: {'app_version': packageInfo.version}, method: NetMethod.GET);
  96. //
  97. // var result = {
  98. // 'dialog': '1',
  99. // 'is_force': '0',
  100. // 'app_version': '5',
  101. // 'app_version_name': '5.0.0',
  102. // 'tip': '\n1、优化api接口。\n2、添加使用demo演示。\n3、新增自定义更新服务API接口。\n4、优化更新提示界面。',
  103. // 'app_download_url': 'http://imtt.dd.qq.com/16891/apk/FAD58DEFE56A938D86B123DEA4E298EB.apk?fsname=com.hairuyi.www_5.8.8_469.apk&hsr=4d5s',
  104. // };
  105. // AppUpdateModel model = AppUpdateModel.fromJson(result);
  106. // if (!EmptyUtil.isEmpty(model)) {
  107. // // ⚠️ isIgnorable 目前只有两种状态,为强制更新和不强制更新(每次进来都会提示更新UI弹窗)
  108. // return UpdateEntity(
  109. // hasUpdate: model?.dialog == '1',
  110. // isIgnorable: false,
  111. // isForce: model?.isForce == '1',
  112. // versionCode: int.parse(model?.appVersion ?? '1'),
  113. // versionName: model?.appVersionName,
  114. // downloadUrl: model?.appDownLoadUrl,
  115. // updateContent: model?.tip,
  116. // );
  117. // }
  118. // } catch (e, s) {
  119. // Logger.error(e, s);
  120. // }
  121. // return null;
  122. // }
  123. static void customStyle(BuildContext context, {@required VoidCallback onUpdate }) {
  124. UpdateDialog.showUpdate(context,
  125. width: 250,
  126. title: "是否升级到4.1.4版本?",
  127. updateContent: "新版本大小:2.0M\n1.xxxxxxx\n2.xxxxxxx\n3.xxxxxxx",
  128. titleTextSize: 14,
  129. contentTextSize: 12,
  130. buttonTextSize: 12,
  131. // topImage: Image.asset('assets/bg_update_top.png'),
  132. extraHeight: 5,
  133. radius: 8,
  134. themeColor: Colors.redAccent,
  135. progressBackgroundColor: Color(0x5AFFAC5D),
  136. isForce: false,
  137. updateButtonText: '升级',
  138. ignoreButtonText: '忽略此版本',
  139. enableIgnore: true, onIgnore: () {
  140. Navigator.pop(context);
  141. }, onUpdate: onUpdate);
  142. }
  143. }