905086
2026-11-07 6697e53613d5ce562a19795ab41b19803c2bb93d
fluttergamebox/lib/pages/tools_page.dart
@@ -1,3 +1,5 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:fluttergamebox/api/api_client.dart';
import 'package:fluttergamebox/api/api_service.dart';
@@ -137,6 +139,7 @@
class _ToolCardState extends State<_ToolCard> {
  bool _installed = false;
  bool _uninstalling = false;
  @override
  void initState() {
@@ -172,9 +175,146 @@
    }
  }
  /// 右键菜单:已安装可卸载
  void _showContextMenu(Offset globalPosition) {
    final overlay =
        Overlay.of(context).context.findRenderObject() as RenderBox;
    showMenu<String>(
      context: context,
      color: const Color(0xFF1E2130),
      position: RelativeRect.fromLTRB(
          globalPosition.dx,
          globalPosition.dy,
          overlay.size.width - globalPosition.dx,
          overlay.size.height - globalPosition.dy),
      items: [
        if (_effectiveInstalled())
          const PopupMenuItem(
            value: 'uninstall',
            height: 40,
            child: Row(
              children: [
                Icon(Icons.delete_outline, color: AppColors.red, size: 18),
                SizedBox(width: 8),
                Text('卸载工具',
                    style: TextStyle(color: Colors.white, fontSize: 13)),
              ],
            ),
          ),
        if (!_effectiveInstalled())
          const PopupMenuItem(
            value: 'install',
            height: 40,
            child: Row(
              children: [
                Icon(Icons.download, color: AppColors.accent, size: 18),
                SizedBox(width: 8),
                Text('安装工具',
                    style: TextStyle(color: Colors.white, fontSize: 13)),
              ],
            ),
          ),
      ],
    ).then((value) {
      if (value == 'uninstall') _uninstall();
      if (value == 'install') _install();
    });
  }
  bool _effectiveInstalled() {
    final t = DownloadManager.instance.activeTask;
    return _installed ||
        (t != null &&
            t.isTool &&
            t.gameId == widget.tool.id &&
            t.finished &&
            !t.extractFailed);
  }
  /// 卸载:确认后直接删除工具目录
  Future<void> _uninstall() async {
    if (_uninstalling || !_effectiveInstalled()) return;
    final confirmed = await showDialog<bool>(
      context: context,
      builder: (context) => Dialog(
        backgroundColor: const Color(0xFF1E2130),
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
        child: Container(
          width: 360,
          padding: const EdgeInsets.all(24),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              const Icon(Icons.delete_outline,
                  color: AppColors.red, size: 48),
              const SizedBox(height: 16),
              const Text('卸载工具',
                  style: TextStyle(
                      color: Colors.white,
                      fontSize: 16,
                      fontWeight: FontWeight.bold)),
              const SizedBox(height: 8),
              Text(
                '将删除本机的「${widget.tool.name}」工具目录,卸载后可重新下载安装。',
                textAlign: TextAlign.center,
                style: const TextStyle(color: AppColors.textGray, fontSize: 13),
              ),
              const SizedBox(height: 20),
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  OutlinedButton(
                    onPressed: () => Navigator.pop(context, false),
                    style: OutlinedButton.styleFrom(
                      foregroundColor: AppColors.textGray,
                      side: const BorderSide(color: AppColors.bgLighter),
                      padding: const EdgeInsets.symmetric(
                          horizontal: 22, vertical: 10),
                    ),
                    child: const Text('取消'),
                  ),
                  const SizedBox(width: 12),
                  ElevatedButton(
                    onPressed: () => Navigator.pop(context, true),
                    style: ElevatedButton.styleFrom(
                      backgroundColor: AppColors.red,
                      foregroundColor: Colors.white,
                      padding: const EdgeInsets.symmetric(
                          horizontal: 22, vertical: 10),
                    ),
                    child: const Text('卸载'),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
    if (confirmed != true) return;
    setState(() => _uninstalling = true);
    try {
      final dir = Directory(await gameInstallDir(widget.tool.name));
      if (await dir.exists()) {
        await dir.delete(recursive: true);
      }
    } catch (_) {}
    // 清除残留的已完成任务记录,避免仍被判定为已安装
    DownloadManager.instance.clearFinishedToolTask(widget.tool.id);
    if (mounted) {
      setState(() {
        _uninstalling = false;
        _installed = false;
      });
    }
  }
  @override
  Widget build(BuildContext context) {
    return Container(
    return GestureDetector(
      onSecondaryTapUp: (d) => _showContextMenu(d.globalPosition),
      child: Container(
      decoration: BoxDecoration(
        color: AppColors.bgLight,
        borderRadius: BorderRadius.circular(6),
@@ -252,10 +392,11 @@
                  listenable: DownloadManager.instance,
                  builder: (context, _) {
                    final t = DownloadManager.instance.activeTask;
                    final busy = t != null &&
                    final downloading = t != null &&
                        t.isTool &&
                        t.gameId == widget.tool.id &&
                        !t.finished;
                    final busy = downloading || _uninstalling;
                    final installed = _installed ||
                        (t != null &&
                            t.isTool &&
@@ -284,11 +425,13 @@
                              borderRadius: BorderRadius.circular(3),
                            ),
                            child: Text(
                              busy
                                  ? '下载中'
                                  : installed
                                      ? '启动'
                                      : '安装',
                              _uninstalling
                                  ? '卸载中'
                                  : busy
                                      ? '下载中'
                                      : installed
                                          ? '启动'
                                          : '安装',
                              style: const TextStyle(
                                  color: Colors.white, fontSize: 11),
                            ),
@@ -303,6 +446,7 @@
          ),
        ],
      ),
      ),
    );
  }
}