From 262ce70998fa52dec3120589f51c47e96fb96df4 Mon Sep 17 00:00:00 2001
From: 905086 <k2049@qq.com>
Date: Tue, 03 Nov 2026 17:04:19 +0800
Subject: [PATCH] feat: 完成游戏盒子前后端基础功能开发

---
 fluttergamebox/lib/widgets/download_manager.dart |  390 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 390 insertions(+), 0 deletions(-)

diff --git a/fluttergamebox/lib/widgets/download_manager.dart b/fluttergamebox/lib/widgets/download_manager.dart
new file mode 100644
index 0000000..724970c
--- /dev/null
+++ b/fluttergamebox/lib/widgets/download_manager.dart
@@ -0,0 +1,390 @@
+import 'dart:io';
+
+import 'package:flutter/material.dart';
+import 'package:fluttergamebox/api/api_client.dart';
+import 'package:fluttergamebox/common/theme.dart';
+import 'package:fluttergamebox/store/app_state.dart';
+import 'package:fluttergamebox/utils/util.dart';
+import 'package:path/path.dart' as p;
+
+/// 下载管理弹窗(安装任务)
+void showDownloadDialog(BuildContext context) {
+  showDialog(
+    context: context,
+    barrierColor: Colors.black.withOpacity(0.6),
+    builder: (context) => const _DownloadDialog(),
+  );
+}
+
+class _DownloadDialog extends StatelessWidget {
+  const _DownloadDialog();
+  @override
+  Widget build(BuildContext context) {
+    return AnimatedBuilder(
+      animation: DownloadManager.instance,
+      builder: (context, _) {
+        final dm = DownloadManager.instance;
+        final task = dm.activeTask;
+        if (task == null) {
+          return Dialog(
+            backgroundColor: const Color(0xFF1E2130),
+            child: Container(
+              width: 560,
+              padding: const EdgeInsets.all(32),
+              child: const Column(
+                mainAxisSize: MainAxisSize.min,
+                children: [
+                  Icon(Icons.download_done, color: AppColors.textGray, size: 48),
+                  SizedBox(height: 16),
+                  Text('暂无下载任务',
+                      style: TextStyle(color: AppColors.textGray)),
+                ],
+              ),
+            ),
+          );
+        }
+        return Dialog(
+          backgroundColor: const Color(0xFF1E2130),
+          shape:
+              RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
+          child: Container(
+            width: 680,
+            padding: const EdgeInsets.all(24),
+            child: Column(
+              mainAxisSize: MainAxisSize.min,
+              crossAxisAlignment: CrossAxisAlignment.start,
+              children: [
+                // 标题
+                Row(
+                  children: [
+                    if (task.finished)
+                      const Icon(Icons.check_circle,
+                          color: AppColors.green, size: 20)
+                    else
+                      const Icon(Icons.download,
+                          color: AppColors.accent, size: 20),
+                    const SizedBox(width: 8),
+                    const Text('安装任务',
+                        style: TextStyle(
+                            color: Colors.white,
+                            fontSize: 15,
+                            fontWeight: FontWeight.bold)),
+                    const SizedBox(width: 10),
+                    const Expanded(
+                      child: Text('如长时间无速度,请取消安装保留文件尝试再次安装',
+                          style:
+                              TextStyle(color: AppColors.red, fontSize: 11)),
+                    ),
+                    const SizedBox(width: 10),
+                    // 自动关机
+                    _AutoShutdownToggle(task: task),
+                    const SizedBox(width: 12),
+                    GestureDetector(
+                      onTap: () => Navigator.pop(context),
+                      child: const Icon(Icons.close,
+                          color: AppColors.textGray, size: 18),
+                    ),
+                  ],
+                ),
+                const SizedBox(height: 16),
+                // 游戏名
+                Text(task.title,
+                    maxLines: 1,
+                    overflow: TextOverflow.ellipsis,
+                    style: const TextStyle(
+                        color: Colors.white,
+                        fontSize: 14,
+                        fontWeight: FontWeight.w500)),
+                const SizedBox(height: 18),
+                // 进度区
+                Row(
+                  children: [
+                    // 环形进度
+                    SizedBox(
+                      width: 86,
+                      height: 86,
+                      child: Stack(
+                        alignment: Alignment.center,
+                        children: [
+                          SizedBox(
+                            width: 86,
+                            height: 86,
+                            child: CircularProgressIndicator(
+                              value: task.finished ? 1 : task.progress,
+                              color: task.paused
+                                  ? AppColors.yellow
+                                  : AppColors.accent,
+                              backgroundColor: AppColors.bgLighter,
+                              strokeWidth: 5,
+                            ),
+                          ),
+                          Text(
+                            '${(task.progress * 100).toStringAsFixed(task.progress < 0.1 ? 2 : 0)}%',
+                            style: const TextStyle(
+                                color: Colors.white,
+                                fontSize: 15,
+                                fontWeight: FontWeight.bold),
+                          ),
+                        ],
+                      ),
+                    ),
+                    const SizedBox(width: 22),
+                    Expanded(
+                      child: Column(
+                        crossAxisAlignment: CrossAxisAlignment.start,
+                        children: [
+                          Row(
+                            children: [
+                              Text(
+                                task.finished
+                                    ? '下载完成'
+                                    : task.paused
+                                        ? '已暂停'
+                                        : '正在下载',
+                                style: TextStyle(
+                                    color: task.paused
+                                        ? AppColors.yellow
+                                        : task.finished
+                                            ? AppColors.green
+                                            : AppColors.accent,
+                                    fontSize: 14,
+                                    fontWeight: FontWeight.w500),
+                              ),
+                            ],
+                          ),
+                          const SizedBox(height: 8),
+                          Text(
+                            '${formatBytes(task.speed)}/s',
+                            style: const TextStyle(
+                                color: AppColors.textGray, fontSize: 13),
+                          ),
+                          const SizedBox(height: 4),
+                          Text(
+                            '${formatBytes(task.receivedBytes)} / ${formatBytes(task.totalBytes)}',
+                            style: const TextStyle(
+                                color: AppColors.textGray, fontSize: 13),
+                          ),
+                          const SizedBox(height: 4),
+                          Text(
+                            task.finished
+                                ? '已完成'
+                                : '剩余时间 ${formatDuration(task.etaSeconds)}',
+                            style: const TextStyle(
+                                color: AppColors.textGray, fontSize: 13),
+                          ),
+                        ],
+                      ),
+                    ),
+                  ],
+                ),
+                const SizedBox(height: 16),
+                // 网络信息条
+                Container(
+                  padding:
+                      const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
+                  decoration: BoxDecoration(
+                    color: AppColors.bg,
+                    borderRadius: BorderRadius.circular(4),
+                  ),
+                  child: Row(
+                    children: [
+                      const Text('网络运营商:联通',
+                          style: TextStyle(
+                              color: AppColors.textGray, fontSize: 12)),
+                      const Spacer(),
+                      const Text('点击切换线路:自动',
+                          style: TextStyle(
+                              color: AppColors.textGray, fontSize: 12)),
+                    ],
+                  ),
+                ),
+                const SizedBox(height: 10),
+                // 文件列表
+                Container(
+                  constraints: const BoxConstraints(maxHeight: 180),
+                  decoration: BoxDecoration(
+                    color: AppColors.bg,
+                    borderRadius: BorderRadius.circular(4),
+                  ),
+                  child: ListView.builder(
+                    shrinkWrap: true,
+                    itemCount: task.files.length,
+                    itemBuilder: (context, i) {
+                      final f = task.files[i];
+                      Color stColor = AppColors.textDim;
+                      String stLabel = '等待下载';
+                      switch (f.status) {
+                        case DownloadStatus.preparing:
+                          stColor = AppColors.yellow;
+                          stLabel = '正在准备';
+                        case DownloadStatus.downloading:
+                          stColor = AppColors.green;
+                          stLabel = '正在下载';
+                        case DownloadStatus.paused:
+                          stColor = AppColors.yellow;
+                          stLabel = '已暂停';
+                        case DownloadStatus.done:
+                          stColor = AppColors.green;
+                          stLabel = '已完成';
+                        case DownloadStatus.failed:
+                          stColor = AppColors.red;
+                          stLabel = '下载失败';
+                        default:
+                          stLabel = '等待下载';
+                      }
+                      return Padding(
+                        padding: const EdgeInsets.symmetric(
+                            horizontal: 12, vertical: 6),
+                        child: Row(
+                          children: [
+                            SizedBox(
+                              width: 200,
+                              child: Text(f.name,
+                                  maxLines: 1,
+                                  overflow: TextOverflow.ellipsis,
+                                  style: const TextStyle(
+                                      color: AppColors.textWhite,
+                                      fontSize: 12)),
+                            ),
+                            Expanded(
+                              child: LinearProgressIndicator(
+                                value: f.progress,
+                                backgroundColor: AppColors.bgLighter,
+                                color: AppColors.accent,
+                                minHeight: 3,
+                              ),
+                            ),
+                            const SizedBox(width: 10),
+                            SizedBox(
+                              width: 130,
+                              child: Text(
+                                  '${formatBytes(f.received)}/${formatBytes(f.totalBytes)}',
+                                  style: const TextStyle(
+                                      color: AppColors.textGray,
+                                      fontSize: 11)),
+                            ),
+                            SizedBox(
+                              width: 60,
+                              child: Text(stLabel,
+                                  style: TextStyle(
+                                      color: stColor, fontSize: 11)),
+                            ),
+                          ],
+                        ),
+                      );
+                    },
+                  ),
+                ),
+                const SizedBox(height: 18),
+                // 操作按钮
+                Row(
+                  children: [
+                    Expanded(
+                      child: ElevatedButton(
+                        onPressed: task.finished
+                            ? null
+                            : () {
+                                if (task.paused) {
+                                  DownloadManager.instance.resume();
+                                } else {
+                                  DownloadManager.instance.pause();
+                                }
+                              },
+                        style: ElevatedButton.styleFrom(
+                          backgroundColor: task.paused
+                              ? AppColors.accent
+                              : const Color(0xFF3A3F52),
+                          foregroundColor: Colors.white,
+                          disabledBackgroundColor:
+                              AppColors.green.withOpacity(0.3),
+                          padding: const EdgeInsets.symmetric(vertical: 12),
+                        ),
+                        child: Text(task.finished
+                            ? '下载完成'
+                            : task.paused
+                                ? '继续下载'
+                                : '暂停下载'),
+                      ),
+                    ),
+                    const SizedBox(width: 12),
+                    Expanded(
+                      child: ElevatedButton(
+                        onPressed: () {
+                          DownloadManager.instance.cancelTask();
+                          Navigator.pop(context);
+                        },
+                        style: ElevatedButton.styleFrom(
+                          backgroundColor: const Color(0xFF3A3F52),
+                          foregroundColor: Colors.white,
+                          padding: const EdgeInsets.symmetric(vertical: 12),
+                        ),
+                        child: const Text('取消安装'),
+                      ),
+                    ),
+                  ],
+                ),
+              ],
+            ),
+          ),
+        );
+      },
+    );
+  }
+}
+
+class _AutoShutdownToggle extends StatefulWidget {
+  final DownloadTask task;
+  const _AutoShutdownToggle({required this.task});
+  @override
+  State<_AutoShutdownToggle> createState() => _AutoShutdownToggleState();
+}
+
+class _AutoShutdownToggleState extends State<_AutoShutdownToggle> {
+  @override
+  Widget build(BuildContext context) {
+    return GestureDetector(
+      onTap: () => setState(
+          () => widget.task.autoShutdown = !widget.task.autoShutdown),
+      child: Row(
+        children: [
+          Text('自动关机',
+              style: const TextStyle(
+                  color: AppColors.textGray, fontSize: 11)),
+          const SizedBox(width: 6),
+          Container(
+            width: 28,
+            height: 15,
+            decoration: BoxDecoration(
+              color: widget.task.autoShutdown
+                  ? AppColors.accent
+                  : AppColors.bgLighter,
+              borderRadius: BorderRadius.circular(8),
+            ),
+            child: AnimatedAlign(
+              duration: const Duration(milliseconds: 150),
+              alignment: widget.task.autoShutdown
+                  ? Alignment.centerRight
+                  : Alignment.centerLeft,
+              child: Container(
+                width: 11,
+                height: 11,
+                margin: const EdgeInsets.all(2),
+                decoration:
+                    const BoxDecoration(color: Colors.white, shape: BoxShape.circle),
+              ),
+            ),
+          ),
+        ],
+      ),
+    );
+  }
+}
+
+/// 打开下载/安装目录
+Future<void> openDirectory(String path) async {
+  final dir = Directory(path);
+  if (!await dir.exists()) {
+    await dir.create(recursive: true);
+  }
+  Process.run('explorer.exe', [p.normalize(dir.path)]);
+}

--
Gitblit v1.10.0