import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../providers/auth_provider.dart'; import '../widgets/widgets.dart'; import '../utils/validators.dart'; import 'register_page.dart'; /// Login page with email and password authentication class LoginPage extends ConsumerStatefulWidget { const LoginPage({super.key}); @override ConsumerState createState() => _LoginPageState(); } class _LoginPageState extends ConsumerState { final _formKey = GlobalKey(); final _emailController = TextEditingController(text: 'admin@retailpos.com'); final _passwordController = TextEditingController(text: 'Admin123!'); bool _rememberMe = false; @override void dispose() { _emailController.dispose(); _passwordController.dispose(); super.dispose(); } Future _handleLogin() async { // Dismiss keyboard FocusScope.of(context).unfocus(); // Validate form if (!_formKey.currentState!.validate()) { return; } // Attempt login final success = await ref.read(authProvider.notifier).login( email: _emailController.text.trim(), password: _passwordController.text, ); if (!mounted) return; // Show error if login failed if (!success) { final authState = ref.read(authProvider); if (authState.errorMessage != null) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text(authState.errorMessage!), backgroundColor: Theme.of(context).colorScheme.error, behavior: SnackBarBehavior.floating, action: SnackBarAction( label: 'Dismiss', textColor: Colors.white, onPressed: () {}, ), ), ); } } // Navigation is handled by AuthWrapper } void _navigateToRegister() { Navigator.of(context).push( MaterialPageRoute( builder: (context) => const RegisterPage(), ), ); } void _handleForgotPassword() { // TODO: Implement forgot password functionality ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('Forgot password feature coming soon!'), behavior: SnackBarBehavior.floating, ), ); } @override Widget build(BuildContext context) { final theme = Theme.of(context); final authState = ref.watch(authProvider); final isLoading = authState.isLoading; return GestureDetector( onTap: () => FocusScope.of(context).unfocus(), child: Scaffold( body: SafeArea( child: Center( child: SingleChildScrollView( padding: const EdgeInsets.all(24.0), child: ConstrainedBox( constraints: const BoxConstraints(maxWidth: 400), child: Form( key: _formKey, child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ // Header with logo and title const AuthHeader( title: 'Retail POS', subtitle: 'Welcome back! Please login to continue.', ), const SizedBox(height: 48), // Email field AuthTextField( controller: _emailController, label: 'Email', hint: 'Enter your email', keyboardType: TextInputType.emailAddress, textInputAction: TextInputAction.next, prefixIcon: Icons.email_outlined, validator: AuthValidators.validateEmail, enabled: !isLoading, autofocus: true, ), const SizedBox(height: 16), // Password field PasswordField( controller: _passwordController, label: 'Password', hint: 'Enter your password', textInputAction: TextInputAction.done, validator: AuthValidators.validateLoginPassword, onFieldSubmitted: (_) => _handleLogin(), enabled: !isLoading, ), const SizedBox(height: 8), // Remember me and forgot password row Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ // Remember me checkbox Row( children: [ Checkbox( value: _rememberMe, onChanged: isLoading ? null : (value) { setState(() { _rememberMe = value ?? false; }); }, ), Text( 'Remember me', style: theme.textTheme.bodyMedium, ), ], ), // Forgot password link TextButton( onPressed: isLoading ? null : _handleForgotPassword, child: Text( 'Forgot Password?', style: theme.textTheme.bodyMedium?.copyWith( color: theme.colorScheme.primary, fontWeight: FontWeight.w600, ), ), ), ], ), const SizedBox(height: 24), // Login button AuthButton( onPressed: _handleLogin, text: 'Login', isLoading: isLoading, ), const SizedBox(height: 24), // Divider Row( children: [ Expanded( child: Divider( color: theme.colorScheme.onSurface.withOpacity(0.2), ), ), Padding( padding: const EdgeInsets.symmetric(horizontal: 16), child: Text( 'OR', style: theme.textTheme.bodySmall?.copyWith( color: theme.colorScheme.onSurface.withOpacity(0.5), ), ), ), Expanded( child: Divider( color: theme.colorScheme.onSurface.withOpacity(0.2), ), ), ], ), const SizedBox(height: 24), // Register link Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( "Don't have an account? ", style: theme.textTheme.bodyMedium, ), TextButton( onPressed: isLoading ? null : _navigateToRegister, child: Text( 'Register', style: theme.textTheme.bodyMedium?.copyWith( color: theme.colorScheme.primary, fontWeight: FontWeight.bold, ), ), ), ], ), ], ), ), ), ), ), ), ), ); } }