import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import '../services/connection_service.dart'; import '../services/theme_service.dart'; class LoginScreen extends StatefulWidget { const LoginScreen({super.key}); @override State createState() => _LoginScreenState(); } class _LoginScreenState extends State { final _usernameController = TextEditingController(); final _serverUrlController = TextEditingController(text: 'ws://localhost:8443/ws'); bool _loading = false; String? _error; @override void dispose() { _usernameController.dispose(); _serverUrlController.dispose(); super.dispose(); } Future _connect() async { setState(() { _loading = true; _error = null; }); try { final conn = context.read(); conn.serverUrl = _serverUrlController.text.trim(); await conn.connect(); final username = _usernameController.text.trim(); if (username.isNotEmpty) { conn.authenticate(username: username); } await Future.delayed(const Duration(seconds: 1)); if (mounted) { Navigator.pushReplacementNamed(context, '/home'); } } catch (e) { setState(() => _error = 'Connection failed: $e'); } finally { if (mounted) setState(() => _loading = false); } } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: ThemeService.backgroundPrimary, body: Center( child: SingleChildScrollView( padding: const EdgeInsets.all(32), child: Column( mainAxisSize: MainAxisSize.min, children: [ Container( width: 72, height: 72, decoration: BoxDecoration( color: ThemeService.primary, borderRadius: BorderRadius.circular(18), ), child: const Center( child: Text('JAM', style: TextStyle( color: Colors.white, fontSize: 28, fontWeight: FontWeight.w800)), ), ), const SizedBox(height: 32), const Text( 'Welcome to JustAMessenger', style: TextStyle( color: ThemeService.textPrimary, fontSize: 24, fontWeight: FontWeight.w700, ), ), const SizedBox(height: 8), const Text( 'Connect to a server to get started', style: TextStyle( color: ThemeService.textSecondary, fontSize: 14, ), ), const SizedBox(height: 32), TextField( controller: _serverUrlController, decoration: const InputDecoration( labelText: 'Server URL', hintText: 'ws://localhost:8443/ws', prefixIcon: Icon(Icons.dns_outlined), ), style: const TextStyle(color: ThemeService.textPrimary), ), const SizedBox(height: 16), TextField( controller: _usernameController, decoration: const InputDecoration( labelText: 'Username', hintText: 'Enter your username', prefixIcon: Icon(Icons.person_outline), ), style: const TextStyle(color: ThemeService.textPrimary), textCapitalization: TextCapitalization.none, ), if (_error != null) ...[ const SizedBox(height: 16), Container( padding: const EdgeInsets.all(12), decoration: BoxDecoration( color: ThemeService.danger.withOpacity(0.1), borderRadius: BorderRadius.circular(8), border: Border.all( color: ThemeService.danger.withOpacity(0.3)), ), child: Row( children: [ const Icon(Icons.error_outline, color: ThemeService.danger, size: 20), const SizedBox(width: 8), Expanded( child: Text(_error!, style: const TextStyle( color: ThemeService.danger, fontSize: 13)), ), ], ), ), ], const SizedBox(height: 24), SizedBox( width: double.infinity, height: 48, child: ElevatedButton( onPressed: _loading ? null : _connect, style: ElevatedButton.styleFrom( backgroundColor: ThemeService.primary, foregroundColor: Colors.white, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12), ), elevation: 0, ), child: _loading ? const SizedBox( width: 22, height: 22, child: CircularProgressIndicator( strokeWidth: 2, color: Colors.white, ), ) : const Text('Connect', style: TextStyle( fontSize: 16, fontWeight: FontWeight.w600)), ), ), ], ), ), ), ); } }