import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../../providers/auth_provider.dart'; import 'signup_screen.dart'; import 'reset_password_screen.dart'; class LoginScreen extends ConsumerStatefulWidget { const LoginScreen({super.key}); @override ConsumerState createState() => _LoginScreenState(); } class _LoginScreenState extends ConsumerState { final _formKey = GlobalKey(); final _emailController = TextEditingController(); final _passwordController = TextEditingController(); bool _isLoading = false; bool _obscurePassword = true; @override void dispose() { _emailController.dispose(); _passwordController.dispose(); super.dispose(); } Future _handleLogin() async { if (!_formKey.currentState!.validate()) return; setState(() => _isLoading = true); try { final login = ref.read(loginProvider); await login( email: _emailController.text.trim(), password: _passwordController.text, ); // Navigation håndteres automatisk av authStateProvider i main.dart } catch (e) { if (mounted) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text(e.toString()), backgroundColor: Colors.red, ), ); } } finally { if (mounted) { setState(() => _isLoading = false); } } } @override Widget build(BuildContext context) { return Scaffold( body: SafeArea( child: Center( child: SingleChildScrollView( padding: const EdgeInsets.all(24.0), child: Form( key: _formKey, child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ // Logo/Ikon Icon( Icons.access_time_rounded, size: 80, color: Theme.of(context).colorScheme.primary, ), const SizedBox(height: 16), // Tittel Text( 'TimeReg', style: Theme.of(context).textTheme.headlineLarge?.copyWith( fontWeight: FontWeight.bold, ), textAlign: TextAlign.center, ), const SizedBox(height: 8), Text( 'Registrer arbeidstid med AML-kontroll', style: Theme.of(context).textTheme.bodyMedium?.copyWith( color: Colors.grey, ), textAlign: TextAlign.center, ), const SizedBox(height: 48), // E-post felt TextFormField( controller: _emailController, keyboardType: TextInputType.emailAddress, decoration: const InputDecoration( labelText: 'E-post', prefixIcon: Icon(Icons.email_outlined), ), validator: (value) { if (value == null || value.isEmpty) { return 'Vennligst skriv inn e-post'; } if (!value.contains('@')) { return 'Vennligst skriv inn en gyldig e-post'; } return null; }, ), const SizedBox(height: 16), // Passord felt TextFormField( controller: _passwordController, obscureText: _obscurePassword, decoration: InputDecoration( labelText: 'Passord', prefixIcon: const Icon(Icons.lock_outlined), suffixIcon: IconButton( icon: Icon( _obscurePassword ? Icons.visibility_outlined : Icons.visibility_off_outlined, ), onPressed: () { setState(() { _obscurePassword = !_obscurePassword; }); }, ), ), validator: (value) { if (value == null || value.isEmpty) { return 'Vennligst skriv inn passord'; } return null; }, ), const SizedBox(height: 8), // Glemt passord Align( alignment: Alignment.centerRight, child: TextButton( onPressed: () { Navigator.push( context, MaterialPageRoute( builder: (context) => const ResetPasswordScreen(), ), ); }, child: const Text('Glemt passord?'), ), ), const SizedBox(height: 24), // Logg inn knapp FilledButton( onPressed: _isLoading ? null : _handleLogin, style: FilledButton.styleFrom( padding: const EdgeInsets.symmetric(vertical: 16), ), child: _isLoading ? const SizedBox( height: 20, width: 20, child: CircularProgressIndicator(strokeWidth: 2), ) : const Text('Logg inn'), ), const SizedBox(height: 16), // Opprett konto OutlinedButton( onPressed: _isLoading ? null : () { Navigator.push( context, MaterialPageRoute( builder: (context) => const SignUpScreen(), ), ); }, style: OutlinedButton.styleFrom( padding: const EdgeInsets.symmetric(vertical: 16), ), child: const Text('Opprett konto'), ), ], ), ), ), ), ), ); } }