import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:go_router/go_router.dart'; import '../../di/auth_dependency_injection.dart'; import '../widgets/login_form.dart'; /// Login page for user authentication /// /// Displays login form and handles authentication flow class LoginPage extends ConsumerStatefulWidget { const LoginPage({super.key}); @override ConsumerState createState() => _LoginPageState(); } class _LoginPageState extends ConsumerState { @override void initState() { super.initState(); // Check authentication status on page load WidgetsBinding.instance.addPostFrameCallback((_) { _checkAuthStatus(); }); } /// Check if user is already authenticated Future _checkAuthStatus() async { ref.read(authProvider.notifier).checkAuthStatus(); } /// Handle login button press void _handleLogin(String username, String password) { ref.read(authProvider.notifier).login(username, password); } @override Widget build(BuildContext context) { final theme = Theme.of(context); final authState = ref.watch(authProvider); final isLoading = authState.isLoading; final error = authState.error; // Listen for authentication state changes ref.listen(authProvider, (previous, next) { if (next.isAuthenticated) { // Navigate to warehouses page on successful login context.go('/warehouses'); } }); return Scaffold( body: SafeArea( child: Center( child: SingleChildScrollView( padding: const EdgeInsets.all(24.0), child: ConstrainedBox( constraints: const BoxConstraints(maxWidth: 400), child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ // App logo/icon Icon( Icons.warehouse_outlined, size: 80, color: theme.colorScheme.primary, ), const SizedBox(height: 24), // App title Text( 'Warehouse Manager', style: theme.textTheme.headlineMedium?.copyWith( fontWeight: FontWeight.bold, color: theme.colorScheme.onSurface, ), textAlign: TextAlign.center, ), const SizedBox(height: 8), // Subtitle Text( 'Login to continue', style: theme.textTheme.bodyLarge?.copyWith( color: theme.colorScheme.onSurface.withOpacity(0.6), ), textAlign: TextAlign.center, ), const SizedBox(height: 48), // Error message (show before form) if (error != null) Container( padding: const EdgeInsets.all(12), decoration: BoxDecoration( color: theme.colorScheme.errorContainer, borderRadius: BorderRadius.circular(8), ), child: Row( children: [ Icon( Icons.error_outline, color: theme.colorScheme.error, size: 20, ), const SizedBox(width: 8), Expanded( child: Text( error, style: theme.textTheme.bodyMedium?.copyWith( color: theme.colorScheme.error, ), ), ), ], ), ), if (error != null) const SizedBox(height: 16), // Login form (includes button) LoginForm( onSubmit: _handleLogin, isLoading: isLoading, ), const SizedBox(height: 24), // Additional info or version Text( 'Version 1.0.0', style: theme.textTheme.bodySmall?.copyWith( color: theme.colorScheme.onSurface.withOpacity(0.4), ), textAlign: TextAlign.center, ), ], ), ), ), ), ), ); } }