Browse Source

feat(ota_check_service): 添加暗黑模式支持并改进对话框样式

为OTA检查服务的升级对话框添加暗黑模式适配,改进文本显示和样式配置。包括:
- 根据当前主题模式动态调整颜色
- 优化标题文本布局防止溢出
- 增强按钮边框和背景色适配
weicu
lxm 1 year ago
parent
commit
1001c83d02
  1. 89
      lib/data/services/ota_check_service.dart

89
lib/data/services/ota_check_service.dart

@ -9,49 +9,50 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
/// 负责管理全局OTA检查状态,确保在应用生命周期内只检查一次 /// 负责管理全局OTA检查状态,确保在应用生命周期内只检查一次
class OtaCheckService extends GetxService { class OtaCheckService extends GetxService {
static const String _tag = 'OtaCheckService'; static const String _tag = 'OtaCheckService';
// 全局状态管理 // 全局状态管理
static bool _hasCheckedInCurrentSession = false; static bool _hasCheckedInCurrentSession = false;
static bool _isCheckingInProgress = false; static bool _isCheckingInProgress = false;
final _otaCoreService = OtaCoreService(); final _otaCoreService = OtaCoreService();
/// 获取服务实例 /// 获取服务实例
static OtaCheckService get to => Get.find<OtaCheckService>(); static OtaCheckService get to => Get.find<OtaCheckService>();
@override @override
void onInit() { void onInit() {
super.onInit(); super.onInit();
Logger.d(_tag, 'OTA检查服务已初始化'); Logger.d(_tag, 'OTA检查服务已初始化');
} }
/// 重置检查状态(仅在应用重启时调用) /// 重置检查状态(仅在应用重启时调用)
static void resetCheckStatus() { static void resetCheckStatus() {
_hasCheckedInCurrentSession = false; _hasCheckedInCurrentSession = false;
_isCheckingInProgress = false; _isCheckingInProgress = false;
Logger.d(_tag, 'OTA检查状态已重置'); Logger.d(_tag, 'OTA检查状态已重置');
} }
/// 检查是否应该进行OTA检查 /// 检查是否应该进行OTA检查
bool shouldCheckOta() { bool shouldCheckOta() {
return !_hasCheckedInCurrentSession && !_isCheckingInProgress; return !_hasCheckedInCurrentSession && !_isCheckingInProgress;
} }
/// 标记为已检查 /// 标记为已检查
void _markAsChecked() { void _markAsChecked() {
_hasCheckedInCurrentSession = true; _hasCheckedInCurrentSession = true;
_isCheckingInProgress = false; _isCheckingInProgress = false;
Logger.d(_tag, 'OTA检查状态已标记为完成'); Logger.d(_tag, 'OTA检查状态已标记为完成');
} }
/// 检查当前是否在Home页面 /// 检查当前是否在Home页面
bool _isCurrentlyOnHomePage() { bool _isCurrentlyOnHomePage() {
final currentRoute = Get.currentRoute; final currentRoute = Get.currentRoute;
final isOnHome = currentRoute == Routes.home || currentRoute == Routes.initial; final isOnHome =
currentRoute == Routes.home || currentRoute == Routes.initial;
Logger.d(_tag, '当前路由: $currentRoute, 是否在Home页面: $isOnHome'); Logger.d(_tag, '当前路由: $currentRoute, 是否在Home页面: $isOnHome');
return isOnHome; return isOnHome;
} }
/// 执行OTA升级检查 /// 执行OTA升级检查
/// [isFullyConnected] 设备是否完全连接 /// [isFullyConnected] 设备是否完全连接
/// 返回是否成功执行了检查 /// 返回是否成功执行了检查
@ -62,53 +63,54 @@ class OtaCheckService extends GetxService {
Logger.d(_tag, '跳过OTA检查 - 已检查过或正在检查中'); Logger.d(_tag, '跳过OTA检查 - 已检查过或正在检查中');
return false; return false;
} }
// 检查设备连接状态 // 检查设备连接状态
if (!isFullyConnected) { if (!isFullyConnected) {
Logger.d(_tag, '设备未完全连接,跳过OTA检查'); Logger.d(_tag, '设备未完全连接,跳过OTA检查');
return false; return false;
} }
// 检查当前页面 // 检查当前页面
if (!_isCurrentlyOnHomePage()) { if (!_isCurrentlyOnHomePage()) {
Logger.d(_tag, '当前不在Home页面,跳过OTA检查'); Logger.d(_tag, '当前不在Home页面,跳过OTA检查');
return false; return false;
} }
// 标记为检查中 // 标记为检查中
_isCheckingInProgress = true; _isCheckingInProgress = true;
Logger.d(_tag, '开始执行OTA固件升级检查...'); Logger.d(_tag, '开始执行OTA固件升级检查...');
// 获取当前版本信息 // 获取当前版本信息
final currentInfo = await _otaCoreService.getCurrentVersionInfo(); final currentInfo = await _otaCoreService.getCurrentVersionInfo();
final currentVersion = currentInfo['currentVersion'] as String; final currentVersion = currentInfo['currentVersion'] as String;
// 获取最新版本信息 // 获取最新版本信息
final latestInfo = await _otaCoreService.getLatestVersionInfo(); final latestInfo = await _otaCoreService.getLatestVersionInfo();
final latestVersion = latestInfo['latestVersion'] as String; final latestVersion = latestInfo['latestVersion'] as String;
Logger.d(_tag, '当前版本: $currentVersion, 最新版本: $latestVersion'); Logger.d(_tag, '当前版本: $currentVersion, 最新版本: $latestVersion');
// 检查是否有更新 // 检查是否有更新
final hasUpdate = _otaCoreService.isNewVersionAvailable(currentVersion, latestVersion); final hasUpdate =
_otaCoreService.isNewVersionAvailable(currentVersion, latestVersion);
if (hasUpdate) { if (hasUpdate) {
Logger.d(_tag, '发现新版本,准备显示升级提醒'); Logger.d(_tag, '发现新版本,准备显示升级提醒');
// 再次确认当前在Home页面(防止异步执行期间页面切换) // 再次确认当前在Home页面(防止异步执行期间页面切换)
if (_isCurrentlyOnHomePage()) { if (_isCurrentlyOnHomePage()) {
_showOtaUpgradeDialog(latestVersion, latestInfo['updateDescription'] as String?); _showOtaUpgradeDialog(
latestVersion, latestInfo['updateDescription'] as String?);
} else { } else {
Logger.d(_tag, '检查完成时已不在Home页面,取消显示弹窗'); Logger.d(_tag, '检查完成时已不在Home页面,取消显示弹窗');
} }
} else { } else {
Logger.d(_tag, '当前已是最新版本'); Logger.d(_tag, '当前已是最新版本');
} }
// 标记为已检查(无论是否有更新都标记为已检查) // 标记为已检查(无论是否有更新都标记为已检查)
_markAsChecked(); _markAsChecked();
return true; return true;
} catch (e) { } catch (e) {
Logger.e(_tag, '检查OTA升级失败: $e'); Logger.e(_tag, '检查OTA升级失败: $e');
// 即使检查失败,也标记为已检查,避免重复尝试 // 即使检查失败,也标记为已检查,避免重复尝试
@ -116,7 +118,7 @@ class OtaCheckService extends GetxService {
return false; return false;
} }
} }
/// 显示OTA升级提醒对话框 /// 显示OTA升级提醒对话框
/// [latestVersion] 最新版本号 /// [latestVersion] 最新版本号
/// [updateDescription] 更新描述 /// [updateDescription] 更新描述
@ -126,24 +128,32 @@ class OtaCheckService extends GetxService {
Logger.d(_tag, '显示弹窗时发现不在Home页面,取消显示'); Logger.d(_tag, '显示弹窗时发现不在Home页面,取消显示');
return; return;
} }
final isDarkMode = Get.isDarkMode;
Logger.d(_tag, '显示OTA升级提醒对话框'); Logger.d(_tag, '显示OTA升级提醒对话框');
Get.dialog( Get.dialog(
AlertDialog( AlertDialog(
title: Row( title: Row(
children: [ children: [
Icon( Icon(
Icons.headset, Icons.headset,
color: Colors.blue[600], color: isDarkMode ? const Color(0xFF0066CC) : Colors.blue[600],
size: 24.sp, size: 24.sp,
), ),
SizedBox(width: 8.w), SizedBox(width: 8.w),
Text( // 使用Flexible确保标题文本能够自适应不同语言长度,避免溢出
'ota_newVersionFound'.tr, Flexible(
style: TextStyle( child: Text(
color: Colors.blue[700], 'ota_newVersionFound'.tr, // 发现新版本
fontWeight: FontWeight.w600, style: TextStyle(
color: isDarkMode ? Colors.white : Colors.blue[700],
fontWeight: FontWeight.w600,
fontSize: 20.sp, // 设置合适的字体大小
),
softWrap: true, // 启用软换行
overflow: TextOverflow.visible, // 确保文本完全显示
maxLines: 2, // 最多显示两行
), ),
), ),
], ],
@ -155,6 +165,7 @@ class OtaCheckService extends GetxService {
Text( Text(
'${'latestFirmwareVersion'.tr}: $latestVersion', '${'latestFirmwareVersion'.tr}: $latestVersion',
style: TextStyle( style: TextStyle(
color: isDarkMode ? Colors.grey[400] : Colors.grey[600],
fontWeight: FontWeight.w500, fontWeight: FontWeight.w500,
fontSize: 16.sp, fontSize: 16.sp,
), ),
@ -173,7 +184,9 @@ class OtaCheckService extends GetxService {
}, },
child: Text( child: Text(
'talkLater'.tr, 'talkLater'.tr,
style: TextStyle(color: Colors.grey[600]), style: TextStyle(
color: isDarkMode ? Colors.grey[400] : Colors.grey[600],
),
), ),
), ),
// 立即升级 (靠右) // 立即升级 (靠右)
@ -184,11 +197,15 @@ class OtaCheckService extends GetxService {
Get.toNamed(Routes.OTA_UPGRADE); Get.toNamed(Routes.OTA_UPGRADE);
}, },
style: ElevatedButton.styleFrom( style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue[600], backgroundColor:
isDarkMode ? const Color(0xFF0066CC) : Colors.blue[600],
foregroundColor: Colors.white, foregroundColor: Colors.white,
shape: RoundedRectangleBorder( shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.sp), borderRadius: BorderRadius.circular(8.sp),
), ),
side: isDarkMode
? BorderSide(color: Colors.grey[800]!, width: 1)
: BorderSide.none,
), ),
child: Text( child: Text(
'ota_updateNow'.tr, 'ota_updateNow'.tr,
@ -201,12 +218,12 @@ class OtaCheckService extends GetxService {
shape: RoundedRectangleBorder( shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12.sp), borderRadius: BorderRadius.circular(12.sp),
), ),
backgroundColor: Colors.white, backgroundColor: isDarkMode ? const Color(0xFF1A1A1A) : Colors.white,
), ),
barrierDismissible: true, // 允许点击外部关闭 barrierDismissible: true, // 允许点击外部关闭
); );
} }
/// 获取当前检查状态(用于调试) /// 获取当前检查状态(用于调试)
Map<String, dynamic> getCheckStatus() { Map<String, dynamic> getCheckStatus() {
return { return {

Loading…
Cancel
Save