import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import '../services/auth_service.dart'; class LoginScreen extends StatefulWidget { @override _LoginScreenState createState() => _LoginScreenState(); } class _LoginScreenState extends State { final _userCtrl = TextEditingController(); final _pwCtrl = TextEditingController(); bool _loading = false; @override Widget build(BuildContext context) { final auth = Provider.of(context); // Auto-redirect if already logged in if (auth.token != null) { Future.microtask(() => Navigator.pushReplacementNamed(context, '/profile')); } return Scaffold( body: Center( child: Padding( padding: const EdgeInsets.all(24.0), child: Column( mainAxisSize: MainAxisSize.min, children: [ Text('Bejelentkezés', style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold)), SizedBox(height: 16), TextField(controller: _userCtrl, decoration: InputDecoration(labelText: 'Felhasználó')), SizedBox(height: 8), TextField(controller: _pwCtrl, decoration: InputDecoration(labelText: 'Jelszó'), obscureText: true), SizedBox(height: 16), ElevatedButton( onPressed: _loading ? null : () async { final username = _userCtrl.text.trim(); final password = _pwCtrl.text.trim(); if (username.isEmpty || password.isEmpty) { ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Kérlek add meg a felhasználónevet és jelszót'))); return; } setState(() { _loading = true; }); final ok = await auth.login(username, password); setState(() { _loading = false; }); if (ok) Navigator.pushReplacementNamed(context, '/home'); else ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Bejelentkezés sikertelen'))); }, child: _loading ? SizedBox(height: 16, width: 16, child: CircularProgressIndicator(strokeWidth: 2)) : Text('Bejelentkezés'), ) ], ), ), ), ); } }