905086
2026-11-07 6697e53613d5ce562a19795ab41b19803c2bb93d
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);
    }
@@ -99,11 +102,20 @@
    }
  }
  /// 启动已安装的游戏
  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),
    );
    return result == true;
  }
@@ -516,27 +528,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)),
                    ],
                  ),
                ),
              );
            },
          ),
        ],
      ),
@@ -577,145 +604,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)),
            ),
          ],
        ),
      ],
    );
  }
}