295 lines
11 KiB
Dart
295 lines
11 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
// import 'package:flutter_install_app/flutter_install_app.dart';
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
|
import 'package:url_launcher/url_launcher.dart';
|
|
|
|
import '../../network/dio_manager.dart';
|
|
import '../../util/util.dart';
|
|
|
|
class UpdateDialog extends StatefulWidget {
|
|
final bool isDismiss;
|
|
final UpdateInfo updateInfo;
|
|
|
|
UpdateDialog({required this.isDismiss, required this.updateInfo});
|
|
|
|
@override
|
|
_CustomDialogState createState() => _CustomDialogState();
|
|
}
|
|
|
|
double myProgress = 0;
|
|
|
|
class _CustomDialogState extends State<UpdateDialog> {
|
|
bool isDownload = false;
|
|
bool isShowWeb = false;
|
|
|
|
void incrementCounter() {}
|
|
|
|
void setDownloadUi() {
|
|
setState(() {
|
|
isDownload = true;
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return WillPopScope(
|
|
onWillPop: () async {
|
|
return widget.isDismiss;
|
|
},
|
|
child: Dialog(
|
|
backgroundColor: Colors.transparent,
|
|
child: Container(
|
|
height: 317.sp,
|
|
padding: const EdgeInsets.all(1.0),
|
|
child: Stack(
|
|
children: [
|
|
Container(
|
|
decoration: BoxDecoration(
|
|
shape: BoxShape.rectangle,
|
|
borderRadius: BorderRadius.circular(10.0),
|
|
gradient: const LinearGradient(
|
|
colors: [Color(0xFFDD3DF4), Color(0xFF30FFD9)],
|
|
begin: Alignment.topCenter,
|
|
end: Alignment.bottomCenter,
|
|
),
|
|
),
|
|
),
|
|
Container(
|
|
margin: EdgeInsets.all(1.sp),
|
|
decoration: BoxDecoration(
|
|
shape: BoxShape.rectangle,
|
|
borderRadius: BorderRadius.circular(10.0),
|
|
gradient: const LinearGradient(
|
|
colors: [Color(0xFF4C3E5F), Color(0xFF324140)],
|
|
begin: Alignment.topCenter,
|
|
end: Alignment.bottomCenter,
|
|
),
|
|
),
|
|
),
|
|
Container(
|
|
margin: EdgeInsets.only(top: 24.sp),
|
|
child: Column(
|
|
children: [
|
|
Center(
|
|
child: Text(
|
|
"发现新版本",
|
|
style: TextStyle(color: Colors.white, fontSize: 16.sp),
|
|
),
|
|
),
|
|
// Container(
|
|
// margin: EdgeInsets.only(top: 14.sp),
|
|
// alignment: Alignment.center,
|
|
// child: Image(
|
|
// image: AssetImage(getBaseImage("ic_launcher")),
|
|
// width: 70.sp,
|
|
// height: 70.sp,
|
|
// ),
|
|
// ),
|
|
SizedBox(
|
|
height: 215.sp,
|
|
child: ListView(
|
|
children: [
|
|
Container(
|
|
margin: EdgeInsets.only(
|
|
top: 12.sp, left: 14.sp, right: 14.sp),
|
|
alignment: Alignment.center,
|
|
child: Text(
|
|
widget.updateInfo.updateInfo,
|
|
textAlign: TextAlign.left,
|
|
style: TextStyle(
|
|
color: const Color(0xCCF7FAFA),
|
|
fontSize: 16.sp),
|
|
),
|
|
)
|
|
],
|
|
),
|
|
),
|
|
isDownload
|
|
? GestureDetector(
|
|
onTap: () async {
|
|
//incrementCounter();
|
|
},
|
|
child: Column(
|
|
children: [
|
|
Container(
|
|
margin: EdgeInsets.only(top: 4.sp),
|
|
child: CircularProgressIndicator(
|
|
value: myProgress,
|
|
backgroundColor: Colors.grey,
|
|
valueColor:
|
|
const AlwaysStoppedAnimation<Color>(
|
|
Color(0xFF30FFD9)),
|
|
strokeWidth: 4.0.sp,
|
|
),
|
|
),
|
|
GestureDetector(
|
|
onTap: () async {
|
|
final String appStoreUrl = widget.updateInfo
|
|
.downloadUrl; // App Store链接示例
|
|
|
|
if (await canLaunch(appStoreUrl)) {
|
|
await launch(appStoreUrl,
|
|
forceSafariVC: false);
|
|
} else {
|
|
throw 'Could not open App Store.';
|
|
}
|
|
},
|
|
child:isShowWeb? Container(
|
|
margin: EdgeInsets.only(top: 4.sp),
|
|
child: Text(
|
|
"下载太慢?试试游览器下载吧。",
|
|
style: TextStyle(
|
|
color: Colors.blue, // 设置蓝色颜色
|
|
decoration:
|
|
TextDecoration.underline, // 添加下划线
|
|
),
|
|
),
|
|
): Container(),
|
|
)
|
|
],
|
|
),
|
|
)
|
|
: GestureDetector(
|
|
behavior: HitTestBehavior.opaque,
|
|
onTap: () async {
|
|
if (Platform.isIOS) {
|
|
final String appStoreUrl = widget
|
|
.updateInfo.downloadUrl; // App Store链接示例
|
|
if (await canLaunch(appStoreUrl)) {
|
|
await launch(appStoreUrl,
|
|
forceSafariVC: false);
|
|
} else {
|
|
throw 'Could not open App Store.';
|
|
}
|
|
} else {
|
|
final String appStoreUrl = widget
|
|
.updateInfo.downloadUrl; // App Store链接示例
|
|
|
|
if (appStoreUrl.contains("apk")) {
|
|
updataApk(appStoreUrl);
|
|
setDownloadUi();
|
|
await Future.delayed(Duration(seconds: 10));
|
|
setState(() {
|
|
isShowWeb = true;
|
|
});
|
|
|
|
} else {
|
|
if (await canLaunch(appStoreUrl)) {
|
|
await launch(appStoreUrl,
|
|
forceSafariVC: false);
|
|
} else {
|
|
throw 'Could not open App Store.';
|
|
}
|
|
}
|
|
}
|
|
|
|
//Navigator.pop(context);
|
|
// logic.setBlock();
|
|
},
|
|
child: Container(
|
|
width: 200.sp,
|
|
margin: EdgeInsets.only(top: 10.sp),
|
|
decoration: BoxDecoration(
|
|
borderRadius: BorderRadius.circular(17),
|
|
gradient: const LinearGradient(
|
|
colors: [
|
|
Color(0xFF06F9FA),
|
|
Color(0xFFDC5BFD),
|
|
],
|
|
begin: Alignment.centerLeft,
|
|
end: Alignment.centerRight,
|
|
),
|
|
),
|
|
padding: EdgeInsets.only(
|
|
top: 10.sp,
|
|
bottom: 10.sp,
|
|
left: 52.sp,
|
|
right: 52.sp),
|
|
child: Center(
|
|
child: Text(
|
|
"立即更新",
|
|
style: TextStyle(
|
|
color: Colors.white,
|
|
fontSize: 12.sp,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
)
|
|
],
|
|
),
|
|
),
|
|
Positioned(
|
|
right: 8.sp,
|
|
top: 8.sp,
|
|
child: widget.isDismiss
|
|
? GestureDetector(
|
|
onTap: () {
|
|
Navigator.pop(context);
|
|
},
|
|
child: Icon(
|
|
Icons.close,
|
|
color: Colors.white,
|
|
size: 24.sp,
|
|
))
|
|
: Container())
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
updataApk(String downloadUrl) async {
|
|
//SmartDialog.showLoading();
|
|
var filePath = await getApplicationSupportDirectoryPath();
|
|
filePath = filePath + "leyuan.apk";
|
|
var data = await DioManager.instance
|
|
.download(downloadUrl, filePath, (received, total) {
|
|
double progress = received / total * 100;
|
|
setState(() {
|
|
myProgress = received / total;
|
|
});
|
|
print('Download progress: $progress%');
|
|
})
|
|
.then((value) => {print(value)})
|
|
.catchError((error) {
|
|
// SmartDialog.dismiss();
|
|
print(error);
|
|
});
|
|
// await AppInstaller.installApk(filePath, actionRequired: false);
|
|
}
|
|
}
|
|
|
|
class UpdateInfo {
|
|
final String version; //最新版本号
|
|
final int update; //是否要更新
|
|
final int constraint; //是否强制
|
|
final String publishTime;
|
|
final String downloadUrl;
|
|
final String updateInfo;
|
|
|
|
UpdateInfo({
|
|
required this.version,
|
|
required this.update,
|
|
required this.constraint,
|
|
required this.publishTime,
|
|
required this.downloadUrl,
|
|
required this.updateInfo,
|
|
});
|
|
|
|
factory UpdateInfo.fromJson(Map<String, dynamic> json) {
|
|
return UpdateInfo(
|
|
version: json['version'],
|
|
update: json['update'],
|
|
constraint: json['constraint'],
|
|
publishTime: json['publish_time'],
|
|
downloadUrl: json['download_url'],
|
|
updateInfo: json['update_info'],
|
|
);
|
|
}
|
|
}
|