d8c0bd74d2
- 新增 lib/core/update/:app_updater(入口+进度对话框) + io/web 条件实现
- Windows: Dio 下载 setup.exe(无 MOTW) -> 启动安装包 -> 退出,规避浏览器
SmartScreen 下载页与运行时拦截
- macOS: 下载 zip -> ditto 解压 -> 后台脚本等退出后替换 /Applications 内
app 并重启(权限失败回退访达高亮)
- Web: 刷新页面;其它平台回退浏览器下载
- app_shell 更新 banner/强制更新弹窗、设置页「立即更新」改走应用内更新
- 设置页「检查更新」加反馈(正在检查/已是最新/发现新版本/失败)
- jiu-installer.iss 加 CloseApplications=yes 便于覆盖
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
86 lines
2.7 KiB
Dart
86 lines
2.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
import '../../providers/update_provider.dart';
|
|
import 'app_updater_io.dart'
|
|
if (dart.library.js_interop) 'app_updater_web.dart' as impl;
|
|
|
|
/// 应用内更新统一入口。
|
|
/// - Web:刷新页面。
|
|
/// - Windows/macOS:应用内下载安装包并自动安装/重启(带进度对话框)。
|
|
/// - 其它平台 / 无对应下载地址:回退浏览器下载(launchUpdateUrl)。
|
|
Future<void> startInAppUpdate(BuildContext context, AppUpdateInfo info) async {
|
|
if (impl.isWeb) {
|
|
impl.reloadForUpdate();
|
|
return;
|
|
}
|
|
|
|
final url = impl.platformDownloadUrl(info.downloadUrls);
|
|
if (!impl.isInAppSupported || url == null || url.isEmpty) {
|
|
await launchUpdateUrl(info.downloadUrls); // 兜底:开浏览器/商店
|
|
return;
|
|
}
|
|
|
|
await _runWithProgress(context, info, url);
|
|
}
|
|
|
|
Future<void> _runWithProgress(
|
|
BuildContext context, AppUpdateInfo info, String url) async {
|
|
final progress = ValueNotifier<double>(0);
|
|
|
|
// 不可关闭的进度对话框;下载完成后 impl 会让进程退出。
|
|
showDialog<void>(
|
|
context: context,
|
|
barrierDismissible: false,
|
|
builder: (_) => PopScope(
|
|
canPop: false,
|
|
child: AlertDialog(
|
|
title: Text('正在更新到 v${info.latestVersion}'),
|
|
content: ValueListenableBuilder<double>(
|
|
valueListenable: progress,
|
|
builder: (_, p, __) => Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
LinearProgressIndicator(value: p > 0 ? p : null),
|
|
const SizedBox(height: 12),
|
|
Text(p >= 1
|
|
? '下载完成,正在安装,应用即将重启…'
|
|
: '正在下载… ${(p * 100).toStringAsFixed(0)}%'),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
|
|
try {
|
|
await impl.downloadAndInstall(url, (p) => progress.value = p);
|
|
// 桌面端到此通常已 exit(0),不会继续执行。
|
|
} catch (e) {
|
|
if (!context.mounted) return;
|
|
Navigator.of(context, rootNavigator: true).pop(); // 关进度对话框
|
|
if (!context.mounted) return;
|
|
showDialog<void>(
|
|
context: context,
|
|
builder: (ctx) => AlertDialog(
|
|
title: const Text('更新失败'),
|
|
content: Text('$e\n\n可改用浏览器下载更新。'),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(ctx),
|
|
child: const Text('取消'),
|
|
),
|
|
ElevatedButton(
|
|
onPressed: () {
|
|
Navigator.pop(ctx);
|
|
launchUpdateUrl(info.downloadUrls);
|
|
},
|
|
child: const Text('浏览器下载'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
} finally {
|
|
progress.dispose();
|
|
}
|
|
}
|