import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:go_router/go_router.dart'; import '../../blocs/auth/auth_bloc.dart'; import '../../blocs/auth/auth_event.dart'; import '../../blocs/auth/auth_state.dart'; class LoginScreen extends StatefulWidget { const LoginScreen({super.key}); @override State createState() => _LoginScreenState(); } class _LoginScreenState extends State { final _formKey = GlobalKey(); final _usernameController = TextEditingController(); final _passwordController = TextEditingController(); bool _obscurePassword = true; @override void dispose() { _usernameController.dispose(); _passwordController.dispose(); super.dispose(); } void _handleLogin() { if (_formKey.currentState!.validate()) { context.read().add( LoginRequested( username: _usernameController.text.trim(), password: _passwordController.text, ), ); } } @override Widget build(BuildContext context) { return Scaffold( body: Center( child: SingleChildScrollView( child: Padding( padding: const EdgeInsets.all(24.0), child: ConstrainedBox( constraints: const BoxConstraints(maxWidth: 400), child: BlocConsumer( listener: (context, state) { if (state is Authenticated) { // Navigate to home screen context.go('/'); } else if (state is AuthError) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text(state.message), backgroundColor: Colors.red, ), ); } }, builder: (context, state) { final isLoading = state is AuthLoading; return Card( elevation: 8, child: Padding( padding: const EdgeInsets.all(32.0), child: Form( key: _formKey, child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ // Logo/Title const Icon( Icons.videocam, size: 64, color: Colors.blue, ), const SizedBox(height: 16), Text( 'GeViAPI', style: Theme.of(context).textTheme.headlineMedium?.copyWith( fontWeight: FontWeight.bold, ), textAlign: TextAlign.center, ), const SizedBox(height: 8), Text( 'Video Management System', style: Theme.of(context).textTheme.bodyLarge?.copyWith( color: Colors.grey[600], ), textAlign: TextAlign.center, ), const SizedBox(height: 32), // Username field TextFormField( controller: _usernameController, decoration: const InputDecoration( labelText: 'Username', prefixIcon: Icon(Icons.person), border: OutlineInputBorder(), ), enabled: !isLoading, validator: (value) { if (value == null || value.isEmpty) { return 'Please enter your username'; } return null; }, onFieldSubmitted: (_) => _handleLogin(), ), const SizedBox(height: 16), // Password field TextFormField( controller: _passwordController, obscureText: _obscurePassword, decoration: InputDecoration( labelText: 'Password', prefixIcon: const Icon(Icons.lock), border: const OutlineInputBorder(), suffixIcon: IconButton( icon: Icon( _obscurePassword ? Icons.visibility : Icons.visibility_off, ), onPressed: () { setState(() { _obscurePassword = !_obscurePassword; }); }, ), ), enabled: !isLoading, validator: (value) { if (value == null || value.isEmpty) { return 'Please enter your password'; } return null; }, onFieldSubmitted: (_) => _handleLogin(), ), const SizedBox(height: 24), // Login button ElevatedButton( onPressed: isLoading ? null : _handleLogin, style: ElevatedButton.styleFrom( padding: const EdgeInsets.symmetric(vertical: 16), ), child: isLoading ? const SizedBox( height: 20, width: 20, child: CircularProgressIndicator( strokeWidth: 2, ), ) : const Text( 'Login', style: TextStyle(fontSize: 16), ), ), ], ), ), ), ); }, ), ), ), ), ), ); } }