From 7fd2270570811c0d29f0d5d87d11bfc4612e0743 Mon Sep 17 00:00:00 2001
From: 905086 <k2049@qq.com>
Date: Sat, 07 Nov 2026 20:30:06 +0800
Subject: [PATCH] feat: 实现用户密码找回功能及相关配套功能

---
 fluttergamebox/lib/pages/detail_page.dart |  243 ++++++++++++------------------------------------
 1 files changed, 63 insertions(+), 180 deletions(-)

diff --git a/fluttergamebox/lib/pages/detail_page.dart b/fluttergamebox/lib/pages/detail_page.dart
index f538ae3..3031f6b 100644
--- a/fluttergamebox/lib/pages/detail_page.dart
+++ b/fluttergamebox/lib/pages/detail_page.dart
@@ -1,6 +1,5 @@
 import 'dart:async';
 
-import 'package:file_picker/file_picker.dart';
 import 'package:flutter/material.dart';
 import 'package:fluttergamebox/api/api_client.dart';
 import 'package:fluttergamebox/api/api_service.dart';
@@ -21,6 +20,7 @@
   Game? game;
   bool isFavorite = false;
   bool inLibrary = false;
+  bool _installed = false; // 游戏是否已安装到本机
   bool _loading = true;
   int _mediaIndex = 0;
   Timer? _slideTimer;
@@ -60,6 +60,9 @@
               curve: Curves.easeInOut);
         }
       });
+      // 检测游戏是否已安装到本机
+      _installed = await isGameInstalled(game!.title, game!.launchExe);
+      if (mounted) setState(() {});
     } else {
       setState(() => _loading = false);
     }
@@ -69,6 +72,8 @@
     final res = await ApiService.toggleFavorite(widget.gameId);
     if (res['code'] == 0 && mounted) {
       setState(() => isFavorite = !isFavorite);
+      // 通知收藏页实时刷新
+      AppState.instance.bumpFavorites();
       _toast(res['msg'] ?? '操作成功');
     }
   }
@@ -77,6 +82,8 @@
     final res = await ApiService.addToLibrary(widget.gameId);
     if (res['code'] == 0 && mounted) {
       setState(() => inLibrary = true);
+      // 通知资源库页实时刷新
+      AppState.instance.bumpLibrary();
       _showConfirmDialog(
           '兑换成功!', '游戏已加入资源库,请在资源库页面查看。');
     } else if (mounted) {
@@ -86,13 +93,9 @@
 
   Future<void> _download() async {
     if (game == null) return;
-    // 检查下载路径
-    var dlPath = await AppPrefs.getDownloadPath();
-    if (dlPath.isEmpty) {
-      final ok = await _askDownloadPath();
-      if (!ok) return;
-      dlPath = await AppPrefs.getDownloadPath();
-    }
+    // 每次下载前都弹出选择下载安装路径
+    final ok = await _askDownloadPath();
+    if (!ok) return;
     // 会员校验+开始下载
     try {
       await DownloadManager.instance.startDownload(game!);
@@ -103,16 +106,23 @@
     }
   }
 
-  /// 首次下载询问下载路径
-  Future<bool> _askDownloadPath() async {
-    final res = await ApiService.downloadInfo(widget.gameId);
-    if (res['code'] != 0) {
-      if (mounted) _toast(res['msg'] ?? '无法下载', error: true);
-      return false;
+  /// 启动已安装的游戏
+  Future<void> _launch() async {
+    if (game == null) return;
+    final ok = await launchInstalledGame(game!.title, game!.launchExe);
+    if (!ok && mounted) {
+      _toast('未找到游戏文件,请先下载游戏', error: true);
     }
+  }
+
+  /// 下载前选择下载安装路径(并校验磁盘剩余空间)
+  Future<bool> _askDownloadPath() async {
     final result = await showDialog<bool>(
       context: context,
-      builder: (context) => _DownloadPathDialog(gameTitle: game!.title),
+      builder: (context) => DownloadPathDialog(
+        gameTitle: game!.title,
+        requiredGb: game!.requiredDiskGb,
+      ),
     );
     return result == true;
   }
@@ -484,8 +494,8 @@
             ],
           ),
           const SizedBox(height: 18),
-          _infoRow('游戏大小', game!.size),
-          _infoRow('磁盘占用', game!.installSize),
+          _infoRow('游戏大小', sizeWithGb(game!.size)),
+          _infoRow('磁盘占用', sizeWithGb(game!.installSize)),
           const Divider(color: AppColors.bgLighter, height: 28),
           _infoRow('操作系统', game!.osReq),
           _infoRow('CPU', game!.cpuReq),
@@ -525,27 +535,42 @@
             ),
           ),
           const SizedBox(height: 12),
-          // 下载游戏
-          SizedBox(
-            width: double.infinity,
-            height: 44,
-            child: ElevatedButton(
-              onPressed: _download,
-              style: ElevatedButton.styleFrom(
-                backgroundColor: AppColors.accent,
-                foregroundColor: Colors.white,
-                shape: RoundedRectangleBorder(
-                    borderRadius: BorderRadius.circular(4)),
-              ),
-              child: const Row(
-                mainAxisAlignment: MainAxisAlignment.center,
-                children: [
-                  Icon(Icons.download, size: 18),
-                  SizedBox(width: 8),
-                  Text('下载游戏', style: TextStyle(fontSize: 14)),
-                ],
-              ),
-            ),
+          // 下载游戏 / 启动游戏(已安装则切换为启动)
+          ListenableBuilder(
+            listenable: DownloadManager.instance,
+            builder: (context, _) {
+              // 本机已安装,或该游戏下载任务已完成(含解压)则显示启动按钮
+              final t = DownloadManager.instance.activeTask;
+              final installed = _installed ||
+                  (t != null &&
+                      t.gameId == game!.id &&
+                      !t.isTool &&
+                      t.finished &&
+                      !t.extractFailed);
+              return SizedBox(
+                width: double.infinity,
+                height: 44,
+                child: ElevatedButton(
+                  onPressed: installed ? _launch : _download,
+                  style: ElevatedButton.styleFrom(
+                    backgroundColor: AppColors.accent,
+                    foregroundColor: Colors.white,
+                    shape: RoundedRectangleBorder(
+                        borderRadius: BorderRadius.circular(4)),
+                  ),
+                  child: Row(
+                    mainAxisAlignment: MainAxisAlignment.center,
+                    children: [
+                      Icon(installed ? Icons.play_arrow : Icons.download,
+                          size: 18),
+                      const SizedBox(width: 8),
+                      Text(installed ? '启动游戏' : '下载游戏',
+                          style: const TextStyle(fontSize: 14)),
+                    ],
+                  ),
+                ),
+              );
+            },
           ),
         ],
       ),
@@ -586,145 +611,3 @@
     );
   }
 }
-
-/// 下载路径选择弹窗
-class _DownloadPathDialog extends StatefulWidget {
-  final String gameTitle;
-  const _DownloadPathDialog({required this.gameTitle});
-  @override
-  State<_DownloadPathDialog> createState() => _DownloadPathDialogState();
-}
-
-class _DownloadPathDialogState extends State<_DownloadPathDialog> {
-  final _dlCtrl = TextEditingController();
-  final _insCtrl = TextEditingController();
-
-  @override
-  void initState() {
-    super.initState();
-    _load();
-  }
-
-  Future<void> _load() async {
-    final dl = await AppPrefs.getDownloadPath();
-    final ins = await AppPrefs.getInstallPath();
-    setState(() {
-      _dlCtrl.text = dl.isEmpty ? 'C:\\99GameBox\\99GameD' : dl;
-      _insCtrl.text = ins.isEmpty ? 'C:\\99GameBox\\99GameI' : ins;
-    });
-  }
-
-  @override
-  Widget build(BuildContext context) {
-    return Dialog(
-      backgroundColor: const Color(0xFF1E2130),
-      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(6)),
-      child: Container(
-        width: 500,
-        padding: const EdgeInsets.all(24),
-        child: Column(
-          mainAxisSize: MainAxisSize.min,
-          crossAxisAlignment: CrossAxisAlignment.start,
-          children: [
-            Row(
-              children: [
-                const Icon(Icons.folder,
-                    size: 16, color: AppColors.textGray),
-                const SizedBox(width: 8),
-                const Text('请选择下载安装路径',
-                    style: TextStyle(
-                        color: Colors.white,
-                        fontSize: 14,
-                        fontWeight: FontWeight.w500)),
-                const Spacer(),
-                GestureDetector(
-                  onTap: () => Navigator.pop(context, false),
-                  child: const Icon(Icons.close,
-                      size: 16, color: AppColors.textGray),
-                ),
-              ],
-            ),
-            const SizedBox(height: 18),
-            _pathRow('下载路径', _dlCtrl),
-            const SizedBox(height: 12),
-            _pathRow('安装路径', _insCtrl),
-            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: () async {
-                    await AppPrefs.setPaths(_dlCtrl.text, _insCtrl.text);
-                    if (context.mounted) Navigator.pop(context, true);
-                  },
-                  style: ElevatedButton.styleFrom(
-                    backgroundColor: AppColors.accent,
-                    foregroundColor: Colors.white,
-                    padding: const EdgeInsets.symmetric(
-                        horizontal: 22, vertical: 10),
-                  ),
-                  child: const Text('确认'),
-                ),
-              ],
-            ),
-          ],
-        ),
-      ),
-    );
-  }
-
-  Widget _pathRow(String label, TextEditingController ctrl) {
-    return Column(
-      crossAxisAlignment: CrossAxisAlignment.start,
-      children: [
-        Text(label,
-            style: const TextStyle(color: AppColors.textGray, fontSize: 12)),
-        const SizedBox(height: 6),
-        Row(
-          children: [
-            Expanded(
-              child: TextField(
-                controller: ctrl,
-                style: const TextStyle(color: Colors.black, fontSize: 13),
-                decoration: InputDecoration(
-                  filled: true,
-                  fillColor: Colors.white,
-                  contentPadding: const EdgeInsets.symmetric(
-                      horizontal: 10, vertical: 10),
-                  border: OutlineInputBorder(
-                      borderRadius: BorderRadius.circular(4),
-                      borderSide: BorderSide.none),
-                ),
-              ),
-            ),
-            const SizedBox(width: 8),
-            ElevatedButton(
-              onPressed: () async {
-                final dir = await FilePicker.platform.getDirectoryPath();
-                if (dir != null) setState(() => ctrl.text = dir);
-              },
-              style: ElevatedButton.styleFrom(
-                backgroundColor: AppColors.accent,
-                foregroundColor: Colors.white,
-                padding: const EdgeInsets.symmetric(
-                    horizontal: 14, vertical: 11),
-              ),
-              child: const Text('选择目录', style: TextStyle(fontSize: 12)),
-            ),
-          ],
-        ),
-      ],
-    );
-  }
-}

--
Gitblit v1.10.0