import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:go_router/go_router.dart'; import '../../../core/router/routes.dart'; import '../../providers/user_providers.dart'; class LoginPage extends ConsumerStatefulWidget { const LoginPage({super.key}); @override ConsumerState createState() => _LoginPageState(); } class _LoginPageState extends ConsumerState { // 设计色彩 - 与导航页面统一 static const Color _orange = Color(0xFFFF6B00); static const Color _darkText = Color(0xFF1F2937); static const Color _grayText = Color(0xFF6B7280); static const Color _lightGray = Color(0xFF9CA3AF); static const Color _bgGray = Color(0xFFF3F4F6); static const Color _borderGray = Color(0xFFE5E7EB); final _formKey = GlobalKey(); final _phoneController = TextEditingController(); final _passwordController = TextEditingController(); final _smsCodeController = TextEditingController(); bool _isPasswordLogin = true; bool _obscurePassword = true; int _countDown = 0; @override void dispose() { _phoneController.dispose(); _passwordController.dispose(); _smsCodeController.dispose(); super.dispose(); } Future _sendSmsCode() async { final phone = _phoneController.text.trim(); if (phone.isEmpty || !RegExp(r'^1[3-9]\d{9}$').hasMatch(phone)) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('请输入正确的手机号')), ); return; } try { await ref.read(userNotifierProvider.notifier).sendSmsCode(phone, 'LOGIN'); setState(() => _countDown = 60); _startCountDown(); if (mounted) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('验证码已发送')), ); } } catch (e) { if (mounted) { ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('发送失败: $e')), ); } } } void _startCountDown() { Future.delayed(const Duration(seconds: 1), () { if (_countDown > 0 && mounted) { setState(() => _countDown--); _startCountDown(); } }); } Future _login() async { if (!_formKey.currentState!.validate()) return; final phone = _phoneController.text.trim(); try { if (_isPasswordLogin) { await ref.read(userNotifierProvider.notifier).loginWithPassword( phone, _passwordController.text, ); } else { await ref.read(userNotifierProvider.notifier).loginWithSms( phone, _smsCodeController.text.trim(), ); } if (mounted) { context.go(Routes.contribution); } } catch (e) { if (mounted) { ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('登录失败: $e')), ); } } } @override Widget build(BuildContext context) { final userState = ref.watch(userNotifierProvider); return Scaffold( backgroundColor: Colors.white, body: SafeArea( child: SingleChildScrollView( padding: const EdgeInsets.all(24), child: Form( key: _formKey, child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ const SizedBox(height: 60), // Logo Center( child: Container( width: 80, height: 80, decoration: BoxDecoration( color: _orange.withOpacity(0.1), borderRadius: BorderRadius.circular(20), ), child: const Icon( Icons.eco, size: 48, color: _orange, ), ), ), const SizedBox(height: 24), const Text( '欢迎回来', style: TextStyle( fontSize: 28, fontWeight: FontWeight.bold, color: _darkText, ), textAlign: TextAlign.center, ), const SizedBox(height: 8), const Text( '登录您的股行账户', style: TextStyle( fontSize: 16, color: _grayText, ), textAlign: TextAlign.center, ), const SizedBox(height: 48), // 登录方式切换 Container( decoration: const BoxDecoration( border: Border( bottom: BorderSide(color: _borderGray, width: 1), ), ), child: Row( children: [ Expanded( child: GestureDetector( onTap: () => setState(() => _isPasswordLogin = true), child: Container( padding: const EdgeInsets.symmetric(vertical: 12), decoration: BoxDecoration( border: Border( bottom: BorderSide( color: _isPasswordLogin ? _orange : Colors.transparent, width: 2, ), ), ), child: Text( '密码登录', textAlign: TextAlign.center, style: TextStyle( fontSize: 16, fontWeight: _isPasswordLogin ? FontWeight.bold : FontWeight.normal, color: _isPasswordLogin ? _orange : _lightGray, ), ), ), ), ), Expanded( child: GestureDetector( onTap: () => setState(() => _isPasswordLogin = false), child: Container( padding: const EdgeInsets.symmetric(vertical: 12), decoration: BoxDecoration( border: Border( bottom: BorderSide( color: !_isPasswordLogin ? _orange : Colors.transparent, width: 2, ), ), ), child: Text( '验证码登录', textAlign: TextAlign.center, style: TextStyle( fontSize: 16, fontWeight: !_isPasswordLogin ? FontWeight.bold : FontWeight.normal, color: !_isPasswordLogin ? _orange : _lightGray, ), ), ), ), ), ], ), ), const SizedBox(height: 24), // 手机号 TextFormField( controller: _phoneController, keyboardType: TextInputType.phone, style: const TextStyle(color: _darkText), decoration: InputDecoration( labelText: '手机号', labelStyle: const TextStyle(color: _grayText), prefixIcon: const Icon(Icons.phone_outlined, color: _grayText), border: OutlineInputBorder( borderRadius: BorderRadius.circular(12), borderSide: const BorderSide(color: _borderGray), ), enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(12), borderSide: const BorderSide(color: _borderGray), ), focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(12), borderSide: const BorderSide(color: _orange, width: 2), ), filled: true, fillColor: Colors.white, ), validator: (value) { if (value == null || value.isEmpty) { return '请输入手机号'; } if (!RegExp(r'^1[3-9]\d{9}$').hasMatch(value)) { return '请输入正确的手机号'; } return null; }, ), const SizedBox(height: 16), // 密码/验证码 if (_isPasswordLogin) TextFormField( controller: _passwordController, obscureText: _obscurePassword, style: const TextStyle(color: _darkText), decoration: InputDecoration( labelText: '密码', labelStyle: const TextStyle(color: _grayText), prefixIcon: const Icon(Icons.lock_outline, color: _grayText), suffixIcon: IconButton( icon: Icon( _obscurePassword ? Icons.visibility_off_outlined : Icons.visibility_outlined, color: _grayText, ), onPressed: () => setState(() => _obscurePassword = !_obscurePassword), ), border: OutlineInputBorder( borderRadius: BorderRadius.circular(12), borderSide: const BorderSide(color: _borderGray), ), enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(12), borderSide: const BorderSide(color: _borderGray), ), focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(12), borderSide: const BorderSide(color: _orange, width: 2), ), filled: true, fillColor: Colors.white, ), validator: (value) { if (value == null || value.isEmpty) { return '请输入密码'; } return null; }, ) else Row( children: [ Expanded( child: TextFormField( controller: _smsCodeController, keyboardType: TextInputType.number, maxLength: 6, style: const TextStyle(color: _darkText), decoration: InputDecoration( labelText: '验证码', labelStyle: const TextStyle(color: _grayText), prefixIcon: const Icon(Icons.sms_outlined, color: _grayText), counterText: '', border: OutlineInputBorder( borderRadius: BorderRadius.circular(12), borderSide: const BorderSide(color: _borderGray), ), enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(12), borderSide: const BorderSide(color: _borderGray), ), focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(12), borderSide: const BorderSide(color: _orange, width: 2), ), filled: true, fillColor: Colors.white, ), validator: (value) { if (value == null || value.isEmpty) { return '请输入验证码'; } if (value.length != 6) { return '验证码为6位'; } return null; }, ), ), const SizedBox(width: 12), SizedBox( width: 120, height: 56, child: ElevatedButton( onPressed: _countDown > 0 ? null : _sendSmsCode, style: ElevatedButton.styleFrom( backgroundColor: _countDown > 0 ? _bgGray : _orange.withOpacity(0.1), foregroundColor: _countDown > 0 ? _lightGray : _orange, elevation: 0, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12), ), ), child: Text( _countDown > 0 ? '${_countDown}s' : '获取验证码', style: const TextStyle(fontSize: 14, fontWeight: FontWeight.w500), ), ), ), ], ), const SizedBox(height: 32), // 登录按钮 SizedBox( height: 52, child: ElevatedButton( onPressed: userState.isLoading ? null : _login, style: ElevatedButton.styleFrom( backgroundColor: _orange, disabledBackgroundColor: _orange.withOpacity(0.5), elevation: 0, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12), ), ), child: userState.isLoading ? const SizedBox( width: 24, height: 24, child: CircularProgressIndicator( strokeWidth: 2, color: Colors.white, ), ) : const Text( '登录', style: TextStyle( fontSize: 18, fontWeight: FontWeight.bold, color: Colors.white, ), ), ), ), const SizedBox(height: 16), // 忘记密码 if (_isPasswordLogin) Align( alignment: Alignment.centerRight, child: TextButton( onPressed: () => context.push(Routes.forgotPassword), child: const Text( '忘记密码?', style: TextStyle(color: _grayText), ), ), ), const SizedBox(height: 8), // 注册入口 Row( mainAxisAlignment: MainAxisAlignment.center, children: [ const Text( '还没有账号?', style: TextStyle(color: _grayText), ), TextButton( onPressed: () => context.push(Routes.register), child: const Text( '立即注册', style: TextStyle( color: _orange, fontWeight: FontWeight.w600, ), ), ), ], ), ], ), ), ), ), ); } }