Files
flutterApp/lib/core/update/update_dialog.dart

142 lines
5.0 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'update_cubit.dart';
import 'update_state.dart';
import 'version_check_service.dart';
/// 更新对话框
class UpdateDialog extends StatelessWidget {
final AppVersionInfo versionInfo;
const UpdateDialog({super.key, required this.versionInfo});
@override
Widget build(BuildContext context) {
return BlocListener<UpdateCubit, UpdateState>(
listener: (context, state) {
if (state is UpdateSuccess) {
// 更新成功,关闭对话框
Navigator.of(context).pop();
if (versionInfo.updateType == 'patch') {
// 差量更新需要重启
_showRestartDialog(context);
}
// 整包更新会自动打开安装界面,不需要额外操作
} else if (state is UpdateFailure) {
Navigator.of(context).pop();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('更新失败: ${state.error}')),
);
}
},
child: AlertDialog(
title: const Text('发现新版本'),
content: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('版本号: ${versionInfo.version}'),
const SizedBox(height: 8),
Text('更新类型: ${versionInfo.updateType == "patch" ? "差量更新" : "整包更新"}'),
const SizedBox(height: 8),
if (versionInfo.forceUpdate)
Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.red.shade50,
borderRadius: BorderRadius.circular(4),
),
child: const Text(
'⚠️ 强制更新',
style: TextStyle(color: Colors.red),
),
),
const SizedBox(height: 12),
const Text('更新内容:', style: TextStyle(fontWeight: FontWeight.bold)),
const SizedBox(height: 4),
Text(versionInfo.updateDesc.isEmpty ? '优化用户体验,修复已知问题' : versionInfo.updateDesc),
],
),
),
actions: [
if (!versionInfo.forceUpdate)
TextButton(
onPressed: () {
context.read<UpdateCubit>().cancelUpdate();
Navigator.of(context).pop();
},
child: const Text('稍后'),
),
BlocBuilder<UpdateCubit, UpdateState>(
builder: (context, state) {
if (state is UpdateDownloading) {
return SizedBox(
width: 60,
height: 60,
child: Stack(
alignment: Alignment.center,
children: [
CircularProgressIndicator(
value: state.progress,
strokeWidth: 3,
),
Text('${(state.progress * 100).toInt()}%'),
],
),
);
} else if (state is UpdateInstalling) {
return const Text('安装中...');
} else {
return ElevatedButton(
onPressed: () {
if (versionInfo.updateType == 'patch' && versionInfo.patchUrl != null) {
// 差量更新
context.read<UpdateCubit>().applyPatch(
versionInfo.patchUrl!,
versionInfo.version,
versionInfo.versionCode,
md5: versionInfo.patchMd5,
);
} else if (versionInfo.apkUrl != null) {
// 整包更新
context.read<UpdateCubit>().downloadAndInstallApk(versionInfo.apkUrl!);
}
},
child: const Text('立即更新'),
);
}
},
),
],
),
);
}
void _showRestartDialog(BuildContext context) {
showDialog(
context: context,
barrierDismissible: false,
builder: (ctx) => AlertDialog(
title: const Text('更新完成'),
content: const Text('差量更新已应用,需要重启应用才能生效。\n是否立即重启?'),
actions: [
TextButton(
onPressed: () => Navigator.of(ctx).pop(),
child: const Text('稍后'),
),
ElevatedButton(
onPressed: () {
Navigator.of(ctx).pop();
// TODO: 实现重启逻辑
// 可以使用 restart_app 包或退出应用让用户手动打开
},
child: const Text('立即重启'),
),
],
),
);
}
}