Browse Source

feat(ota升级): 添加详细状态提示功能

在OTA升级过程中添加详细状态提示界面,显示当前操作进度和状态信息。包括下载准备、文件检查、下载进度、升级进度等各个阶段的详细状态,提升用户对升级过程的感知。

新增`showDetailedStatus`和`detailedStatus`控制变量,在控制器各阶段更新状态信息,并在视图中添加对应的状态显示组件。状态提示包含进度百分比和操作说明,支持暗黑模式适配。
weicu
lxm 1 year ago
parent
commit
857946e600
  1. 47
      lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart
  2. 88
      lib/modules/ota_upgrade/views/ota_upgrade_view.dart

47
lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart

@ -63,6 +63,10 @@ class OtaUpgradeController extends GetxController {
final RxDouble progress = 0.0.obs; // 控制器自己的可观察属性
final RxInt type = OtaEvent.NOT_STARTED.obs;
final RxBool isUpgrade = false.obs;
// 新增:详细状态提示
final RxString detailedStatus = ''.obs;
final RxBool showDetailedStatus = false.obs;
@override
void onInit() {
@ -289,6 +293,9 @@ class OtaUpgradeController extends GetxController {
// 修改后的startDownload方法
Future<void> startDownload() async {
isDownloading.value = true;
showDetailedStatus.value = true;
detailedStatus.value = '准备下载固件包...';
int lastIndex = updatepackageaddress.lastIndexOf('/');
String fileName = updatepackageaddress.substring(lastIndex + 1);
String directoryName = "ota_upgrades";
@ -302,6 +309,7 @@ class OtaUpgradeController extends GetxController {
type.value = DOWNLOADING_UP_FILES;
try {
detailedStatus.value = '正在获取下载目录...';
final Directory? downloadsDir = await getApplicationSupportDirectory();
if (downloadsDir == null) {
throw FileSystemException('无法访问下载目录');
@ -310,12 +318,14 @@ class OtaUpgradeController extends GetxController {
final targetDir =
Directory('${downloadsDir.path}/Download/$directoryName');
if (!await targetDir.exists()) {
detailedStatus.value = '创建下载目录...';
await targetDir.create(recursive: true);
}
final savePath = '${targetDir.path}/$fileName';
// 新增:检查同名文件逻辑
detailedStatus.value = '检查本地文件...';
bool existingSameFile = false;
try {
var files = await targetDir.list().toList();
@ -336,12 +346,13 @@ class OtaUpgradeController extends GetxController {
if (existingSameFile) {
//Get.snackbar('文件已存在', '跳过下载,直接更新');
detailedStatus.value = '文件已存在,准备升级...';
startUpgrading(savePath);
return;
}
// 新增:删除其他文件逻辑
detailedStatus.value = '清理旧文件...';
try {
var files = await targetDir.list().toList();
for (var entity in files) {
@ -357,6 +368,7 @@ class OtaUpgradeController extends GetxController {
print('文件保存路径: $savePath');
detailedStatus.value = '开始下载固件包...';
final options = Options(
receiveTimeout: Duration.zero,
followRedirects: true,
@ -370,14 +382,19 @@ class OtaUpgradeController extends GetxController {
onReceiveProgress: (received, total) {
if (total != -1) {
downloadProgress.value = received / total;
detailedStatus.value = '正在下载固件包... ${(received / total * 100).toStringAsFixed(1)}%';
}
},
);
File file = File(savePath);
if (await file.exists()) {
// Get.snackbar('下载完成', '文件已保存至: $savePath');
detailedStatus.value = '下载完成,准备升级...';
} else {
Get.snackbar('错误', '文件未找到,请检查写入权限');
detailedStatus.value = '下载失败';
showDetailedStatus.value = false;
return;
}
isDownloading.value = false;
startUpgrading(savePath);
@ -387,12 +404,19 @@ class OtaUpgradeController extends GetxController {
} on FileSystemException catch (e) {
Get.snackbar('存储失败', '无法写入文件: ${e.message}');
} catch (e) {
Get.snackbar('未知错误', e.toString());
isDownloading.value = false;
detailedStatus.value = '下载失败,请检查网络连接';
showDetailedStatus.value = false;
Get.snackbar('下载失败', '请检查网络连接: $e');
print('下载失败: $e');
}
}
// 开始升级
void startUpgrading(String path) async {
detailedStatus.value = '准备设备连接...';
await Future.delayed(const Duration(milliseconds: 500));
detailedStatus.value = '正在连接设备...';
jlota.startUpgrading(address: _address, filePath: path);
return;
// isUpgrade.value = false;
@ -467,29 +491,38 @@ class OtaUpgradeController extends GetxController {
progress.value = event.progress!;
type.value = event.type!;
isUpgrade.value = true; // 开始升级
detailedStatus.value = '升级进度: ${(event.progress! * 100).toStringAsFixed(1)}%';
}
if (event.event == 'onStartOTA') {
type.value = OtaEvent.START_UPGRADING;
type.value = START_UPGRADING;
isUpgrade.value = true; // 开始升级
detailedStatus.value = 'OTA升级已开始...';
}
if (event.event == 'onStopOTA') {
type.value = OtaEvent.UPGRADE_SUCCESS;
type.value = UPGRADE_SUCCESS;
isUpgrade.value = false;
detailedStatus.value = '升级成功完成';
showDetailedStatus.value = false;
Get.snackbar('升级成功', '请退出重新连接设备');
_getCurrentVersion();
}
if (event.event == 'onError') {
type.value = OtaEvent.UPGRADE_FAILED;
type.value = UPGRADE_FAILED;
isUpgrade.value = false;
detailedStatus.value = '升级失败,错误码:${event.code}';
showDetailedStatus.value = false;
Get.snackbar('升级失败', '请检查设备连接或升级包,错误码:${event.code}');
}
if (event.event == 'NOCONNECTION') {
type.value = OtaEvent.CONNECTION_DISCONNECTION;
type.value = CONNECTION_DISCONNECTION;
isUpgrade.value = false;
detailedStatus.value = '设备连接断开';
showDetailedStatus.value = false;
}
if (event.event == 'CONNECTION_SUCCESS') {
type.value = OtaEvent.CONNECTION_SUCCESS;
type.value = CONNECTION_SUCCESS;
isUpgrade.value = true; // 开始升级
detailedStatus.value = '设备连接成功';
}
});
}

88
lib/modules/ota_upgrade/views/ota_upgrade_view.dart

@ -787,6 +787,11 @@ class OtaUpgradeView extends GetView<OtaUpgradeController> {
return _buildProgressIndicator(isDarkMode);
}
// 显示详细状态提示
if (controller.showDetailedStatus.value) {
return _buildDetailedStatusIndicator(isDarkMode);
}
return SizedBox(
width: double.infinity,
child: Column(
@ -892,6 +897,89 @@ class OtaUpgradeView extends GetView<OtaUpgradeController> {
});
}
// 详细状态指示器
Widget _buildDetailedStatusIndicator(bool isDarkMode) {
return Obx(() {
return Container(
width: double.infinity,
padding: EdgeInsets.all(24.w),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: isDarkMode
? [
const Color(0xFF1A1A1A).withValues(alpha: 0.9),
const Color(0xFF2A2A2A).withValues(alpha: 0.9),
]
: [
const Color(0xFFF8FAFF),
const Color(0xFFE3F2FD),
],
),
borderRadius: BorderRadius.circular(20.r),
boxShadow: [
BoxShadow(
color: isDarkMode
? Colors.black.withValues(alpha: 0.3)
: Colors.black.withValues(alpha: 0.08),
blurRadius: 20,
offset: const Offset(0, 8),
),
],
),
child: Column(
children: [
// 状态图标和文字
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
padding: EdgeInsets.all(8.w),
decoration: BoxDecoration(
color: Colors.blue.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(8.r),
),
child: SizedBox(
width: 20.sp,
height: 20.sp,
child: CircularProgressIndicator(
strokeWidth: 2,
valueColor: AlwaysStoppedAnimation<Color>(
isDarkMode ? const Color(0xFF0066CC) : Colors.blue,
),
),
),
),
SizedBox(width: 12.w),
Expanded(
child: Text(
controller.detailedStatus.value,
style: TextStyle(
fontSize: 16.sp,
fontWeight: FontWeight.w500,
color: isDarkMode ? Colors.blue[300] : Colors.blue[700],
),
),
),
],
),
SizedBox(height: 16.h),
// 提示信息
Text(
'请保持设备连接,不要关闭应用',
style: TextStyle(
fontSize: 12.sp,
color: isDarkMode ? Colors.grey[400] : Colors.grey[600],
),
),
],
),
);
});
}
// 进度指示器
Widget _buildProgressIndicator(bool isDarkMode) {
return Obx(() {

Loading…
Cancel
Save