circle_app/circle_app/lib/app/dialog/UpdateDialog.dart
2023-07-19 17:52:53 +08:00

227 lines
7.9 KiB
Dart

import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:url_launcher/url_launcher.dart';
import '../../main.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();
}
class _CustomDialogState extends State<UpdateDialog> {
double progress = 0;
bool isDownload = false;
void incrementCounter() {
setState(() {
progress = progress + 0.1;
});
}
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: 277.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,
),
),
Container(
margin: EdgeInsets.only(
top: 12.sp, left: 14.sp, right: 14.sp),
alignment: Alignment.center,
child: Text(
widget.updateInfo.updateInfo,
textAlign: TextAlign.center,
style: TextStyle(
color: const Color(0xCCF7FAFA), fontSize: 16.sp),
),
),
isDownload
? GestureDetector(
onTap: () async {
if (Platform.isIOS) {
final String appStoreUrl = 'https://apps.apple.com/app/id$iOSAPPid'; // App Store链接示例
if (await canLaunch(appStoreUrl)) {
await launch(appStoreUrl);
} else {
throw 'Could not open App Store.';
}
} else {
updataApk(widget.updateInfo.downloadUrl);
}
//incrementCounter();
},
child: Container(
margin: EdgeInsets.only(top: 24.sp),
child: CircularProgressIndicator(
value: progress,
backgroundColor: Colors.grey,
valueColor: const AlwaysStoppedAnimation<Color>(
Colors.blue),
strokeWidth: 4.0.sp,
),
),
)
: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
setDownloadUi();
//Navigator.pop(context);
// logic.setBlock();
},
child: Container(
width: 200.sp,
margin: EdgeInsets.only(top: 24.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,
),
),
),
),
)
],
),
)
],
),
),
),
);
}
}
updataApk(String downloadUrl) async {
//SmartDialog.showLoading();
var filePath = await getApplicationSupportDirectoryPath();
filePath = filePath + "kuayou.apk";
var data = await DioManager.instance
.download(
"https://xidi-official-website.oss-cn-shenzhen.aliyuncs.com/%E8%B7%A8%E5%8F%8B_2.3.4.apk",
filePath, (received, total) {
double progress = received / total * 100;
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'],
);
}
}