From af52743e94c83d843c46bb846264b0df168e4e7e Mon Sep 17 00:00:00 2001 From: "DESKTOP-I3JPKHK\\wy" <1111> Date: Thu, 11 Dec 2025 17:05:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E8=BE=85=E5=8A=A9=E5=87=BD?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/upgrade_auxiliary_utils.dart | 80 ++++++++++++++++++++++++++++++++ lib/yx_app_upgrade_flutter.dart | 1 + 2 files changed, 81 insertions(+) create mode 100644 lib/upgrade_auxiliary_utils.dart diff --git a/lib/upgrade_auxiliary_utils.dart b/lib/upgrade_auxiliary_utils.dart new file mode 100644 index 0000000..b40f522 --- /dev/null +++ b/lib/upgrade_auxiliary_utils.dart @@ -0,0 +1,80 @@ +import 'dart:io'; + +import 'package:flutter/foundation.dart'; +import 'package:flutter/widgets.dart'; +import 'package:yx_app_upgrade_flutter/app_upgrade_simple.dart'; +import 'package:yx_app_upgrade_flutter/models/app_upgrade_version.dart'; + +/// App 升级服务 +class UpgradeAuxiliaryUtils { + /// 设备类型 1:安卓, 2:IOS + late final int? deviceType; + + /// 是否已经点击稍后更新按钮 + bool _updateLater = false; + + /// 版本检查是否已完成 + bool completed = false; + + /// 上次检查的版本信息 + AppUpgradeVersion? _lastVersion; + + UpgradeAuxiliaryUtils._() { + if (Platform.isAndroid) { + deviceType = 1; + } else if (Platform.isIOS) { + deviceType = 2; + } else { + deviceType = null; + } + } + + static final UpgradeAuxiliaryUtils _instance = UpgradeAuxiliaryUtils._(); + + /// 获取 UpgradeService 单例 + static UpgradeAuxiliaryUtils get instance => _instance; + + /// 启动版本检查 + Future initiateVersionCheck( + BuildContext context, { + required Future Function(int upType) future, + UpgradeConfig? config, + bool? showNoUpdateToast, + bool? autoInstall, + VoidCallback? onComplete, + VoidCallback? onUpdateLater, + }) async { + if (deviceType == null || completed) return; + completed = true; + await AppUpgradeSimple.instance.checkUpdate( + context: context, + future: () async { + final result = await future(deviceType!); + + // 如果用户选择了"稍后更新",且版本未发生变化,则跳过本次检查 + if (_updateLater && + _lastVersion != null && + result != null && + result.versionName == _lastVersion!.versionName && + result.versionBuildNumber == _lastVersion!.versionBuildNumber) { + return null; + } + + return _lastVersion = result; + }, + showNoUpdateToast: showNoUpdateToast, + autoInstall: autoInstall, + onComplete: () { + // 版本检查完成 + debugPrint("更新插件执行完成....:"); + completed = false; + onComplete?.call(); + }, + onUpdateLater: () { + _updateLater = true; + onUpdateLater?.call(); + }, + config: config ?? (kDebugMode ? UpgradeConfig.development : UpgradeConfig.production), + ); + } +} diff --git a/lib/yx_app_upgrade_flutter.dart b/lib/yx_app_upgrade_flutter.dart index f9ce14a..75a9629 100644 --- a/lib/yx_app_upgrade_flutter.dart +++ b/lib/yx_app_upgrade_flutter.dart @@ -2,3 +2,4 @@ library; export 'package:yx_app_upgrade_flutter/app_upgrade_simple.dart'; export 'package:yx_app_upgrade_flutter/models/index.dart'; +export 'package:yx_app_upgrade_flutter/upgrade_auxiliary_utils.dart';