|
- import 'dart:io';
-
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter_update_dialog/flutter_update_dialog.dart';
- // import 'package:flutter_xupdate/flutter_xupdate.dart';
- import 'package:package_info/package_info.dart';
- import 'package:zhiying_comm/util/update/app_update_model.dart';
- import 'package:zhiying_comm/zhiying_comm.dart';
- import 'package:fluttertoast/fluttertoast.dart';
-
- import '../log/let_log.dart';
-
- ///
- /// App更新工具类
- ///
- class AppUpdateUtil {
- ///
- /// 初始化更新插件
- ///
- static void initXUpdate() {
- if (Platform.isAndroid) {
- // FlutterXUpdate.init(
- //
- // ///是否输出日志
- // debug: true,
- //
- // ///是否使用post请求
- // isPost: false,
- //
- // ///post请求是否是上传json
- // isPostJson: false,
- //
- // ///是否开启自动模式
- // isWifiOnly: false,
- //
- // ///是否开启自动模式
- // isAutoMode: false,
- //
- // ///需要设置的公共参数
- // supportSilentInstall: false,
- //
- // ///在下载过程中,如果点击了取消的话,是否弹出切换下载方式的重试提示弹窗
- // enableRetry: false)
- // .then((value) {
- // // updateMessage("初始化成功: $value");
- // Logger.log('初始化成功: $value');
- // }).catchError((error) {
- // print(error);
- // });
-
- // FlutterXUpdate.setErrorHandler(onUpdateError: (Map<String, dynamic> message) async {
- // Logger.warn(message);
- // // setState(() {
- // // _message = "$message";
- // // });
- // });
- } else {
- // updateMessage("ios暂不支持XUpdate更新");
- Logger.log('ios暂不支持XUpdate更新');
- }
- }
-
- ///
- /// 检查并且更新app
- ///
- static void updateApp(BuildContext context) async {
- PackageInfo packageInfo = await PackageInfo.fromPlatform();
- Logger.log('version = ${packageInfo.version}, buildNum = ${packageInfo.buildNumber}');
- var result = await NetUtil.post('/api/v1/appcheck', params: {'app_version': packageInfo.buildNumber}, method: NetMethod.GET);
- customStyle(context, onUpdate: (){
- Navigator.pop(context);
- });
- // UpdateEntity updateEntity = await _checkAppUpdate();
- // // 有新版本,进行更新
- // if (!EmptyUtil.isEmpty(updateEntity) && updateEntity.hasUpdate) {
- // if (Platform.isAndroid) {
- // FlutterXUpdate.updateByInfo(
- // updateEntity: updateEntity,
- // supportBackgroundUpdate: true,
- // );
- // } else if (Platform.isIOS) {
- // // TODO 完成IOS更新
- // String updateUrl = updateEntity.downloadUrl;
- // customStyle(context, onUpdate: (){
- //
- //
- //
- // Navigator.pop(context);
- // });
- // }
- // } else {
- // Fluttertoast.showToast(msg: '已经是最新版本了~');
- // }
- }
-
- /// 检查是否有新版本
- // static Future<UpdateEntity> _checkAppUpdate() async {
- // try {
- // PackageInfo packageInfo = await PackageInfo.fromPlatform();
- // // var result = await NetUtil.post('/api/v1/appcheck', params: {'app_version': packageInfo.version}, method: NetMethod.GET);
- //
- // var result = {
- // 'dialog': '1',
- // 'is_force': '0',
- // 'app_version': '5',
- // 'app_version_name': '5.0.0',
- // 'tip': '\n1、优化api接口。\n2、添加使用demo演示。\n3、新增自定义更新服务API接口。\n4、优化更新提示界面。',
- // 'app_download_url': 'http://imtt.dd.qq.com/16891/apk/FAD58DEFE56A938D86B123DEA4E298EB.apk?fsname=com.hairuyi.www_5.8.8_469.apk&hsr=4d5s',
- // };
- // AppUpdateModel model = AppUpdateModel.fromJson(result);
- // if (!EmptyUtil.isEmpty(model)) {
- // // ⚠️ isIgnorable 目前只有两种状态,为强制更新和不强制更新(每次进来都会提示更新UI弹窗)
- // return UpdateEntity(
- // hasUpdate: model?.dialog == '1',
- // isIgnorable: false,
- // isForce: model?.isForce == '1',
- // versionCode: int.parse(model?.appVersion ?? '1'),
- // versionName: model?.appVersionName,
- // downloadUrl: model?.appDownLoadUrl,
- // updateContent: model?.tip,
- // );
- // }
- // } catch (e, s) {
- // Logger.error(e, s);
- // }
- // return null;
- // }
-
- static void customStyle(BuildContext context, {@required VoidCallback onUpdate }) {
- UpdateDialog.showUpdate(context,
- width: 250,
- title: "是否升级到4.1.4版本?",
- updateContent: "新版本大小:2.0M\n1.xxxxxxx\n2.xxxxxxx\n3.xxxxxxx",
- titleTextSize: 14,
- contentTextSize: 12,
- buttonTextSize: 12,
- // topImage: Image.asset('assets/bg_update_top.png'),
- extraHeight: 5,
- radius: 8,
- themeColor: Colors.redAccent,
- progressBackgroundColor: Color(0x5AFFAC5D),
- isForce: false,
- updateButtonText: '升级',
- ignoreButtonText: '忽略此版本',
- enableIgnore: true, onIgnore: () {
- Navigator.pop(context);
- }, onUpdate: onUpdate);
- }
- }
|