| | |
| | | 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'; |
| | |
| | | Game? game; |
| | | bool isFavorite = false; |
| | | bool inLibrary = false; |
| | | bool _installed = false; // 游戏是否已安装到本机 |
| | | bool _loading = true; |
| | | int _mediaIndex = 0; |
| | | Timer? _slideTimer; |
| | |
| | | curve: Curves.easeInOut); |
| | | } |
| | | }); |
| | | // 检测游戏是否已安装到本机 |
| | | _installed = await isGameInstalled(game!.title, game!.launchExe); |
| | | if (mounted) setState(() {}); |
| | | } else { |
| | | setState(() => _loading = false); |
| | | } |
| | |
| | | |
| | | 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!); |
| | |
| | | } |
| | | } |
| | | |
| | | /// 首次下载询问下载路径 |
| | | 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), |
| | | ); |
| | | return result == true; |
| | | } |
| | |
| | | ), |
| | | ), |
| | | 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)), |
| | | ], |
| | | ), |
| | | ), |
| | | ); |
| | | }, |
| | | ), |
| | | ], |
| | | ), |
| | |
| | | ); |
| | | } |
| | | } |
| | | |
| | | /// 下载路径选择弹窗 |
| | | 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)), |
| | | ), |
| | | ], |
| | | ), |
| | | ], |
| | | ); |
| | | } |
| | | } |