905086
2026-11-07 7fd2270570811c0d29f0d5d87d11bfc4612e0743
fluttergamebox/lib/widgets/download_manager.dart
@@ -54,7 +54,7 @@
  Future<void> _launchGame(DownloadTask task) async {
    final ok = await launchInstalledGame(task.title, task.launchExe);
    if (!ok && mounted) {
      _toast('未找到游戏文件,请重新下载安装');
      _toast(task.isTool ? '未找到工具文件,请重新下载安装' : '未找到游戏文件,请重新下载安装');
    }
  }
@@ -475,13 +475,13 @@
                          padding: const EdgeInsets.symmetric(vertical: 12),
                        ),
                        child: (task.finished && !task.extractFailed)
                            ? const Row(
                            ? Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  Icon(Icons.play_arrow, size: 18),
                                  SizedBox(width: 8),
                                  Text('启动游戏',
                                      style: TextStyle(fontSize: 14)),
                                  const Icon(Icons.play_arrow, size: 18),
                                  const SizedBox(width: 8),
                                  Text(task.isTool ? '启动' : '启动游戏',
                                      style: const TextStyle(fontSize: 14)),
                                ],
                              )
                            : const Text('取消安装'),
@@ -556,9 +556,12 @@
}
/// 下载路径选择弹窗(详情页下载、资源库安装共用)
/// [requiredGb] 游戏所需磁盘空间(GB),所选下载目录所在磁盘剩余空间不足时禁止下载
class DownloadPathDialog extends StatefulWidget {
  final String gameTitle;
  const DownloadPathDialog({super.key, required this.gameTitle});
  final double requiredGb;
  const DownloadPathDialog(
      {super.key, required this.gameTitle, this.requiredGb = 0});
  @override
  State<DownloadPathDialog> createState() => _DownloadPathDialogState();
}
@@ -566,15 +569,26 @@
class _DownloadPathDialogState extends State<DownloadPathDialog> {
  final _dlCtrl = TextEditingController();
  final _insCtrl = TextEditingController();
  double _freeBytes = -2; // -2 未检查,-1 无法获取
  bool _checking = false;
  /// 需要的空间(字节):优先"磁盘占用",未设置时回退"游戏大小"
  double get _requiredBytes =>
      widget.requiredGb > 0 ? widget.requiredGb * 1024 * 1024 * 1024 : 0;
  bool get _insufficient =>
      _requiredBytes > 0 && _freeBytes >= 0 && _freeBytes < _requiredBytes;
  @override
  void initState() {
    super.initState();
    _dlCtrl.addListener(_checkFreeSpace);
    _load();
  }
  @override
  void dispose() {
    _dlCtrl.removeListener(_checkFreeSpace);
    _dlCtrl.dispose();
    _insCtrl.dispose();
    super.dispose();
@@ -587,6 +601,32 @@
      _dlCtrl.text = dl.isEmpty ? 'C:\\99GameBox\\99GameD' : dl;
      _insCtrl.text = ins.isEmpty ? 'C:\\99GameBox\\99GameI' : ins;
    });
    _checkFreeSpace();
  }
  /// 检查下载目录所在磁盘的剩余空间
  void _checkFreeSpace() {
    final path = _dlCtrl.text.trim();
    if (path.isEmpty || _checking) return;
    _checking = true;
    getDiskFreeBytes(path).then((free) {
      _checking = false;
      if (!mounted) return;
      // 检查期间路径又变了则重新检查
      final cur = _dlCtrl.text.trim();
      if (cur != path) {
        _checkFreeSpace();
        return;
      }
      setState(() => _freeBytes = free);
    });
  }
  String get _freeInfo {
    if (_checking) return '正在检查磁盘剩余空间...';
    if (_freeBytes < 0) return '';
    final freeGb = _freeBytes / 1024 / 1024 / 1024;
    return '下载目录所在磁盘剩余空间:${freeGb.toStringAsFixed(1)} GB';
  }
  @override
@@ -623,6 +663,22 @@
            _pathRow('下载路径', _dlCtrl),
            const SizedBox(height: 12),
            _pathRow('安装路径', _insCtrl),
            // 磁盘剩余空间提示
            if (_requiredBytes > 0) ...[
              const SizedBox(height: 10),
              Text(
                _insufficient
                    ? '磁盘剩余空间不足(需要 ${widget.requiredGb.toStringAsFixed(1)} GB,'
                      '当前剩余 ${(_freeBytes / 1024 / 1024 / 1024).toStringAsFixed(1)} GB),'
                      '请选择其他磁盘!'
                    : _freeInfo,
                style: TextStyle(
                    color: _insufficient
                        ? AppColors.red
                        : AppColors.textGray,
                    fontSize: 12),
              ),
            ],
            const SizedBox(height: 20),
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
@@ -639,12 +695,19 @@
                ),
                const SizedBox(width: 12),
                ElevatedButton(
                  onPressed: () async {
                    await AppPrefs.setPaths(_dlCtrl.text, _insCtrl.text);
                    if (context.mounted) Navigator.pop(context, true);
                  },
                  // 磁盘空间不足时禁止下载
                  onPressed: (_checking || _insufficient)
                      ? null
                      : () async {
                          await AppPrefs.setPaths(_dlCtrl.text, _insCtrl.text);
                          if (context.mounted) {
                            Navigator.pop(context, true);
                          }
                        },
                  style: ElevatedButton.styleFrom(
                    backgroundColor: AppColors.accent,
                    disabledBackgroundColor: AppColors.bgLighter,
                    disabledForegroundColor: AppColors.textDim,
                    foregroundColor: Colors.white,
                    padding: const EdgeInsets.symmetric(
                        horizontal: 22, vertical: 10),