import "package:flutter/material.dart"; import "package:provider/provider.dart"; import "../app/app_router.dart"; import "../app/app_state.dart"; import "../services/api.dart"; import "../services/auth_storage.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 _isLoading = false; String? _errorMessage; @override void dispose() { _usernameController.dispose(); _passwordController.dispose(); super.dispose(); } Future _handleLogin() async { if (!_formKey.currentState!.validate()) { return; } setState(() { _isLoading = true; _errorMessage = null; }); try { final result = await Api.login( username: _usernameController.text.trim(), password: _passwordController.text, ); if (!mounted) return; // Save credentials for persistent login await AuthStorage.saveAuth( userId: result.userId, token: result.token, ); // Set the auth token on the API class Api.setAuthToken(result.token); final appState = context.read(); appState.setUserId(result.userId); // Go back to previous screen (menu) or splash if no previous route if (Navigator.of(context).canPop()) { Navigator.of(context).pop(); } else { // No previous route - go to splash which will auto-navigate based on beacon detection Navigator.of(context).pushReplacementNamed(AppRoutes.splash); } } catch (e) { if (!mounted) return; setState(() { _errorMessage = e.toString(); _isLoading = false; }); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text("Login"), ), body: Center( child: SingleChildScrollView( padding: const EdgeInsets.all(24), child: ConstrainedBox( constraints: const BoxConstraints(maxWidth: 400), child: Form( key: _formKey, child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ const Text( "PAYFRIT", textAlign: TextAlign.center, style: TextStyle( fontSize: 32, fontWeight: FontWeight.bold, letterSpacing: 2, ), ), const SizedBox(height: 8), const Text( "Sign in to order", textAlign: TextAlign.center, style: TextStyle( fontSize: 16, color: Colors.grey, ), ), const SizedBox(height: 48), TextFormField( controller: _usernameController, decoration: const InputDecoration( labelText: "Email or Phone Number", border: OutlineInputBorder(), prefixIcon: Icon(Icons.person), ), keyboardType: TextInputType.emailAddress, textInputAction: TextInputAction.next, enabled: !_isLoading, validator: (value) { if (value == null || value.trim().isEmpty) { return "Please enter your email or phone number"; } return null; }, ), const SizedBox(height: 16), TextFormField( controller: _passwordController, decoration: const InputDecoration( labelText: "Password", border: OutlineInputBorder(), prefixIcon: Icon(Icons.lock), ), obscureText: true, textInputAction: TextInputAction.done, enabled: !_isLoading, onFieldSubmitted: (_) => _handleLogin(), validator: (value) { if (value == null || value.isEmpty) { return "Please enter your password"; } return null; }, ), const SizedBox(height: 24), if (_errorMessage != null) Container( padding: const EdgeInsets.all(12), margin: const EdgeInsets.only(bottom: 16), decoration: BoxDecoration( color: Colors.red.shade50, borderRadius: BorderRadius.circular(8), border: Border.all(color: Colors.red.shade300), ), child: Row( children: [ Icon(Icons.error_outline, color: Colors.red.shade700), const SizedBox(width: 12), Expanded( child: Text( _errorMessage!, style: TextStyle(color: Colors.red.shade900), ), ), ], ), ), FilledButton( onPressed: _isLoading ? null : _handleLogin, child: _isLoading ? const SizedBox( height: 20, width: 20, child: CircularProgressIndicator( strokeWidth: 2, valueColor: AlwaysStoppedAnimation(Colors.white), ), ) : const Text("Login"), ), const SizedBox(height: 16), TextButton( onPressed: _isLoading ? null : () { Navigator.of(context) .pushReplacementNamed(AppRoutes.signup); }, child: const Text("Don't have an account? Sign Up"), ), ], ), ), ), ), ), ); } }