import 'dart:io'; import 'package:flutter/material.dart'; import 'package:fluttergamebox/api/api_client.dart'; import 'package:fluttergamebox/common/theme.dart'; import 'package:fluttergamebox/pages/detail_page.dart'; import 'package:fluttergamebox/pages/favorites_page.dart'; import 'package:fluttergamebox/pages/library_page.dart'; import 'package:fluttergamebox/pages/store_page.dart'; import 'package:fluttergamebox/pages/tools_page.dart'; import 'package:fluttergamebox/store/app_state.dart'; import 'package:fluttergamebox/widgets/activate_dialog.dart'; import 'package:fluttergamebox/widgets/download_manager.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:window_manager/window_manager.dart'; import 'package:fluttergamebox/widgets/info_dialogs.dart'; import 'package:fluttergamebox/widgets/window_controls.dart'; /// 主界面导航:卡片点击打开详情页(保留顶部导航栏) class ShellNav { static final ShellNav instance = ShellNav(); void Function(int gameId)? openDetail; VoidCallback? goBack; } class MainShell extends StatefulWidget { const MainShell({super.key}); @override State createState() => _MainShellState(); } class _MainShellState extends State { int _navIndex = 0; // 0商店 1资源库 2收藏 3工具 int? _detailGameId; // 非空时显示详情页(带顶部导航栏) final _storeKey = GlobalKey(); final _libKey = GlobalKey(); final _favKey = GlobalKey(); @override void initState() { super.initState(); AppState.instance.refreshUserInfo(); ShellNav.instance.openDetail = (id) => setState(() => _detailGameId = id); ShellNav.instance.goBack = _closeDetail; } @override void dispose() { ShellNav.instance.openDetail = null; ShellNav.instance.goBack = null; super.dispose(); } void _closeDetail() { setState(() => _detailGameId = null); // 详情页里可能收藏/加入资源库/下载,关闭后刷新各页面 _storeKey.currentState?.refresh(); _libKey.currentState?.refresh(); _favKey.currentState?.refresh(); } void _switchNav(int index) { setState(() { _navIndex = index; _detailGameId = null; }); // 切换页面时刷新对应数据(如收藏、资源库) switch (index) { case 0: _storeKey.currentState?.refresh(); case 1: _libKey.currentState?.refresh(); case 2: _favKey.currentState?.refresh(); } } @override Widget build(BuildContext context) { return Scaffold( body: Column( children: [ _buildTopBar(), // 进度条(下载中显示) AnimatedBuilder( animation: DownloadManager.instance, builder: (context, _) { final task = DownloadManager.instance.activeTask; if (task == null || task.finished) return const SizedBox.shrink(); return Container( height: 3, color: AppColors.bgLight, child: LinearProgressIndicator( value: task.progress, backgroundColor: Colors.transparent, color: AppColors.accent, ), ); }, ), Expanded( child: _detailGameId != null ? DetailPage( key: ValueKey('detail_$_detailGameId'), gameId: _detailGameId!, onBack: _closeDetail, ) : Stack( children: [ // 主内容 IndexedStack( index: _navIndex, children: [ StorePage(key: _storeKey), LibraryPage(key: _libKey), FavoritesPage(key: _favKey), const ToolsPage(), ], ), // 左侧浮动栏 const Positioned(left: 0, top: 20, child: _LeftSidebar()), // 右侧浮动栏 Positioned(right: 0, top: 20, child: _RightSidebar( onActivate: () => showActivateDialog(context), )), ], ), ), ], ), ); } Widget _buildTopBar() { return GestureDetector( // 拖动顶栏移动窗口;双击最大化/还原 onPanStart: (_) => windowManager.startDragging(), onDoubleTap: () async { if (await windowManager.isMaximized()) { windowManager.unmaximize(); } else { windowManager.maximize(); } }, child: Container( height: 52, color: const Color(0xFF191C2B), child: Row( children: [ const SizedBox(width: 14), // Logo ClipOval( child: Image.asset('assets/images/logo.png', width: 28, height: 28, fit: BoxFit.cover, errorBuilder: (_, __, ___) => Container( width: 28, height: 28, color: AppColors.pink, child: const Center( child: Text('游', style: TextStyle( color: Colors.white, fontSize: 11))))), ), const SizedBox(width: 8), const Text('游趣电玩', style: TextStyle( color: Colors.white, fontSize: 14, fontWeight: FontWeight.bold)), const SizedBox(width: 24), // 返回 / 前进 / 主页(对标顶部导航栏) IconButton( icon: Icon(Icons.arrow_back, size: 18, color: _detailGameId != null ? AppColors.textWhite : AppColors.textDim), onPressed: _detailGameId != null ? _closeDetail : null, tooltip: '返回', ), const Icon(Icons.arrow_forward, size: 18, color: AppColors.textDim), const SizedBox(width: 4), IconButton( icon: Icon(Icons.home_outlined, size: 18, color: _navIndex == 0 && _detailGameId == null ? AppColors.textWhite : AppColors.textGray), onPressed: () => _switchNav(0), tooltip: '主页', ), const SizedBox(width: 8), // 导航按钮 _NavItem( icon: Icons.shopping_bag_outlined, label: '商店', active: _navIndex == 0, onTap: () => _switchNav(0), ), _NavItem( icon: Icons.folder_outlined, label: '资源库', active: _navIndex == 1, onTap: () => _switchNav(1), ), _NavItem( icon: Icons.star_outline, label: '收藏', active: _navIndex == 2, onTap: () => _switchNav(2), ), _NavItem( icon: Icons.build_outlined, label: '工具', active: _navIndex == 3, onTap: () => _switchNav(3), ), const Spacer(), // 下载状态指示 AnimatedBuilder( animation: DownloadManager.instance, builder: (context, _) { final dm = DownloadManager.instance; final task = dm.activeTask; if (task == null) return const SizedBox.shrink(); if (task.finished) { return GestureDetector( onTap: () => showDownloadDialog(context), child: Container( padding: const EdgeInsets.symmetric( horizontal: 10, vertical: 4), decoration: BoxDecoration( color: AppColors.green.withOpacity(0.15), borderRadius: BorderRadius.circular(4), ), child: const Text('下载完成', style: TextStyle( color: AppColors.green, fontSize: 12)), ), ); } return GestureDetector( onTap: () => showDownloadDialog(context), child: Container( padding: const EdgeInsets.symmetric( horizontal: 10, vertical: 4), decoration: BoxDecoration( color: AppColors.accent.withOpacity(0.15), borderRadius: BorderRadius.circular(4), ), child: Text( task.paused ? '已暂停 ${(task.progress * 100).toStringAsFixed(0)}%' : '${(task.progress * 100).toStringAsFixed(0)}%', style: const TextStyle( color: AppColors.accent, fontSize: 12), ), ), ); }, ), const SizedBox(width: 16), // 用户信息 _UserMenu(), const SizedBox(width: 14), // 问题按钮 const Icon(Icons.help_outline, size: 18, color: AppColors.textGray), const SizedBox(width: 8), // 更多菜单 const _MoreMenu(), const SizedBox(width: 4), const WindowControls(), ], ), ), ); } } class _NavItem extends StatelessWidget { final IconData icon; final String label; final bool active; final VoidCallback onTap; const _NavItem({ required this.icon, required this.label, required this.active, required this.onTap, }); @override Widget build(BuildContext context) { return InkWell( onTap: onTap, borderRadius: BorderRadius.circular(6), child: Container( padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 8), decoration: BoxDecoration( color: active ? AppColors.accent.withOpacity(0.15) : null, borderRadius: BorderRadius.circular(6), ), child: Row( children: [ Icon(icon, size: 18, color: active ? AppColors.accent : AppColors.textGray), const SizedBox(width: 6), Text(label, style: TextStyle( color: active ? AppColors.accent : AppColors.textGray, fontSize: 14, fontWeight: active ? FontWeight.w500 : FontWeight.normal)), ], ), ), ); } } /// 用户头像 + 会员信息悬浮 class _UserMenu extends StatefulWidget { @override State<_UserMenu> createState() => _UserMenuState(); } class _UserMenuState extends State<_UserMenu> { final LayerLink _layerLink = LayerLink(); OverlayEntry? _entry; void _toggle() { if (_entry != null) { _remove(); } else { _show(); } } void _remove() { _entry?.remove(); _entry = null; } void _show() { final app = AppState.instance; _entry = OverlayEntry( builder: (context) => Stack( children: [ GestureDetector( behavior: HitTestBehavior.opaque, onTap: _remove, child: Container(color: Colors.transparent), ), CompositedTransformFollower( link: _layerLink, targetAnchor: Alignment.bottomCenter, followerAnchor: Alignment.topCenter, offset: const Offset(0, 8), child: Material( color: Colors.transparent, child: Container( width: 240, padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: const Color(0xFF232739), borderRadius: BorderRadius.circular(8), border: Border.all(color: AppColors.bgLighter), boxShadow: [ BoxShadow( color: Colors.black.withOpacity(0.5), blurRadius: 16) ], ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: [ Row( children: [ Text('No.${app.userId}', style: const TextStyle( color: Colors.white, fontSize: 15, fontWeight: FontWeight.bold)), const SizedBox(width: 8), if (app.isMember) Container( padding: const EdgeInsets.symmetric( horizontal: 6, vertical: 2), decoration: BoxDecoration( color: AppColors.red, borderRadius: BorderRadius.circular(3), ), child: const Text('VIP会员', style: TextStyle( color: Colors.white, fontSize: 10)), ), ], ), const SizedBox(height: 8), Text( app.isMember ? '到期时间:${app.memberExpire.split(' ').first}' : '您还不是会员,请激活密钥', style: TextStyle( color: app.isMember ? AppColors.textGray : AppColors.yellow, fontSize: 13), ), const SizedBox(height: 12), SizedBox( width: double.infinity, height: 34, child: ElevatedButton( onPressed: () { _remove(); showActivateDialog(context); }, style: ElevatedButton.styleFrom( backgroundColor: AppColors.accent, foregroundColor: Colors.white, elevation: 0, ), child: const Text('会员激活', style: TextStyle(fontSize: 13)), ), ), const Divider(color: AppColors.bgLighter, height: 24), SizedBox( width: double.infinity, height: 34, child: OutlinedButton( onPressed: () { _remove(); AppState.instance.logout(); }, style: OutlinedButton.styleFrom( foregroundColor: AppColors.textGray, side: const BorderSide(color: AppColors.bgLighter), ), child: const Text('退出登录', style: TextStyle(fontSize: 13)), ), ), ], ), ), ), ), ], ), ); Overlay.of(context).insert(_entry!); } @override Widget build(BuildContext context) { return CompositedTransformTarget( link: _layerLink, child: GestureDetector( onTap: _toggle, child: Row( children: [ ClipOval( child: Image.asset('assets/images/logo.png', width: 26, height: 26, fit: BoxFit.cover, errorBuilder: (_, __, ___) => Container( width: 26, height: 26, color: AppColors.pink, child: const Center( child: Text('99', style: TextStyle( color: Colors.white, fontSize: 10))))), ), const SizedBox(width: 6), Text( 'No.${AppState.instance.userId}', style: const TextStyle(color: AppColors.textGray, fontSize: 13), ), ], ), ), ); } } /// 右上角"更多"下拉菜单 class _MoreMenu extends StatefulWidget { const _MoreMenu(); @override State<_MoreMenu> createState() => _MoreMenuState(); } class _MoreMenuState extends State<_MoreMenu> { final LayerLink _layerLink = LayerLink(); OverlayEntry? _entry; void _toggle() { if (_entry != null) { _remove(); } else { _show(); } } void _remove() { _entry?.remove(); _entry = null; } void _show() { const menus = [ ('电脑设备配置', Icons.computer), ('电脑网络信息', Icons.wifi), ('下载安装路径', Icons.folder_open), ('打开下载目录', Icons.download), ('打开安装目录', Icons.install_desktop), ('游戏恢复', Icons.restore), ('重启应用', Icons.restart_alt), ('检查更新', Icons.update), ('退出应用', Icons.power_settings_new), ]; _entry = OverlayEntry( builder: (context) => Stack( children: [ GestureDetector( behavior: HitTestBehavior.opaque, onTap: _remove, child: Container(color: Colors.transparent), ), CompositedTransformFollower( link: _layerLink, targetAnchor: Alignment.topRight, followerAnchor: Alignment.topRight, offset: const Offset(-4, 6), child: Material( color: Colors.transparent, child: Container( width: 170, decoration: BoxDecoration( color: const Color(0xFF232739), borderRadius: BorderRadius.circular(8), border: Border.all(color: AppColors.bgLighter), boxShadow: [ BoxShadow( color: Colors.black.withOpacity(0.5), blurRadius: 16) ], ), child: Column( mainAxisSize: MainAxisSize.min, children: menus .map((m) => InkWell( onTap: () { _remove(); _onMenuTap(m.$1); }, child: Padding( padding: const EdgeInsets.symmetric( horizontal: 14, vertical: 10), child: Row( children: [ Icon(m.$2, size: 16, color: AppColors.textGray), const SizedBox(width: 10), Text(m.$1, style: const TextStyle( color: AppColors.textWhite, fontSize: 13)), ], ), ), )) .toList(), ), ), ), ), ], ), ); Overlay.of(context).insert(_entry!); } Future _onMenuTap(String name) async { switch (name) { case '电脑设备配置': showDeviceConfigDialog(context); case '电脑网络信息': showNetworkInfoDialog(context); case '下载安装路径': showPathDialog(context); case '打开下载目录': _openDir(await _dir('download_path', '99GameD')); case '打开安装目录': _openDir(await _dir('install_path', '99GameI')); case '游戏恢复': _showToast('游戏恢复功能:重新下载可恢复游戏文件'); case '重启应用': _showToast('应用重启中...'); case '检查更新': _showToast('当前已是最新版本!'); case '退出应用': exit(0); } } void _showToast(String msg) { final ctx = context; // 使用全局 overlay 显示 final overlay = Overlay.maybeOf(ctx); if (overlay == null) return; final entry = OverlayEntry( builder: (context) => Positioned( top: 60, left: MediaQuery.of(ctx).size.width / 2 - 140, child: Material( color: Colors.transparent, child: Container( width: 280, padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12), decoration: BoxDecoration( color: AppColors.green, borderRadius: BorderRadius.circular(6), ), child: Row( children: [ const Icon(Icons.check_circle, color: Colors.white, size: 18), const SizedBox(width: 8), Expanded( child: Text(msg, style: const TextStyle( color: Colors.white, fontSize: 13)), ), ], ), ), ), ), ); overlay.insert(entry); Future.delayed(const Duration(seconds: 2), () => entry.remove()); } Future _dir(String key, String sub) async { final sp = await SharedPreferencesUtil.get(key); return sp.isNotEmpty ? sp : 'C:\\99GameBox\\$sub'; } void _openDir(String path) async { final dir = Directory(path); if (!await dir.exists()) { await dir.create(recursive: true); } Process.run('explorer.exe', [path]); } @override Widget build(BuildContext context) { return CompositedTransformTarget( link: _layerLink, child: GestureDetector( onTap: _toggle, child: Container( padding: const EdgeInsets.all(6), child: const Icon(Icons.menu, size: 18, color: AppColors.textGray), ), ), ); } } class SharedPreferencesUtil { static Future get(String key) async { final sp = await SharedPreferences.getInstance(); return sp.getString(key) ?? ''; } } /// 左侧浮动栏:系统公告/联系客服/常见问题 class _LeftSidebar extends StatelessWidget { const _LeftSidebar(); @override Widget build(BuildContext context) { return Container( decoration: BoxDecoration( color: const Color(0xFF191C2B).withOpacity(0.95), borderRadius: const BorderRadius.horizontal(right: Radius.circular(8)), border: Border.all(color: AppColors.bgLighter), ), padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 4), child: Column( mainAxisSize: MainAxisSize.min, children: [ _sideIcon(context, Icons.campaign, '系统公告', () { _showAnnouncement(context); }), const SizedBox(height: 14), _sideIcon(context, Icons.support_agent, '联系客服', () { _showToast2(context, '客服在线时间:早8点--晚10点'); }), const SizedBox(height: 14), _sideIcon(context, Icons.help_center_outlined, '常见问题', () { _showToast2(context, '常见问题:右键游戏图片可打开安装目录'); }), ], ), ); } void _showAnnouncement(BuildContext context) async { final res = await ApiClient.get('/api/announcements'); if (!context.mounted) return; final content = res['code'] == 0 && (res['data'] as List).isNotEmpty ? res['data'][0]['content'] : '暂无公告'; showDialog( context: context, builder: (context) => AlertDialog( backgroundColor: Colors.white, title: const Text('系统公告', style: TextStyle(color: Colors.black, fontSize: 16)), content: SizedBox( width: 380, child: Text('$content', style: const TextStyle(color: Color(0xFFE74C3C), fontSize: 13, height: 1.9)), ), actions: [ TextButton( onPressed: () => Navigator.pop(context), child: const Text('知道了', style: TextStyle(color: Color(0xFF2B7FFF))), ), ], ), ); } void _showToast2(BuildContext context, String msg) { final overlay = Overlay.of(context); final entry = OverlayEntry( builder: (context) => Positioned( top: 60, left: MediaQuery.of(context).size.width / 2 - 140, child: Material( color: Colors.transparent, child: Container( width: 280, padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12), decoration: BoxDecoration( color: AppColors.green, borderRadius: BorderRadius.circular(6), ), child: Row( children: [ const Icon(Icons.check_circle, color: Colors.white, size: 18), const SizedBox(width: 8), Expanded( child: Text(msg, style: const TextStyle(color: Colors.white, fontSize: 13)), ), ], ), ), ), ), ); overlay.insert(entry); Future.delayed(const Duration(seconds: 2), () => entry.remove()); } Widget _sideIcon( BuildContext context, IconData icon, String label, VoidCallback onTap) { return InkWell( onTap: onTap, child: SizedBox( width: 52, child: Column( children: [ Icon(icon, size: 20, color: AppColors.textGray), const SizedBox(height: 4), Text(label, style: const TextStyle( color: AppColors.textGray, fontSize: 10)), ], ), ), ); } } /// 右侧浮动栏:我要投诉/会员激活 class _RightSidebar extends StatelessWidget { final VoidCallback onActivate; const _RightSidebar({required this.onActivate}); @override Widget build(BuildContext context) { return Container( decoration: BoxDecoration( color: const Color(0xFF191C2B).withOpacity(0.95), borderRadius: const BorderRadius.horizontal(left: Radius.circular(8)), border: Border.all(color: AppColors.bgLighter), ), padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 4), child: Column( mainAxisSize: MainAxisSize.min, children: [ _sideIcon(Icons.local_fire_department, '我要投诉', AppColors.red, () { _toast(context, '投诉已记录,客服将在工作时间内联系您'); }), const SizedBox(height: 14), _sideIcon(Icons.card_giftcard, '会员激活', AppColors.yellow, onActivate), ], ), ); } void _toast(BuildContext context, String msg) { final overlay = Overlay.of(context); final entry = OverlayEntry( builder: (context) => Positioned( top: 60, left: MediaQuery.of(context).size.width / 2 - 140, child: Material( color: Colors.transparent, child: Container( width: 280, padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12), decoration: BoxDecoration( color: AppColors.green, borderRadius: BorderRadius.circular(6), ), child: Row( children: [ const Icon(Icons.check_circle, color: Colors.white, size: 18), const SizedBox(width: 8), Expanded( child: Text(msg, style: const TextStyle(color: Colors.white, fontSize: 13)), ), ], ), ), ), ), ); overlay.insert(entry); Future.delayed(const Duration(seconds: 2), () => entry.remove()); } Widget _sideIcon(IconData icon, String label, Color color, VoidCallback onTap) { return InkWell( onTap: onTap, child: SizedBox( width: 52, child: Column( children: [ Icon(icon, size: 20, color: color), const SizedBox(height: 4), Text(label, style: TextStyle(color: color, fontSize: 10)), ], ), ), ); } }