|
|
|
|
|
|
|
import 'dart:io';
|
|
|
|
import 'dart:math';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
|
|
|
import 'package:install_plugin/install_plugin.dart';
|
|
|
|
import 'package:one_key/models/version.dart';
|
|
|
|
import 'package:one_key/net/dio_util.dart';
|
|
|
|
import 'package:one_key/res/colors.dart';
|
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
|
|
|
|
class UpdateModel{
|
|
|
|
|
|
|
|
int _progress;//下载进度
|
|
|
|
Function _setState;
|
|
|
|
//检查更新
|
|
|
|
Future<Version> checkUpdate(BuildContext context){
|
|
|
|
Future.delayed(new Duration(milliseconds: 1000),(){
|
|
|
|
if(Random().nextInt(10)>0){
|
|
|
|
Version version=Version.fromJson({
|
|
|
|
"version":"v1.0.3",
|
|
|
|
"file_url":"http://xt03.colorcun.com/uploads/20191105/67e38355c41528b24a97e9ac71a221ea.apk",
|
|
|
|
"content":"1.bug修复;\n2.功能更新;"
|
|
|
|
});
|
|
|
|
showUpdateDialog(context, version);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//拒绝订单弹框
|
|
|
|
showUpdateDialog(BuildContext context,Version version){
|
|
|
|
showDialog(context: context,builder: (context){
|
|
|
|
return AlertDialog(
|
|
|
|
title: Text("版本更新${version.version}"),
|
|
|
|
titleTextStyle: TextStyle(color: MyColours.color_353535,fontSize: ScreenUtil().setSp(32)),
|
|
|
|
content: Text("${version.content}"),
|
|
|
|
contentTextStyle:TextStyle(color: MyColours.color_828282,fontSize: ScreenUtil().setSp(28)),
|
|
|
|
actions: <Widget>[
|
|
|
|
FlatButton(
|
|
|
|
child: Text("取消",style: TextStyle(fontSize: ScreenUtil().setSp(32)),),
|
|
|
|
onPressed: (){
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
FlatButton(
|
|
|
|
child: Text("去更新",style: TextStyle(fontSize: ScreenUtil().setSp(32)),),
|
|
|
|
onPressed: () async {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
if(Platform.isAndroid){
|
|
|
|
downloadApk(context,version.file_url);
|
|
|
|
}else{
|
|
|
|
InstallPlugin.gotoAppStore("itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=1510697490");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//下载更新版本
|
|
|
|
Future<void> downloadApk(BuildContext context,String file_url) async {
|
|
|
|
Map<PermissionGroup, PermissionStatus> permissions =
|
|
|
|
await PermissionHandler().requestPermissions([PermissionGroup.storage]);
|
|
|
|
if(permissions[PermissionGroup.storage] == PermissionStatus.granted){
|
|
|
|
final dir=await getExternalStorageDirectory();
|
|
|
|
String savePath=dir.path+"/one_key";
|
|
|
|
DioUtil().download(file_url, savePath,onProgress: (received,total){
|
|
|
|
int progress=((received/total)*100).round();
|
|
|
|
_progress=progress;
|
|
|
|
_setState((){});
|
|
|
|
if(received==total){//下载完成
|
|
|
|
InstallPlugin.installApk(savePath,"com.naji.one_key");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//显示下载进度条
|
|
|
|
Function showDownloadProgress(BuildContext context){
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
barrierDismissible: false,
|
|
|
|
builder: (context) {
|
|
|
|
return AlertDialog(
|
|
|
|
content: StatefulBuilder(builder: (context, setState) {
|
|
|
|
if(_progress==100){
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
|
|
|
|
}
|
|
|
|
_setState=setState;
|
|
|
|
return Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: <Widget>[
|
|
|
|
CircularProgressIndicator(),
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.only(top: 26),
|
|
|
|
child: Text("正在下载...${_progress}%"),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
return _setState;
|
|
|
|
}
|
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|