import 'package:flutter/material.dart'; import 'package:flutter_spinkit/flutter_spinkit.dart'; import 'screens/improved_home_screen.dart'; import 'register.dart'; import 'services/auth_service.dart'; class LoginScreen extends StatefulWidget { const LoginScreen({super.key}); @override State createState() => _LoginScreenState(); } class _LoginScreenState extends State { final _formKey = GlobalKey(); final _emailController = TextEditingController(); final _passwordController = TextEditingController(); final _authService = AuthService(); bool _obscurePassword = true; bool _isLoading = false; @override void dispose() { _emailController.dispose(); _passwordController.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: const Color(0xFFF5F5F5), body: Column( children: [ // Header Container( width: double.infinity, padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16), decoration: const BoxDecoration( color: Color(0xFF1565C0), // Dark blue ), child: SafeArea( bottom: false, child: Row( children: [ IconButton( icon: const Icon(Icons.arrow_back, color: Colors.white), onPressed: () { Navigator.pop(context); }, ), const Spacer(), Container( width: 40, height: 40, decoration: const BoxDecoration( color: Colors.white, shape: BoxShape.circle, ), child: const Icon(Icons.person, color: Color(0xFF1565C0)), ), ], ), ), ), // Main Content Expanded( child: Container( color: Colors.white, child: SingleChildScrollView( padding: const EdgeInsets.all(24.0), child: Form( key: _formKey, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ // Title Section const Text( 'LOGIN', style: TextStyle( fontSize: 32, fontWeight: FontWeight.bold, color: Colors.black87, ), ), const SizedBox(height: 8), Container(height: 1, color: Colors.grey[300]), const SizedBox(height: 24), // Section Heading RichText( text: const TextSpan( style: TextStyle( fontSize: 18, fontWeight: FontWeight.w600, color: Colors.black87, ), children: [ TextSpan(text: 'Masuk ke '), TextSpan( text: 'Akun', style: TextStyle(color: Color(0xFF1565C0)), ), TextSpan(text: ' Anda'), ], ), ), const SizedBox(height: 32), // Email Field _buildTextField( label: 'Email', controller: _emailController, keyboardType: TextInputType.emailAddress, prefixIcon: Icons.email_outlined, ), const SizedBox(height: 20), // Password Field _buildTextField( label: 'Password', controller: _passwordController, obscureText: _obscurePassword, prefixIcon: Icons.lock_outline, suffixIcon: IconButton( icon: Icon( _obscurePassword ? Icons.visibility_outlined : Icons.visibility_off_outlined, color: const Color(0xFF1565C0), ), onPressed: () { setState(() { _obscurePassword = !_obscurePassword; }); }, ), ), const SizedBox(height: 32), // Login Button SizedBox( width: double.infinity, child: _buildActionButton( label: 'LOGIN', onPressed: _isLoading ? null : _handleLogin, ), ), const SizedBox(height: 24), // Info Aplikasi untuk Mahasiswa Polije _buildInfoCard(), const SizedBox(height: 16), // Register Link Row( mainAxisAlignment: MainAxisAlignment.center, children: [ const Text( 'Belum punya akun? ', style: TextStyle( fontSize: 14, color: Colors.black87, ), ), GestureDetector( onTap: () { Navigator.push( context, MaterialPageRoute( builder: (context) => const RegisterScreen(), ), ); }, child: const Text( 'Daftar', style: TextStyle( fontSize: 14, fontWeight: FontWeight.w600, color: Color(0xFF1565C0), ), ), ), ], ), const SizedBox(height: 20), ], ), ), ), ), ), ], ), ); } Future _handleLogin() async { print('=== LOGIN BUTTON CLICKED ==='); print('FormKey currentState: ${_formKey.currentState}'); if (_formKey.currentState == null) { print('ERROR: FormKey currentState is null!'); ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Error: Form not initialized')), ); return; } if (!_formKey.currentState!.validate()) { print('Form validation failed'); return; } print('Form validated, attempting login...'); print('Email: ${_emailController.text.trim()}'); setState(() => _isLoading = true); final result = await _authService.login( email: _emailController.text.trim(), password: _passwordController.text, ); print('Login result: $result'); setState(() => _isLoading = false); if (!mounted) return; if (result['success']) { print('Login successful, navigating to home...'); Navigator.pushReplacement( context, MaterialPageRoute(builder: (context) => const ImprovedHomeScreen()), ); } else { print('Login failed: ${result['message']}'); ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text(result['message'] ?? 'Login gagal'), backgroundColor: Colors.red, ), ); } } Widget _buildTextField({ required String label, required TextEditingController controller, TextInputType? keyboardType, bool obscureText = false, IconData? prefixIcon, Widget? suffixIcon, }) { return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( label, style: const TextStyle( fontSize: 15, fontWeight: FontWeight.w500, color: Colors.black87, ), ), const SizedBox(height: 8), TextFormField( controller: controller, keyboardType: keyboardType, obscureText: obscureText, validator: (value) { if (value == null || value.isEmpty) { return 'Field ini tidak boleh kosong'; } if (keyboardType == TextInputType.emailAddress) { if (!value.contains('@')) { return 'Email tidak valid'; } } return null; }, decoration: InputDecoration( prefixIcon: prefixIcon != null ? Icon(prefixIcon, color: const Color(0xFF1565C0)) : null, suffixIcon: suffixIcon, filled: true, fillColor: Colors.white, border: OutlineInputBorder( borderRadius: BorderRadius.circular(8), borderSide: const BorderSide( color: Color(0xFF1565C0), width: 1.5, ), ), enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(8), borderSide: const BorderSide( color: Color(0xFF1565C0), width: 1.5, ), ), focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(8), borderSide: const BorderSide(color: Color(0xFF1565C0), width: 2), ), contentPadding: const EdgeInsets.symmetric( horizontal: 16, vertical: 14, ), ), ), ], ); } Widget _buildInfoCard() { return Container( padding: const EdgeInsets.all(20), decoration: BoxDecoration( gradient: LinearGradient( begin: Alignment.topLeft, end: Alignment.bottomRight, colors: [ const Color(0xFF1565C0).withValues(alpha: 0.1), const Color(0xFF1565C0).withValues(alpha: 0.05), ], ), borderRadius: BorderRadius.circular(12), border: Border.all( color: const Color(0xFF1565C0).withValues(alpha: 0.3), width: 1.5, ), ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ Container( padding: const EdgeInsets.all(10), decoration: BoxDecoration( color: const Color(0xFF1565C0), borderRadius: BorderRadius.circular(10), ), child: const Icon(Icons.school, color: Colors.white, size: 24), ), const SizedBox(width: 12), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Text( 'Khusus untuk Mahasiswa', style: TextStyle( fontSize: 16, fontWeight: FontWeight.bold, color: Color(0xFF1565C0), ), ), const Text( 'Politeknik Negeri Jember', style: TextStyle( fontSize: 14, fontWeight: FontWeight.w600, color: Colors.black87, ), ), ], ), ), ], ), const SizedBox(height: 16), Container( padding: const EdgeInsets.all(12), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(8), ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ _buildInfoItem( Icons.location_city, 'Temukan Kontrakan Terbaik', 'Dapatkan rekomendasi kontrakan terdekat dari kampus Polije dengan harga terjangkau dan fasilitas lengkap', ), const SizedBox(height: 12), _buildInfoItem( Icons.local_laundry_service, 'Cari Laundry Terpercaya', 'Temukan jasa laundry terdekat dengan kualitas terbaik untuk kebutuhan harian Anda', ), const SizedBox(height: 12), _buildInfoItem( Icons.star, 'Rekomendasi Terpercaya', 'Sistem akan memberikan rekomendasi terbaik berdasarkan preferensi dan kebutuhan Anda', ), ], ), ), const SizedBox(height: 12), Container( padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8), decoration: BoxDecoration( color: const Color(0xFF1565C0).withValues(alpha: 0.1), borderRadius: BorderRadius.circular(8), ), child: Row( children: [ const Icon( Icons.info_outline, color: Color(0xFF1565C0), size: 18, ), const SizedBox(width: 8), Expanded( child: Text( 'Aplikasi ini menggunakan metode SAW untuk memberikan rekomendasi terbaik', style: TextStyle( fontSize: 12, color: Colors.grey[700], fontStyle: FontStyle.italic, ), ), ), ], ), ), ], ), ); } Widget _buildInfoItem(IconData icon, String title, String description) { return Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Icon(icon, color: const Color(0xFF1565C0), size: 20), const SizedBox(width: 12), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( title, style: const TextStyle( fontSize: 13, fontWeight: FontWeight.w600, color: Colors.black87, ), ), const SizedBox(height: 4), Text( description, style: TextStyle( fontSize: 12, color: Colors.grey[600], height: 1.4, ), ), ], ), ), ], ); } Widget _buildActionButton({ required String label, required VoidCallback? onPressed, }) { return ElevatedButton( onPressed: onPressed, style: ElevatedButton.styleFrom( backgroundColor: const Color(0xFF1565C0), foregroundColor: Colors.white, padding: const EdgeInsets.symmetric(vertical: 14), shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)), elevation: 0, disabledBackgroundColor: Colors.grey, ), child: _isLoading ? const SpinKitThreeBounce(color: Colors.white, size: 20) : Text( label, style: const TextStyle( fontSize: 15, fontWeight: FontWeight.w600, letterSpacing: 0.5, ), ), ); } }