// lib/screens/login_screen.dart import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import '../services/api_service.dart'; import '../services/session_manager.dart'; import 'main_screen.dart'; class LoginScreen extends StatefulWidget { const LoginScreen({super.key}); @override State createState() => _LoginScreenState(); } class _LoginScreenState extends State { final _usernameController = TextEditingController(text: 'gast'); final _passwordController = TextEditingController(text: 'gast'); bool _isLoading = false; bool _obscurePassword = true; Future _login() async { if (_usernameController.text.isEmpty || _passwordController.text.isEmpty) { _showError('Bitte Benutzername und Passwort eingeben'); return; } setState(() => _isLoading = true); try { final apiService = ApiService(); final response = await apiService.login( _usernameController.text.trim(), _passwordController.text, ); if (response['success'] == true && response['userID'] != null && response['session'] != null) { final sessionManager = Provider.of(context, listen: false); sessionManager.setSession(response['userID'], response['session']); if (mounted) { Navigator.pushReplacement( context, MaterialPageRoute(builder: (_) => const MainScreen()), ); } } else { _showError(response['error'] ?? 'Anmeldung fehlgeschlagen'); } } catch (e) { _showError('Verbindungsfehler: $e'); } finally { if (mounted) setState(() => _isLoading = false); } } void _showError(String message) { ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text(message), backgroundColor: Colors.red), ); } @override Widget build(BuildContext context) { return Scaffold( body: Center( child: SingleChildScrollView( padding: const EdgeInsets.all(24), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Icon(Icons.school, size: 80, color: Colors.blue), const SizedBox(height: 16), const Text( 'DÜSK', style: TextStyle(fontSize: 48, fontWeight: FontWeight.bold), ), const Text( 'Düsseldorfer Schülerinventar', style: TextStyle(fontSize: 16, color: Colors.grey), ), const SizedBox(height: 48), TextField( controller: _usernameController, decoration: const InputDecoration( labelText: 'Benutzername', border: OutlineInputBorder(), prefixIcon: Icon(Icons.person), ), enabled: !_isLoading, ), const SizedBox(height: 16), TextField( controller: _passwordController, decoration: InputDecoration( labelText: 'Passwort', border: const OutlineInputBorder(), prefixIcon: const Icon(Icons.lock), suffixIcon: IconButton( icon: Icon(_obscurePassword ? Icons.visibility_off : Icons.visibility), onPressed: () => setState(() => _obscurePassword = !_obscurePassword), ), ), obscureText: _obscurePassword, enabled: !_isLoading, ), const SizedBox(height: 24), SizedBox( width: double.infinity, height: 50, child: ElevatedButton( onPressed: _isLoading ? null : _login, child: _isLoading ? const CircularProgressIndicator() : const Text('Anmelden', style: TextStyle(fontSize: 16)), ), ), const SizedBox(height: 24), const Text( 'Server: paul-koop.org | Demozugang: gast/gast', style: TextStyle(fontSize: 12, color: Colors.grey), ), ], ), ), ), ); } }