You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 lines
7.8 KiB

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../../../data/models/channel_app_model.dart';
import '../../../data/services/version_update_service.dart';
/// 版本更新提示对话框 - 禁止外部关闭
class VersionUpdateDialog extends StatelessWidget {
final VersionUpdateInfo updateInfo;
final VoidCallback? onCancel;
final VoidCallback? onUpdate;
const VersionUpdateDialog({
super.key,
required this.updateInfo,
this.onCancel,
this.onUpdate,
});
@override
Widget build(BuildContext context) {
final isDarkMode = Theme.of(context).brightness == Brightness.dark;
// 在强制更新时添加拦截层
Widget dialogContent = _buildDialogContent(isDarkMode);
//if (updateInfo.updateType == UpdateType.force) {
return PopScope(
canPop: false, // 完全禁用返回键关闭
child: dialogContent,
);
//}
//return dialogContent;
}
/// 构建对话框内容
Widget _buildDialogContent(bool isDarkMode) {
return Dialog(
// 关键修改:禁用外部点击关闭
//barrierDismissible: false,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16.r),
),
child: Container(
width: 320.w,
padding: EdgeInsets.all(24.w),
decoration: BoxDecoration(
color: isDarkMode ? Colors.grey[900] : Colors.white,
borderRadius: BorderRadius.circular(16.r),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
// 标题图标
Container(
width: 64.w,
height: 64.w,
decoration: BoxDecoration(
color: Colors.blue.withOpacity(0.1),
borderRadius: BorderRadius.circular(32.r),
),
child: Icon(
Icons.system_update,
size: 32.sp,
color: Colors.blue,
),
),
SizedBox(height: 16.h),
// 标题
Text(
'newVersionFound'.tr, // 发现新版本
style: TextStyle(
fontSize: 20.sp,
fontWeight: FontWeight.bold,
color: isDarkMode ? Colors.white : Colors.black87,
),
),
SizedBox(height: 8.h),
// 版本信息
Text(
'${updateInfo.currentVersion} → ${updateInfo.latestVersion}',
style: TextStyle(
fontSize: 14.sp,
color: isDarkMode ? Colors.grey[400] : Colors.grey[600],
),
),
SizedBox(height: 16.h),
// 更新描述
if (updateInfo.appInfo?.description.isNotEmpty == true) ...[
Container(
width: double.infinity,
padding: EdgeInsets.all(12.w),
decoration: BoxDecoration(
color: isDarkMode ? Colors.grey[800] : Colors.grey[100],
borderRadius: BorderRadius.circular(8.r),
),
child: Text(
updateInfo.appInfo!.description,
style: TextStyle(
fontSize: 14.sp,
color: isDarkMode ? Colors.grey[300] : Colors.grey[700],
height: 1.4,
),
),
),
SizedBox(height: 20.h),
],
// 按钮区域
Row(
children: [
// 取消按钮(仅在非强制更新时显示)
if (updateInfo.updateType != UpdateType.force) ...[
Expanded(
child: TextButton(
onPressed: () {
Get.back();
onCancel?.call();
},
style: TextButton.styleFrom(
padding: EdgeInsets.symmetric(vertical: 12.h),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.r),
side: BorderSide(
color: isDarkMode
? Colors.grey[600]!
: Colors.grey[300]!,
),
),
),
child: Text(
'remindLater'.tr, // 稍后提醒
style: TextStyle(
fontSize: 16.sp,
color:
isDarkMode ? Colors.grey[400] : Colors.grey[600],
),
),
),
),
SizedBox(width: 12.w),
],
// 更新按钮
Expanded(
child: ElevatedButton(
onPressed: () async {
//Get.back();//点击更新之后,弹窗不关闭,增强强制更新的有效性
await _handleUpdate();
onUpdate?.call();
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
padding: EdgeInsets.symmetric(vertical: 12.h),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.r),
),
elevation: 0,
),
child: Text(
_getUpdateButtonText(),
style: TextStyle(
fontSize: 16.sp,
fontWeight: FontWeight.w600,
),
),
),
),
],
),
],
),
),
);
}
/// 获取更新按钮文本
String _getUpdateButtonText() {
switch (updateInfo.updateType) {
case UpdateType.force:
return 'updateNow'.tr; // 立即更新
case UpdateType.optional:
return 'updateNow'.tr; // 立即更新
default:
return 'checkUpdate'.tr; // 检查更新
}
}
/// 处理更新操作
Future<void> _handleUpdate() async {
try {
// 显示加载提示
Get.dialog(
const Center(
child: CircularProgressIndicator(),
),
barrierDismissible: false,
);
final versionService = VersionUpdateService.to;
final success = await versionService.performUpdate(updateInfo);
// 关闭加载提示
Get.back();
if (!success) {
Get.snackbar(
'updateFailed'.tr, // 更新失败
'cannotOpenAppStore'.tr, // 无法打开应用商店,请手动前往应用商店更新
snackPosition: SnackPosition.TOP,
backgroundColor: Colors.red.withOpacity(0.8),
colorText: Colors.white,
);
}
} catch (e) {
// 关闭加载提示
if (Get.isDialogOpen == true) {
Get.back();
}
Get.snackbar(
'updateFailed'.tr, // 更新失败
'updateErrorRetry'.tr, // 更新过程中发生错误,请稍后重试
snackPosition: SnackPosition.TOP,
backgroundColor: Colors.red.withOpacity(0.8),
colorText: Colors.white,
);
}
}
/// 显示版本更新对话框
static Future<void> show(VersionUpdateInfo updateInfo) async {
await Get.dialog(
VersionUpdateDialog(updateInfo: updateInfo),
// 关键修改:禁用外部点击关闭
barrierDismissible: false,
// 防止返回键关闭(在强制更新模式下)
//useRootNavigator: true,
);
}
}