import 'package:flutter/material.dart'; import '../services/supabase_service.dart'; import '../constants/app_colors.dart'; // <-- IMPORT APP COLORS DITAMBAHKAN class LoginScreen extends StatefulWidget { const LoginScreen({super.key}); @override State createState() => _LoginScreenState(); } class _LoginScreenState extends State { final _emailController = TextEditingController(); final _passwordController = TextEditingController(); bool _isLoading = false; bool _obscurePassword = true; Future _handleLogin() async { if (_emailController.text.trim().isEmpty || _passwordController.text.trim().isEmpty) { _showError('Email dan password tidak boleh kosong!'); return; } setState(() => _isLoading = true); try { await SupabaseService().signIn( _emailController.text.trim(), _passwordController.text.trim(), ); if (mounted) { Navigator.pushReplacementNamed(context, '/home'); } } catch (e) { if (mounted) { final errorStr = e.toString().toLowerCase(); String pesanError = 'Login gagal, coba lagi.'; if (errorStr.contains('invalid_credentials') || errorStr.contains('invalid login credentials') || errorStr.contains('email or password')) { pesanError = 'Email atau password salah!'; } else if (errorStr.contains('not confirmed')) { pesanError = 'Email belum dikonfirmasi, cek inbox kamu.'; } else if (errorStr.contains('network') || errorStr.contains('socket') || errorStr.contains('connection')) { pesanError = 'Tidak ada koneksi internet.'; } else if (errorStr.contains('too many') || errorStr.contains('rate limit')) { pesanError = 'Terlalu banyak percobaan, tunggu sebentar.'; } else if (errorStr.contains('user not found')) { pesanError = 'Email atau password salah!'; } _showError(pesanError); } } finally { if (mounted) setState(() => _isLoading = false); } } void _showError(String pesan) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Row( children: [ const Icon(Icons.error_outline, color: Colors.white), // Tetap putih agar kontras di atas merah const SizedBox(width: 10), Expanded( child: Text( pesan, style: const TextStyle( color: Colors.white, // Tetap putih fontWeight: FontWeight.bold, ), ), ), ], ), // --- BERUBAH: Background SnackBar Error --- backgroundColor: AppColors.error, behavior: SnackBarBehavior.floating, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12), ), margin: const EdgeInsets.all(16), duration: const Duration(seconds: 3), ), ); } @override Widget build(BuildContext context) { return Scaffold( // --- BERUBAH: Background Scaffold Utama --- backgroundColor: AppColors.background, body: SingleChildScrollView( child: Padding( padding: const EdgeInsets.symmetric(horizontal: 30.0, vertical: 80.0), child: Column( children: [ CircleAvatar( radius: 60, // --- BERUBAH: Latar belakang logo --- backgroundColor: AppColors.primary, child: ClipOval( child: Padding( padding: const EdgeInsets.all(8), child: Image.asset( 'assets/icon/kopibaru_removebg.png', width: 120, height: 120, fit: BoxFit.contain, ), ), ), ), const SizedBox(height: 30), const Text( 'Selamat Datang', style: TextStyle( // --- BERUBAH: Warna Judul --- color: AppColors.primary, fontSize: 32, fontWeight: FontWeight.bold), ), const Text( 'Sistem Kendali Pengering Kopi', // --- BERUBAH: Warna Sub-judul --- style: TextStyle(color: AppColors.textSecondary, fontSize: 16), ), const SizedBox(height: 50), Container( padding: const EdgeInsets.all(20), decoration: BoxDecoration( // --- BERUBAH: Warna Kotak Form --- color: AppColors.cardBg, borderRadius: BorderRadius.circular(25), // --- BERUBAH: Warna Garis Pinggir Form --- border: Border.all(color: AppColors.textSecondary.withValues(alpha: 0.3)), ), child: Column( children: [ TextField( controller: _emailController, keyboardType: TextInputType.emailAddress, // --- BERUBAH: Warna Teks Inputan --- style: const TextStyle(color: AppColors.textMain), decoration: const InputDecoration( // --- BERUBAH: Warna Ikon --- prefixIcon: Icon(Icons.email, color: AppColors.primary), hintText: 'Email', // --- BERUBAH: Warna Placeholder/Hint --- hintStyle: TextStyle(color: AppColors.textSecondary), border: InputBorder.none, ), ), // --- BERUBAH: Garis Pemisah (Divider) --- Divider(color: AppColors.textSecondary.withValues(alpha: 0.3)), TextField( controller: _passwordController, obscureText: _obscurePassword, // --- BERUBAH: Warna Teks Inputan --- style: const TextStyle(color: AppColors.textMain), decoration: InputDecoration( // --- BERUBAH: Warna Ikon --- prefixIcon: const Icon(Icons.lock, color: AppColors.primary), suffixIcon: IconButton( icon: Icon( _obscurePassword ? Icons.visibility : Icons.visibility_off, // --- BERUBAH: Warna Ikon Mata --- color: AppColors.textSecondary, ), onPressed: () => setState( () => _obscurePassword = !_obscurePassword), ), hintText: 'Password', // --- BERUBAH: Warna Placeholder/Hint --- hintStyle: const TextStyle(color: AppColors.textSecondary), border: InputBorder.none, ), ), const SizedBox(height: 30), SizedBox( width: double.infinity, height: 55, child: ElevatedButton( onPressed: _isLoading ? null : _handleLogin, style: ElevatedButton.styleFrom( // --- BERUBAH: Latar Belakang Tombol --- backgroundColor: AppColors.primary, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(15), ), ), child: _isLoading ? const CircularProgressIndicator( color: Colors.white) // Tetap putih untuk kontras : const Text( 'Masuk ke Akun', style: TextStyle( fontSize: 18, fontWeight: FontWeight.bold, color: Colors.white), // Tetap putih ), ), ), ], ), ), const SizedBox(height: 16), // ── 1. TOMBOL LUPA PASSWORD ── TextButton( onPressed: () => Navigator.pushNamed(context, '/forgot_password'), style: TextButton.styleFrom( padding: EdgeInsets.zero, minimumSize: Size.zero, tapTargetSize: MaterialTapTargetSize.shrinkWrap, ), child: const Text( 'Lupa Password?', style: TextStyle( // --- BERUBAH: Warna Teks --- color: AppColors.primary, fontSize: 14, ), ), ), const SizedBox(height: 12), // ── 2. TOMBOL DAFTAR AKUN ── TextButton( onPressed: () => Navigator.pushNamed(context, '/register'), style: TextButton.styleFrom( padding: EdgeInsets.zero, minimumSize: Size.zero, tapTargetSize: MaterialTapTargetSize.shrinkWrap, ), child: const Text( 'Belum punya akun? Daftar Sekarang', style: TextStyle( // --- BERUBAH: Warna Teks --- color: AppColors.primary, fontSize: 14, ), ), ), ], ), ), ), ); } }