import 'package:flutter/material.dart'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter_background_service/flutter_background_service.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'signup_screen.dart'; import 'firebase_auth_services.dart'; import 'toast.dart'; import 'bottom_navigation.dart'; class LoginScreen extends StatefulWidget { const LoginScreen({super.key}); @override State createState() => _LoginScreenState(); } class _LoginScreenState extends State { final FirebaseAuthService _auth = FirebaseAuthService(); final TextEditingController _emailController = TextEditingController(); final TextEditingController _passwordController = TextEditingController(); bool _isSigningIn = false; bool _obscurePassword = true; @override void dispose() { _emailController.dispose(); _passwordController.dispose(); super.dispose(); } void _signIn() async { setState(() => _isSigningIn = true); String email = _emailController.text.trim(); String password = _passwordController.text.trim(); try { UserCredential userCredential = await FirebaseAuth.instance .signInWithEmailAndPassword(email: email, password: password); setState(() => _isSigningIn = false); if (userCredential.user != null) { final user = FirebaseAuth.instance.currentUser; if (user != null) { SharedPreferences prefs = await SharedPreferences.getInstance(); await prefs.setString('userId', user.uid); FlutterBackgroundService().startService(); print("✅ Login sukses & background service dimulai kembali"); print("✅ userId berhasil disimpan ke SharedPreferences: ${user.uid}"); } showToast(message: "Login berhasil!"); Navigator.pushReplacement( context, MaterialPageRoute(builder: (context) => const MainNavigation()), ); } } on FirebaseAuthException catch (e) { setState(() => _isSigningIn = false); String errorMessage = "Terjadi kesalahan saat login."; switch (e.code) { case 'invalid-email': errorMessage = "Format email tidak valid."; break; case 'user-disabled': errorMessage = "Pengguna ini telah dinonaktifkan."; break; case 'user-not-found': errorMessage = "Email belum terdaftar."; break; case 'wrong-password': errorMessage = "Password salah."; break; case 'too-many-requests': errorMessage = "Terlalu banyak percobaan login. Coba lagi nanti."; break; } showToast(message: errorMessage); } catch (e) { setState(() => _isSigningIn = false); showToast(message: "Terjadi kesalahan tidak terduga."); } } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: const Color(0xFFFFFFFF), body: Column( children: [ Container( width: double.infinity, padding: const EdgeInsets.only(top: 80, bottom: 30), decoration: const BoxDecoration( color: Color(0xFF3FA535), borderRadius: BorderRadius.only( bottomLeft: Radius.circular(40), bottomRight: Radius.circular(40), ), ), child: Column( children: [ Image.asset( 'assets/images/logoputih.png', width: 50, height: 50, ), const SizedBox(height: 10), const Text( 'Sign in to Your Account', style: TextStyle( fontSize: 22, fontWeight: FontWeight.bold, color: Colors.white, ), ), const SizedBox(height: 5), const Text( 'Enter your email and password to sign in', style: TextStyle(fontSize: 14, color: Colors.white70), ), ], ), ), Expanded( child: SingleChildScrollView( padding: const EdgeInsets.all(24), child: Container( padding: const EdgeInsets.all(24), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(24), boxShadow: [ BoxShadow( color: Colors.grey.shade300, blurRadius: 10, offset: const Offset(0, 5), ), ], ), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ const Text( 'Sign In', style: TextStyle( fontSize: 18, fontWeight: FontWeight.bold, ), ), const SizedBox(height: 20), TextField( controller: _emailController, decoration: const InputDecoration( prefixIcon: Icon(Icons.email_outlined), labelText: 'Email', border: UnderlineInputBorder(), ), ), const SizedBox(height: 16), TextField( controller: _passwordController, obscureText: _obscurePassword, decoration: InputDecoration( prefixIcon: const Icon(Icons.lock_outline), labelText: 'Password', border: const UnderlineInputBorder(), suffixIcon: IconButton( icon: Icon( _obscurePassword ? Icons.visibility_off : Icons.visibility, ), onPressed: () { setState(() { _obscurePassword = !_obscurePassword; }); }, ), ), ), const SizedBox(height: 30), ElevatedButton( onPressed: _signIn, style: ElevatedButton.styleFrom( backgroundColor: const Color(0xFF3FA535), padding: const EdgeInsets.symmetric(vertical: 16), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(30), ), ), child: _isSigningIn ? const CircularProgressIndicator( color: Colors.white, ) : const Text( 'Sign In', style: TextStyle( fontSize: 16, color: Colors.white, ), ), ), const SizedBox(height: 20), Row( mainAxisAlignment: MainAxisAlignment.center, children: [ const Text("Don't have an account? "), GestureDetector( onTap: () { Navigator.push( context, MaterialPageRoute( builder: (context) => const SignUpScreen(), ), ); }, child: const Text( 'Sign Up', style: TextStyle( color: Color(0xFF3FA535), fontWeight: FontWeight.bold, ), ), ), ], ), ], ), ), ), ), ], ), ); } }