| | |
| | | import 'package:fluttergamebox/common/theme.dart'; |
| | | import 'package:fluttergamebox/store/app_state.dart'; |
| | | import 'package:fluttergamebox/utils/util.dart'; |
| | | import 'package:fluttergamebox/widgets/download_manager.dart'; |
| | | import 'package:path/path.dart' as p; |
| | | |
| | | class LibraryPage extends StatefulWidget { |
| | |
| | | ], |
| | | ), |
| | | ) |
| | | : GridView.builder( |
| | | padding: const EdgeInsets.only(bottom: 40), |
| | | gridDelegate: |
| | | const SliverGridDelegateWithFixedCrossAxisCount( |
| | | crossAxisCount: 5, |
| | | mainAxisSpacing: 18, |
| | | crossAxisSpacing: 16, |
| | | childAspectRatio: 0.60, |
| | | ), |
| | | itemCount: filtered.length, |
| | | itemBuilder: (context, i) => |
| | | _LibraryCard(game: filtered[i], onRemove: _load), |
| | | ), |
| | | : LayoutBuilder(builder: (context, constraints) { |
| | | // 卡片高度 = 封面高度 + 信息区实际内容高度,避免出现大段空白 |
| | | const crossAxisSpacing = 16.0; |
| | | final cardWidth = |
| | | (constraints.maxWidth - crossAxisSpacing * 4) / 5; |
| | | final coverHeight = cardWidth / 1.42; |
| | | const infoHeight = 96.0; |
| | | return GridView.builder( |
| | | padding: const EdgeInsets.only(bottom: 40), |
| | | gridDelegate: |
| | | SliverGridDelegateWithFixedCrossAxisCount( |
| | | crossAxisCount: 5, |
| | | mainAxisSpacing: 18, |
| | | crossAxisSpacing: crossAxisSpacing, |
| | | mainAxisExtent: coverHeight + infoHeight, |
| | | ), |
| | | itemCount: filtered.length, |
| | | itemBuilder: (context, i) => |
| | | _LibraryCard(game: filtered[i], onRemoved: _load), |
| | | ); |
| | | }), |
| | | ), |
| | | ], |
| | | ), |
| | |
| | | } |
| | | |
| | | /// 资源库游戏卡片(右键菜单) |
| | | class _LibraryCard extends StatelessWidget { |
| | | class _LibraryCard extends StatefulWidget { |
| | | final Game game; |
| | | final VoidCallback onRemove; |
| | | const _LibraryCard({required this.game, required this.onRemove}); |
| | | final VoidCallback? onRemoved; // 从资源库移除后刷新列表 |
| | | const _LibraryCard({required this.game, this.onRemoved}); |
| | | @override |
| | | State<_LibraryCard> createState() => _LibraryCardState(); |
| | | } |
| | | |
| | | Future<String> _installDir() async { |
| | | final ins = await AppPrefs.getInstallPath(); |
| | | final base = ins.isEmpty ? 'C:\\99GameBox\\99GameI' : ins; |
| | | return '$base\\${game.title.replaceAll(RegExp(r'[\\/:*?"<>|]'), '_')}'; |
| | | class _LibraryCardState extends State<_LibraryCard> { |
| | | bool _installed = false; // 游戏是否已安装到本机 |
| | | bool _uninstalling = false; // 正在卸载(删除游戏目录) |
| | | |
| | | @override |
| | | void initState() { |
| | | super.initState(); |
| | | _checkInstalled(); |
| | | } |
| | | |
| | | Future<void> _launch(BuildContext context) async { |
| | | final dir = await _installDir(); |
| | | // 尝试查找目录中的exe并启动(启动已下载的游戏不需要会员) |
| | | final d = Directory(dir); |
| | | if (await d.exists()) { |
| | | final exes = d |
| | | .listSync(recursive: false) |
| | | .whereType<File>() |
| | | .where((f) => f.path.toLowerCase().endsWith('.exe')) |
| | | .toList(); |
| | | if (exes.isNotEmpty) { |
| | | Process.run(exes.first.path, [], workingDirectory: dir); |
| | | return; |
| | | } |
| | | Future<void> _checkInstalled() async { |
| | | final ok = await isGameInstalled(widget.game.title, widget.game.launchExe); |
| | | if (mounted) setState(() => _installed = ok); |
| | | } |
| | | |
| | | /// 当前是否已安装(本机已装,或该游戏的下载任务刚完成) |
| | | bool _effectiveInstalled() { |
| | | final t = DownloadManager.instance.activeTask; |
| | | return _installed || |
| | | (t != null && |
| | | t.gameId == widget.game.id && |
| | | !t.isTool && |
| | | t.finished && |
| | | !t.extractFailed); |
| | | } |
| | | |
| | | Future<String> _installDir() async => gameInstallDir(widget.game.title); |
| | | |
| | | /// 启动游戏(启动已下载的游戏不需要会员) |
| | | Future<void> _launch() async { |
| | | final ok = |
| | | await launchInstalledGame(widget.game.title, widget.game.launchExe); |
| | | if (!ok && mounted) { |
| | | _showToast('未找到游戏文件,请先下载游戏'); |
| | | } |
| | | // 未找到可执行文件 |
| | | } |
| | | |
| | | /// 安装(未安装时):选择路径后下载并安装 |
| | | Future<void> _install() async { |
| | | // 选择下载安装路径(并校验磁盘剩余空间) |
| | | final ok = await showDialog<bool>( |
| | | context: context, |
| | | builder: (context) => DownloadPathDialog( |
| | | gameTitle: widget.game.title, |
| | | requiredGb: widget.game.requiredDiskGb, |
| | | ), |
| | | ); |
| | | if (ok != true) return; |
| | | try { |
| | | await DownloadManager.instance.startDownload(widget.game); |
| | | if (mounted) showDownloadDialog(context); |
| | | } catch (e) { |
| | | _showToast(e.toString()); |
| | | } |
| | | } |
| | | |
| | | void _showToast(String msg) { |
| | | final overlay = Overlay.of(context); |
| | | final entry = OverlayEntry( |
| | | builder: (context) => Positioned( |
| | |
| | | color: Colors.transparent, |
| | | child: Container( |
| | | width: 280, |
| | | padding: const EdgeInsets.symmetric( |
| | | horizontal: 16, vertical: 12), |
| | | padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12), |
| | | decoration: BoxDecoration( |
| | | color: AppColors.red, |
| | | borderRadius: BorderRadius.circular(6), |
| | | ), |
| | | child: const Row( |
| | | child: Row( |
| | | children: [ |
| | | Icon(Icons.error_outline, color: Colors.white, size: 18), |
| | | SizedBox(width: 8), |
| | | const Icon(Icons.error_outline, color: Colors.white, size: 18), |
| | | const SizedBox(width: 8), |
| | | Expanded( |
| | | child: Text('未找到游戏文件,请先下载游戏', |
| | | style: |
| | | TextStyle(color: Colors.white, fontSize: 13)), |
| | | child: Text(msg, |
| | | style: const TextStyle( |
| | | color: Colors.white, fontSize: 13)), |
| | | ), |
| | | ], |
| | | ), |
| | |
| | | // 通过PowerShell创建桌面快捷方式(指向游戏目录) |
| | | final dir = await _installDir(); |
| | | final script = |
| | | '\$s=(New-Object -COM WScript.Shell).CreateShellLink(\"\$env:USERPROFILE\\Desktop\\${game.title}.lnk\");' |
| | | '\$s=(New-Object -COM WScript.Shell).CreateShellLink(\"\$env:USERPROFILE\\Desktop\\${widget.game.title}.lnk\");' |
| | | '\$s.TargetPath=\"$dir\"; \$s.Save()'; |
| | | Process.run('powershell', ['-command', script]); |
| | | } |
| | | |
| | | /// 卸载:确认后仅删除本机游戏目录(游戏保留在资源库中) |
| | | Future<void> _uninstall(BuildContext context) async { |
| | | final res = await ApiService.removeFromLibrary(game.id); |
| | | if (res['code'] == 0) { |
| | | onRemove(); |
| | | if (_uninstalling) 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.warning_amber_rounded, |
| | | color: AppColors.yellow, size: 48), |
| | | const SizedBox(height: 16), |
| | | const Text('确认卸载游戏', |
| | | style: TextStyle( |
| | | color: Colors.white, |
| | | fontSize: 16, |
| | | fontWeight: FontWeight.bold)), |
| | | const SizedBox(height: 8), |
| | | Text( |
| | | '将删除本机的「${widget.game.title}」游戏目录,游戏仍保留在资源库中,卸载后可在详情页重新下载。', |
| | | 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 _installDir()); |
| | | if (await dir.exists()) { |
| | | await dir.delete(recursive: true); |
| | | } |
| | | } catch (_) { |
| | | debugLog('uninstall delete dir failed: ${widget.game.title}'); |
| | | } |
| | | if (mounted) { |
| | | // 卸载完成后按钮由"卸载中"切换为"安装" |
| | | setState(() { |
| | | _uninstalling = false; |
| | | _installed = false; |
| | | }); |
| | | } |
| | | // 清除残留的已完成下载任务记录,避免仍被判定为已安装 |
| | | DownloadManager.instance.clearFinishedTask(widget.game.id); |
| | | } |
| | | |
| | | /// 从资源库移除(仅移除记录,不影响本机游戏文件) |
| | | Future<void> _removeFromLibrary(BuildContext context) async { |
| | | 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.list_alt, |
| | | color: AppColors.yellow, size: 48), |
| | | const SizedBox(height: 16), |
| | | const Text('从资源库移除', |
| | | style: TextStyle( |
| | | color: Colors.white, |
| | | fontSize: 16, |
| | | fontWeight: FontWeight.bold)), |
| | | const SizedBox(height: 8), |
| | | Text( |
| | | '将「${widget.game.title}」从资源库列表中移除,不影响本机已下载的游戏文件。', |
| | | 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; |
| | | |
| | | try { |
| | | final res = await ApiClient.delete('/api/library/${widget.game.id}'); |
| | | if (res['code'] == 0) { |
| | | AppState.instance.bumpLibrary(); |
| | | widget.onRemoved?.call(); // 刷新资源库列表 |
| | | } |
| | | } catch (_) { |
| | | debugLog('remove from library failed: ${widget.game.title}'); |
| | | } |
| | | } |
| | | |
| | |
| | | ), |
| | | child: Column( |
| | | mainAxisSize: MainAxisSize.min, |
| | | children: [ |
| | | _menuItem(context, Icons.play_arrow, '启动游戏', |
| | | () => _launch(context)), |
| | | _menuItem(context, Icons.link, '创建快捷方式', |
| | | _createShortcut), |
| | | _menuItem(context, Icons.folder_open, '打开游戏目录', |
| | | _openDir), |
| | | _menuItem(context, Icons.delete_outline, '卸载游戏', |
| | | () => _uninstall(context)), |
| | | ], |
| | | children: _effectiveInstalled() |
| | | ? [ |
| | | // 已安装:启动/快捷方式/打开目录/卸载 |
| | | _menuItem(context, Icons.play_arrow, '启动游戏', |
| | | _launch), |
| | | _menuItem(context, Icons.link, '创建快捷方式', |
| | | _createShortcut), |
| | | _menuItem(context, Icons.folder_open, '打开游戏目录', |
| | | _openDir), |
| | | _menuItem(context, Icons.delete_outline, '卸载游戏', |
| | | () => _uninstall(context)), |
| | | ] |
| | | : [ |
| | | // 未安装:安装/从资源库移除 |
| | | _menuItem(context, Icons.download, '安装游戏', |
| | | _install), |
| | | _menuItem(context, Icons.list_alt, '从资源库移除', |
| | | () => _removeFromLibrary(context)), |
| | | ], |
| | | ), |
| | | ), |
| | | ), |
| | |
| | | @override |
| | | Widget build(BuildContext context) { |
| | | return GestureDetector( |
| | | onTap: () => _launch(context), |
| | | onTap: () { |
| | | if (_uninstalling) return; // 卸载中不响应点击 |
| | | _effectiveInstalled() ? _launch() : _install(); |
| | | }, |
| | | onSecondaryTapDown: (details) => |
| | | _showContextMenu(context, details.globalPosition), |
| | | child: MouseRegion( |
| | |
| | | borderRadius: const BorderRadius.vertical( |
| | | top: Radius.circular(6)), |
| | | child: Image.network( |
| | | ApiClient.baseUrl + game.cover, |
| | | ApiClient.baseUrl + widget.game.cover, |
| | | fit: BoxFit.cover, |
| | | errorBuilder: (_, __, ___) => Container( |
| | | color: const Color(0xFF2A2E45), |
| | | child: Center( |
| | | child: Text( |
| | | game.title.characters.first, |
| | | widget.game.title.characters.first, |
| | | style: const TextStyle( |
| | | color: AppColors.textDim, |
| | | fontSize: 32)), |
| | |
| | | borderRadius: |
| | | BorderRadius.horizontal(right: Radius.circular(3)), |
| | | ), |
| | | child: Text(game.platformLabel, |
| | | child: Text(widget.game.platformLabel, |
| | | style: const TextStyle( |
| | | color: Colors.white, fontSize: 11)), |
| | | ), |
| | |
| | | color: Colors.black.withOpacity(0.6), |
| | | borderRadius: BorderRadius.circular(3), |
| | | ), |
| | | child: Text(game.size, |
| | | child: Text(sizeWithGb(widget.game.size), |
| | | style: const TextStyle( |
| | | color: Colors.white, fontSize: 11)), |
| | | ), |
| | |
| | | child: Column( |
| | | crossAxisAlignment: CrossAxisAlignment.start, |
| | | children: [ |
| | | if (game.versionNote.isNotEmpty) |
| | | if (widget.game.versionNote.isNotEmpty) |
| | | Padding( |
| | | padding: const EdgeInsets.only(bottom: 2), |
| | | child: Text( |
| | | game.versionNote, |
| | | widget.game.versionNote, |
| | | maxLines: 1, |
| | | overflow: TextOverflow.ellipsis, |
| | | style: const TextStyle( |
| | | color: AppColors.textDim, fontSize: 11), |
| | | ), |
| | | ), |
| | | Text(game.title, |
| | | Text(widget.game.title, |
| | | maxLines: 2, |
| | | overflow: TextOverflow.ellipsis, |
| | | style: const TextStyle( |
| | |
| | | children: [ |
| | | Expanded( |
| | | child: Text( |
| | | game.tags.split(',').first, |
| | | widget.game.tags.split(',').first, |
| | | maxLines: 1, |
| | | overflow: TextOverflow.ellipsis, |
| | | style: const TextStyle( |
| | | color: AppColors.textDim, fontSize: 11), |
| | | ), |
| | | ), |
| | | // 启动按钮(对标:标签行右侧紧凑按钮) |
| | | GestureDetector( |
| | | onTap: () => _launch(context), |
| | | child: Container( |
| | | padding: const EdgeInsets.symmetric( |
| | | horizontal: 14, vertical: 4), |
| | | decoration: BoxDecoration( |
| | | color: AppColors.accent, |
| | | borderRadius: BorderRadius.circular(3), |
| | | ), |
| | | child: const Text('启动', |
| | | style: TextStyle( |
| | | color: Colors.white, fontSize: 11)), |
| | | ), |
| | | // 启动/安装/卸载中按钮(随安装状态切换) |
| | | ListenableBuilder( |
| | | listenable: DownloadManager.instance, |
| | | builder: (context, _) { |
| | | final installed = _effectiveInstalled(); |
| | | final label = _uninstalling |
| | | ? '卸载中' |
| | | : installed |
| | | ? '启动' |
| | | : '安装'; |
| | | return GestureDetector( |
| | | // 卸载中禁用点击 |
| | | onTap: _uninstalling |
| | | ? null |
| | | : () => |
| | | installed ? _launch() : _install(), |
| | | child: Container( |
| | | padding: const EdgeInsets.symmetric( |
| | | horizontal: 14, vertical: 4), |
| | | decoration: BoxDecoration( |
| | | color: _uninstalling |
| | | ? AppColors.bgLighter |
| | | : installed |
| | | ? AppColors.accent |
| | | : AppColors.green, |
| | | borderRadius: BorderRadius.circular(3), |
| | | ), |
| | | child: Text(label, |
| | | style: const TextStyle( |
| | | color: Colors.white, fontSize: 11)), |
| | | ), |
| | | ); |
| | | }, |
| | | ), |
| | | ], |
| | | ), |