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'; import 'package:fluttergamebox/store/app_state.dart'; import 'package:fluttergamebox/widgets/auth_dialogs.dart'; import 'package:fluttergamebox/widgets/window_controls.dart'; class LoginPage extends StatefulWidget { const LoginPage({super.key}); @override State createState() => _LoginPageState(); } class _LoginPageState extends State { final _phoneCtrl = TextEditingController(); final _pwdCtrl = TextEditingController(); bool _loading = false; String? _error; // 检查更新提示 void _checkUpdate() { showToast(context, '当前已是最新版本!'); } Future _login() async { final phone = _phoneCtrl.text.trim(); final pwd = _pwdCtrl.text; if (phone.isEmpty || pwd.isEmpty) { setState(() => _error = '请输入账号和密码'); return; } setState(() { _loading = true; _error = null; }); try { final res = await ApiService.login(phone, pwd); if (!mounted) return; if (res['code'] == 0) { await ApiClient.saveToken(res['data']['token']); await AppState.instance.refreshUserInfo(); // 公告改为进入首页时统一弹出(见 main_shell) } else { setState(() => _error = res['msg'] ?? '登录失败'); } } catch (e) { setState(() => _error = '网络连接失败,请检查网络'); } finally { if (mounted) setState(() => _loading = false); } } @override Widget build(BuildContext context) { return Scaffold( body: Column( children: [ // 顶部窗口控制条(透明悬浮在右上角) SizedBox( height: 36, child: Row( children: [ const Spacer(), const WindowControls(dark: false), ], ), ), Expanded( child: Row( children: [ // 左侧宣传图 Expanded( flex: 3, child: Stack( fit: StackFit.expand, children: [ Image.asset( 'assets/images/login_art.png', fit: BoxFit.cover, errorBuilder: (_, __, ___) => Container( color: const Color(0xFF1B1E2E), child: const Center( child: Text('游趣电玩', style: TextStyle( color: AppColors.pink, fontSize: 40, fontWeight: FontWeight.bold)), ), ), ), // 底部渐变 Container( decoration: BoxDecoration( gradient: LinearGradient( begin: Alignment.centerLeft, end: Alignment.centerRight, colors: [ Colors.transparent, const Color(0xFF14161F).withOpacity(0.9), ], ), ), ), ], ), ), // 右侧登录面板 Container( width: 400, color: const Color(0xFF1B1E2E), padding: const EdgeInsets.symmetric( horizontal: 44, vertical: 30), child: Column( children: [ const Spacer(flex: 2), // Logo Container( width: 88, height: 88, decoration: BoxDecoration( shape: BoxShape.circle, boxShadow: [ BoxShadow( color: AppColors.pink.withOpacity(0.4), blurRadius: 40, spreadRadius: 5, ), ], ), child: ClipOval( child: Image.asset( 'assets/images/logo.png', fit: BoxFit.cover, errorBuilder: (_, __, ___) => Container( color: AppColors.pink, child: const Center( child: Text('99', style: TextStyle( color: Colors.white, fontSize: 32, fontWeight: FontWeight.bold)), ), ), ), ), ), const SizedBox(height: 36), // 账号 _InputField( controller: _phoneCtrl, icon: Icons.person_outline, hint: '请输入账号', ), const SizedBox(height: 16), // 密码 _InputField( controller: _pwdCtrl, icon: Icons.lock_outline, hint: '请输入密码', obscure: true, ), const SizedBox(height: 10), if (_error != null) Padding( padding: const EdgeInsets.only(bottom: 8), child: Text(_error!, style: const TextStyle( color: AppColors.red, fontSize: 13)), ), // 找回密码 / 注册账号 Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ _LinkText('找回密码', onTap: () => showForgotDialog(context)), _LinkText('注册账号', onTap: () => showRegisterDialog(context)), ], ), const SizedBox(height: 22), // 登录按钮 SizedBox( width: double.infinity, height: 46, child: ElevatedButton( onPressed: _loading ? null : _login, style: ElevatedButton.styleFrom( backgroundColor: AppColors.accent, foregroundColor: Colors.white, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(6)), elevation: 0, ), child: _loading ? const SizedBox( width: 20, height: 20, child: CircularProgressIndicator( strokeWidth: 2, color: Colors.white)) : const Text('登录', style: TextStyle(fontSize: 16)), ), ), const Spacer(flex: 3), // 版本信息 Row( mainAxisAlignment: MainAxisAlignment.center, children: [ const Text('客户端版本: V2.2.5', style: TextStyle( color: AppColors.textDim, fontSize: 12)), const SizedBox(width: 8), GestureDetector( onTap: _checkUpdate, child: const Text('检查更新', style: TextStyle( color: AppColors.accent, fontSize: 12)), ), ], ), const SizedBox(height: 10), ], ), ), ], ), ), ], ), ); } } class _InputField extends StatefulWidget { final TextEditingController controller; final IconData icon; final String hint; final bool obscure; const _InputField({ required this.controller, required this.icon, required this.hint, this.obscure = false, }); @override State<_InputField> createState() => _InputFieldState(); } class _InputFieldState extends State<_InputField> { bool _obscure = false; @override void initState() { super.initState(); _obscure = widget.obscure; } @override Widget build(BuildContext context) { return Container( height: 46, decoration: BoxDecoration( color: const Color(0xFF14161F), borderRadius: BorderRadius.circular(6), border: Border.all(color: const Color(0xFF333750)), ), child: Row( children: [ const SizedBox(width: 12), Icon(widget.icon, color: AppColors.textGray, size: 20), const SizedBox(width: 10), Expanded( child: TextField( controller: widget.controller, obscureText: _obscure, style: const TextStyle(color: Colors.white, fontSize: 14), decoration: InputDecoration( hintText: widget.hint, hintStyle: const TextStyle( color: AppColors.textDim, fontSize: 13), border: InputBorder.none, isDense: true, ), ), ), if (widget.obscure) IconButton( icon: Icon( _obscure ? Icons.visibility_off_outlined : Icons.visibility_outlined, color: AppColors.textDim, size: 18), onPressed: () => setState(() => _obscure = !_obscure), ), const SizedBox(width: 8), ], ), ); } } class _LinkText extends StatelessWidget { final String text; final VoidCallback? onTap; const _LinkText(this.text, {this.onTap}); @override Widget build(BuildContext context) { return GestureDetector( onTap: onTap, child: Text(text, style: const TextStyle(color: AppColors.textGray, fontSize: 13)), ); } } /// 全局 Toast void showToast(BuildContext context, String msg, {bool error = false}) { 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: error ? AppColors.red : AppColors.green, borderRadius: BorderRadius.circular(6), boxShadow: [ BoxShadow( color: Colors.black.withOpacity(0.4), blurRadius: 10) ], ), child: Row( children: [ Icon(error ? Icons.error_outline : 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()); }