From 0b4478cf6c0eb509c95a9bec2cc7bda37b259e10 Mon Sep 17 00:00:00 2001
From: 905086 <k2049@qq.com>
Date: Sat, 07 Nov 2026 19:06:06 +0800
Subject: [PATCH] feat: 新增工具管理功能及工具下载安装流程

---
 fluttergamebox/lib/pages/tools_page.dart |  166 ++++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 115 insertions(+), 51 deletions(-)

diff --git a/fluttergamebox/lib/pages/tools_page.dart b/fluttergamebox/lib/pages/tools_page.dart
index 81d01d4..dcc8e5e 100644
--- a/fluttergamebox/lib/pages/tools_page.dart
+++ b/fluttergamebox/lib/pages/tools_page.dart
@@ -1,4 +1,4 @@
-import 'package:flutter/material.dart';
+import 'package:flutter/material.dart';
 import 'package:fluttergamebox/api/api_client.dart';
 import 'package:fluttergamebox/api/api_service.dart';
 import 'package:fluttergamebox/common/theme.dart';
@@ -15,7 +15,6 @@
 class _ToolsPageState extends State<ToolsPage>
     with AutomaticKeepAliveClientMixin {
   List<Tool> tools = [];
-  final Set<int> _installed = {};
   bool _loading = true;
 
   @override
@@ -36,14 +35,7 @@
     });
   }
 
-  Future<void> _install(Tool tool) async {
-    if (_installed.contains(tool.id)) return;
-    // 模拟安装
-    setState(() => _installed.add(tool.id));
-    _toast('${tool.name} 安装完成');
-  }
-
-  void _toast(String msg) {
+  void _toast(String msg, {bool error = false}) {
     final overlay = Overlay.of(context);
     final entry = OverlayEntry(
       builder: (context) => Positioned(
@@ -56,12 +48,12 @@
             padding:
                 const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
             decoration: BoxDecoration(
-              color: AppColors.green,
+              color: error ? AppColors.red : AppColors.green,
               borderRadius: BorderRadius.circular(6),
             ),
             child: Row(
               children: [
-                const Icon(Icons.check_circle,
+                Icon(error ? Icons.error_outline : Icons.check_circle,
                     color: Colors.white, size: 18),
                 const SizedBox(width: 8),
                 Expanded(
@@ -100,8 +92,8 @@
                       fontWeight: FontWeight.w500)),
               const SizedBox(width: 12),
               const Text('游戏必备运行库和实用工具',
-                  style: TextStyle(
-                      color: AppColors.textGray, fontSize: 13)),
+                  style:
+                      TextStyle(color: AppColors.textGray, fontSize: 13)),
             ],
           ),
           const SizedBox(height: 16),
@@ -126,7 +118,7 @@
                         ),
                         itemCount: tools.length,
                         itemBuilder: (context, i) =>
-                            _ToolCard(tool: tools[i], installed: _installed.contains(tools[i].id), onInstall: () => _install(tools[i])),
+                            _ToolCard(tool: tools[i], onError: _toast),
                       ),
           ),
         ],
@@ -135,15 +127,50 @@
   }
 }
 
-class _ToolCard extends StatelessWidget {
+class _ToolCard extends StatefulWidget {
   final Tool tool;
-  final bool installed;
-  final VoidCallback onInstall;
-  const _ToolCard({
-    required this.tool,
-    required this.installed,
-    required this.onInstall,
-  });
+  final void Function(String msg, {bool error}) onError;
+  const _ToolCard({required this.tool, required this.onError});
+  @override
+  State<_ToolCard> createState() => _ToolCardState();
+}
+
+class _ToolCardState extends State<_ToolCard> {
+  bool _installed = false;
+
+  @override
+  void initState() {
+    super.initState();
+    _checkInstalled();
+  }
+
+  Future<void> _checkInstalled() async {
+    final ok = await isGameInstalled(widget.tool.name, widget.tool.launchExe);
+    if (mounted) setState(() => _installed = ok);
+  }
+
+  /// 启动已安装的工具
+  Future<void> _launch() async {
+    final ok =
+        await launchInstalledGame(widget.tool.name, widget.tool.launchExe);
+    if (!ok) widget.onError('未找到工具文件,请重新下载安装', error: true);
+  }
+
+  /// 安装:选择下载路径后下载并解压(与游戏流程一致)
+  Future<void> _install() async {
+    final ok = await showDialog<bool>(
+      context: context,
+      builder: (context) =>
+          DownloadPathDialog(gameTitle: widget.tool.name),
+    );
+    if (ok != true) return;
+    try {
+      await DownloadManager.instance.startToolDownload(widget.tool);
+      if (mounted) showDownloadDialog(context);
+    } catch (e) {
+      widget.onError(e.toString(), error: true);
+    }
+  }
 
   @override
   Widget build(BuildContext context) {
@@ -163,13 +190,13 @@
                   borderRadius: const BorderRadius.vertical(
                       top: Radius.circular(6)),
                   child: Image.network(
-                    ApiClient.baseUrl + tool.cover,
+                    ApiClient.baseUrl + widget.tool.cover,
                     fit: BoxFit.cover,
                     errorBuilder: (_, __, ___) => Container(
                       color: const Color(0xFF2A2E45),
                       child: Center(
                         child: Text(
-                          tool.name.characters.first,
+                          widget.tool.name.characters.first,
                           style: const TextStyle(
                               color: AppColors.textDim, fontSize: 28),
                         ),
@@ -202,37 +229,74 @@
             padding: const EdgeInsets.all(10),
             child: Column(
               children: [
-                Text(tool.description,
+                // 工具名称
+                Text(widget.tool.name,
                     maxLines: 1,
                     overflow: TextOverflow.ellipsis,
                     style: const TextStyle(
-                        color: Colors.white, fontSize: 12)),
+                        color: Colors.white,
+                        fontSize: 12,
+                        fontWeight: FontWeight.w500)),
+                if (widget.tool.description.isNotEmpty) ...[
+                  const SizedBox(height: 4),
+                  // 工具描述
+                  Text(widget.tool.description,
+                      maxLines: 1,
+                      overflow: TextOverflow.ellipsis,
+                      style: const TextStyle(
+                          color: AppColors.textDim, fontSize: 11)),
+                ],
                 const SizedBox(height: 8),
-                Row(
-                  children: [
-                    Text(tool.size,
-                        style: const TextStyle(
-                            color: AppColors.textDim, fontSize: 11)),
-                    const Spacer(),
-                    GestureDetector(
-                      onTap: onInstall,
-                      child: Container(
-                        padding: const EdgeInsets.symmetric(
-                            horizontal: 14, vertical: 4),
-                        decoration: BoxDecoration(
-                          color: installed
-                              ? const Color(0xFF3A3F52)
-                              : AppColors.accent,
-                          borderRadius: BorderRadius.circular(3),
+                // 按钮状态随下载管理器任务实时变化
+                ListenableBuilder(
+                  listenable: DownloadManager.instance,
+                  builder: (context, _) {
+                    final t = DownloadManager.instance.activeTask;
+                    final busy = t != null &&
+                        t.isTool &&
+                        t.gameId == widget.tool.id &&
+                        !t.finished;
+                    final installed = _installed ||
+                        (t != null &&
+                            t.isTool &&
+                            t.gameId == widget.tool.id &&
+                            t.finished &&
+                            !t.extractFailed);
+                    return Row(
+                      children: [
+                        Text(widget.tool.size,
+                            style: const TextStyle(
+                                color: AppColors.textDim, fontSize: 11)),
+                        const Spacer(),
+                        GestureDetector(
+                          onTap: busy
+                              ? null
+                              : () => installed ? _launch() : _install(),
+                          child: Container(
+                            padding: const EdgeInsets.symmetric(
+                                horizontal: 14, vertical: 4),
+                            decoration: BoxDecoration(
+                              color: busy
+                                  ? const Color(0xFF3A3F52)
+                                  : installed
+                                      ? AppColors.accent
+                                      : AppColors.green,
+                              borderRadius: BorderRadius.circular(3),
+                            ),
+                            child: Text(
+                              busy
+                                  ? '下载中'
+                                  : installed
+                                      ? '启动'
+                                      : '安装',
+                              style: const TextStyle(
+                                  color: Colors.white, fontSize: 11),
+                            ),
+                          ),
                         ),
-                        child: Text(
-                          installed ? '已安装' : '安装',
-                          style: const TextStyle(
-                              color: Colors.white, fontSize: 11),
-                        ),
-                      ),
-                    ),
-                  ],
+                      ],
+                    );
+                  },
                 ),
               ],
             ),

--
Gitblit v1.10.0