| | |
| | | import 'package:flutter/material.dart'; |
| | | import 'dart:io'; |
| | | |
| | | import 'package:flutter/material.dart'; |
| | | import 'package:fluttergamebox/api/api_client.dart'; |
| | | import 'package:fluttergamebox/api/api_service.dart'; |
| | | import 'package:fluttergamebox/common/theme.dart'; |
| | |
| | | class _ToolsPageState extends State<ToolsPage> |
| | | with AutomaticKeepAliveClientMixin { |
| | | List<Tool> tools = []; |
| | | final Set<int> _installed = {}; |
| | | bool _loading = true; |
| | | |
| | | @override |
| | |
| | | }); |
| | | } |
| | | |
| | | Future<void> _install(Tool tool) async { |
| | | if (_installed.contains(tool.id)) return; |
| | | // 模拟安装 |
| | | setState(() => _installed.add(tool.id)); |
| | | _toast('${tool.name} 安装完成'); |
| | | } |
| | | |
| | | void _toast(String msg) { |
| | | void _toast(String msg, {bool error = false}) { |
| | | final overlay = Overlay.of(context); |
| | | final entry = OverlayEntry( |
| | | builder: (context) => Positioned( |
| | |
| | | padding: |
| | | const EdgeInsets.symmetric(horizontal: 16, vertical: 12), |
| | | decoration: BoxDecoration( |
| | | color: AppColors.green, |
| | | color: error ? AppColors.red : AppColors.green, |
| | | borderRadius: BorderRadius.circular(6), |
| | | ), |
| | | child: Row( |
| | | children: [ |
| | | const Icon(Icons.check_circle, |
| | | Icon(error ? Icons.error_outline : Icons.check_circle, |
| | | color: Colors.white, size: 18), |
| | | const SizedBox(width: 8), |
| | | Expanded( |
| | |
| | | fontWeight: FontWeight.w500)), |
| | | const SizedBox(width: 12), |
| | | const Text('游戏必备运行库和实用工具', |
| | | style: TextStyle( |
| | | color: AppColors.textGray, fontSize: 13)), |
| | | style: |
| | | TextStyle(color: AppColors.textGray, fontSize: 13)), |
| | | ], |
| | | ), |
| | | const SizedBox(height: 16), |
| | |
| | | ), |
| | | itemCount: tools.length, |
| | | itemBuilder: (context, i) => |
| | | _ToolCard(tool: tools[i], installed: _installed.contains(tools[i].id), onInstall: () => _install(tools[i])), |
| | | _ToolCard(tool: tools[i], onError: _toast), |
| | | ), |
| | | ), |
| | | ], |
| | |
| | | } |
| | | } |
| | | |
| | | class _ToolCard extends StatelessWidget { |
| | | class _ToolCard extends StatefulWidget { |
| | | final Tool tool; |
| | | final bool installed; |
| | | final VoidCallback onInstall; |
| | | const _ToolCard({ |
| | | required this.tool, |
| | | required this.installed, |
| | | required this.onInstall, |
| | | }); |
| | | final void Function(String msg, {bool error}) onError; |
| | | const _ToolCard({required this.tool, required this.onError}); |
| | | @override |
| | | State<_ToolCard> createState() => _ToolCardState(); |
| | | } |
| | | |
| | | class _ToolCardState extends State<_ToolCard> { |
| | | bool _installed = false; |
| | | bool _uninstalling = false; |
| | | |
| | | @override |
| | | void initState() { |
| | | super.initState(); |
| | | _checkInstalled(); |
| | | } |
| | | |
| | | Future<void> _checkInstalled() async { |
| | | final ok = await isGameInstalled(widget.tool.name, widget.tool.launchExe); |
| | | if (mounted) setState(() => _installed = ok); |
| | | } |
| | | |
| | | /// 启动已安装的工具 |
| | | Future<void> _launch() async { |
| | | final ok = |
| | | await launchInstalledGame(widget.tool.name, widget.tool.launchExe); |
| | | if (!ok) widget.onError('未找到工具文件,请重新下载安装', error: true); |
| | | } |
| | | |
| | | /// 安装:选择下载路径后下载并解压(与游戏流程一致) |
| | | Future<void> _install() async { |
| | | final ok = await showDialog<bool>( |
| | | context: context, |
| | | builder: (context) => |
| | | DownloadPathDialog(gameTitle: widget.tool.name), |
| | | ); |
| | | if (ok != true) return; |
| | | try { |
| | | await DownloadManager.instance.startToolDownload(widget.tool); |
| | | if (mounted) showDownloadDialog(context); |
| | | } catch (e) { |
| | | widget.onError(e.toString(), error: true); |
| | | } |
| | | } |
| | | |
| | | /// 右键菜单:已安装可卸载 |
| | | void _showContextMenu(Offset globalPosition) { |
| | | final overlay = |
| | | Overlay.of(context).context.findRenderObject() as RenderBox; |
| | | showMenu<String>( |
| | | context: context, |
| | | color: const Color(0xFF1E2130), |
| | | position: RelativeRect.fromLTRB( |
| | | globalPosition.dx, |
| | | globalPosition.dy, |
| | | overlay.size.width - globalPosition.dx, |
| | | overlay.size.height - globalPosition.dy), |
| | | items: [ |
| | | if (_effectiveInstalled()) |
| | | const PopupMenuItem( |
| | | value: 'uninstall', |
| | | height: 40, |
| | | child: Row( |
| | | children: [ |
| | | Icon(Icons.delete_outline, color: AppColors.red, size: 18), |
| | | SizedBox(width: 8), |
| | | Text('卸载工具', |
| | | style: TextStyle(color: Colors.white, fontSize: 13)), |
| | | ], |
| | | ), |
| | | ), |
| | | if (!_effectiveInstalled()) |
| | | const PopupMenuItem( |
| | | value: 'install', |
| | | height: 40, |
| | | child: Row( |
| | | children: [ |
| | | Icon(Icons.download, color: AppColors.accent, size: 18), |
| | | SizedBox(width: 8), |
| | | Text('安装工具', |
| | | style: TextStyle(color: Colors.white, fontSize: 13)), |
| | | ], |
| | | ), |
| | | ), |
| | | ], |
| | | ).then((value) { |
| | | if (value == 'uninstall') _uninstall(); |
| | | if (value == 'install') _install(); |
| | | }); |
| | | } |
| | | |
| | | bool _effectiveInstalled() { |
| | | final t = DownloadManager.instance.activeTask; |
| | | return _installed || |
| | | (t != null && |
| | | t.isTool && |
| | | t.gameId == widget.tool.id && |
| | | t.finished && |
| | | !t.extractFailed); |
| | | } |
| | | |
| | | /// 卸载:确认后直接删除工具目录 |
| | | Future<void> _uninstall() async { |
| | | if (_uninstalling || !_effectiveInstalled()) 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.delete_outline, |
| | | color: AppColors.red, size: 48), |
| | | const SizedBox(height: 16), |
| | | const Text('卸载工具', |
| | | style: TextStyle( |
| | | color: Colors.white, |
| | | fontSize: 16, |
| | | fontWeight: FontWeight.bold)), |
| | | const SizedBox(height: 8), |
| | | Text( |
| | | '将删除本机的「${widget.tool.name}」工具目录,卸载后可重新下载安装。', |
| | | 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 gameInstallDir(widget.tool.name)); |
| | | if (await dir.exists()) { |
| | | await dir.delete(recursive: true); |
| | | } |
| | | } catch (_) {} |
| | | // 清除残留的已完成任务记录,避免仍被判定为已安装 |
| | | DownloadManager.instance.clearFinishedToolTask(widget.tool.id); |
| | | if (mounted) { |
| | | setState(() { |
| | | _uninstalling = false; |
| | | _installed = false; |
| | | }); |
| | | } |
| | | } |
| | | |
| | | @override |
| | | Widget build(BuildContext context) { |
| | | return Container( |
| | | return GestureDetector( |
| | | onSecondaryTapUp: (d) => _showContextMenu(d.globalPosition), |
| | | child: Container( |
| | | decoration: BoxDecoration( |
| | | color: AppColors.bgLight, |
| | | borderRadius: BorderRadius.circular(6), |
| | |
| | | borderRadius: const BorderRadius.vertical( |
| | | top: Radius.circular(6)), |
| | | child: Image.network( |
| | | ApiClient.baseUrl + tool.cover, |
| | | ApiClient.baseUrl + widget.tool.cover, |
| | | fit: BoxFit.cover, |
| | | errorBuilder: (_, __, ___) => Container( |
| | | color: const Color(0xFF2A2E45), |
| | | child: Center( |
| | | child: Text( |
| | | tool.name.characters.first, |
| | | widget.tool.name.characters.first, |
| | | style: const TextStyle( |
| | | color: AppColors.textDim, fontSize: 28), |
| | | ), |
| | |
| | | padding: const EdgeInsets.all(10), |
| | | child: Column( |
| | | children: [ |
| | | Text(tool.description, |
| | | // 工具名称 |
| | | Text(widget.tool.name, |
| | | maxLines: 1, |
| | | overflow: TextOverflow.ellipsis, |
| | | style: const TextStyle( |
| | | color: Colors.white, fontSize: 12)), |
| | | color: Colors.white, |
| | | fontSize: 12, |
| | | fontWeight: FontWeight.w500)), |
| | | if (widget.tool.description.isNotEmpty) ...[ |
| | | const SizedBox(height: 4), |
| | | // 工具描述 |
| | | Text(widget.tool.description, |
| | | maxLines: 1, |
| | | overflow: TextOverflow.ellipsis, |
| | | style: const TextStyle( |
| | | color: AppColors.textDim, fontSize: 11)), |
| | | ], |
| | | const SizedBox(height: 8), |
| | | Row( |
| | | children: [ |
| | | Text(tool.size, |
| | | style: const TextStyle( |
| | | color: AppColors.textDim, fontSize: 11)), |
| | | const Spacer(), |
| | | GestureDetector( |
| | | onTap: onInstall, |
| | | child: Container( |
| | | padding: const EdgeInsets.symmetric( |
| | | horizontal: 14, vertical: 4), |
| | | decoration: BoxDecoration( |
| | | color: installed |
| | | ? const Color(0xFF3A3F52) |
| | | : AppColors.accent, |
| | | borderRadius: BorderRadius.circular(3), |
| | | // 按钮状态随下载管理器任务实时变化 |
| | | ListenableBuilder( |
| | | listenable: DownloadManager.instance, |
| | | builder: (context, _) { |
| | | final t = DownloadManager.instance.activeTask; |
| | | final downloading = t != null && |
| | | t.isTool && |
| | | t.gameId == widget.tool.id && |
| | | !t.finished; |
| | | final busy = downloading || _uninstalling; |
| | | final installed = _installed || |
| | | (t != null && |
| | | t.isTool && |
| | | t.gameId == widget.tool.id && |
| | | t.finished && |
| | | !t.extractFailed); |
| | | return Row( |
| | | children: [ |
| | | Text(widget.tool.size, |
| | | style: const TextStyle( |
| | | color: AppColors.textDim, fontSize: 11)), |
| | | const Spacer(), |
| | | GestureDetector( |
| | | onTap: busy |
| | | ? null |
| | | : () => installed ? _launch() : _install(), |
| | | child: Container( |
| | | padding: const EdgeInsets.symmetric( |
| | | horizontal: 14, vertical: 4), |
| | | decoration: BoxDecoration( |
| | | color: busy |
| | | ? const Color(0xFF3A3F52) |
| | | : installed |
| | | ? AppColors.accent |
| | | : AppColors.green, |
| | | borderRadius: BorderRadius.circular(3), |
| | | ), |
| | | child: Text( |
| | | _uninstalling |
| | | ? '卸载中' |
| | | : busy |
| | | ? '下载中' |
| | | : installed |
| | | ? '启动' |
| | | : '安装', |
| | | style: const TextStyle( |
| | | color: Colors.white, fontSize: 11), |
| | | ), |
| | | ), |
| | | ), |
| | | child: Text( |
| | | installed ? '已安装' : '安装', |
| | | style: const TextStyle( |
| | | color: Colors.white, fontSize: 11), |
| | | ), |
| | | ), |
| | | ), |
| | | ], |
| | | ], |
| | | ); |
| | | }, |
| | | ), |
| | | ], |
| | | ), |
| | | ), |
| | | ], |
| | | ), |
| | | ), |
| | | ); |
| | | } |
| | | } |