133 lines
4.3 KiB
Dart
133 lines
4.3 KiB
Dart
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:firebase_auth/firebase_auth.dart';
|
|
import 'package:flutter/material.dart';
|
|
import '../utils/constants.dart';
|
|
import 'package:awesome_dialog/awesome_dialog.dart';
|
|
|
|
|
|
class LoginScreen extends StatefulWidget {
|
|
const LoginScreen({super.key});
|
|
|
|
@override
|
|
State<LoginScreen> createState() => _LoginScreenState();
|
|
}
|
|
|
|
class _LoginScreenState extends State<LoginScreen> {
|
|
final emailController = TextEditingController();
|
|
final passwordController = TextEditingController();
|
|
bool isLoading = false;
|
|
String? errorMessage;
|
|
|
|
Future<void> login() async {
|
|
setState(() {
|
|
isLoading = true;
|
|
errorMessage = null;
|
|
});
|
|
|
|
try {
|
|
final credential = await FirebaseAuth.instance.signInWithEmailAndPassword(
|
|
email: emailController.text.trim(),
|
|
password: passwordController.text.trim(),
|
|
);
|
|
|
|
final uid = credential.user!.uid;
|
|
final userDoc =
|
|
await FirebaseFirestore.instance.collection('users').doc(uid).get();
|
|
|
|
if (userDoc.exists && userDoc.data()!.containsKey('nama_sungai')) {
|
|
final namaSungai = userDoc['nama_sungai'];
|
|
|
|
// Langsung pindah halaman tanpa alert
|
|
Navigator.pushReplacementNamed(context, '/dashboard',
|
|
arguments: namaSungai);
|
|
} else {
|
|
AwesomeDialog(
|
|
context: context,
|
|
dialogType: DialogType.warning,
|
|
animType: AnimType.leftSlide,
|
|
title: 'Akun Tidak Valid',
|
|
desc: 'Akun tidak memiliki data sungai.',
|
|
btnOkOnPress: () {},
|
|
).show();
|
|
}
|
|
} on FirebaseAuthException catch (e) {
|
|
AwesomeDialog(
|
|
context: context,
|
|
dialogType: DialogType.error,
|
|
animType: AnimType.scale,
|
|
title: 'Gagal Login',
|
|
desc: e.message ?? 'Terjadi kesalahan saat login.',
|
|
btnOkOnPress: () {},
|
|
).show();
|
|
} finally {
|
|
setState(() => isLoading = false);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
backgroundColor: AppColors.background,
|
|
body: SingleChildScrollView(
|
|
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 48),
|
|
child: Column(
|
|
children: [
|
|
const SizedBox(height: 60),
|
|
const Icon(Icons.lock_outline, size: 80, color: AppColors.primary),
|
|
const SizedBox(height: 24),
|
|
const Text("Login Akun Alat", style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold)),
|
|
const SizedBox(height: 8),
|
|
const Text("Silakan login dengan akun yang sudah dibuat oleh admin.", style: TextStyle(fontSize: 14, color: Colors.black54), textAlign: TextAlign.center),
|
|
const SizedBox(height: 32),
|
|
|
|
TextField(
|
|
controller: emailController,
|
|
decoration: InputDecoration(
|
|
labelText: 'Email',
|
|
border: OutlineInputBorder(borderRadius: BorderRadius.circular(12)),
|
|
filled: true,
|
|
fillColor: Colors.white,
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
TextField(
|
|
controller: passwordController,
|
|
obscureText: true,
|
|
decoration: InputDecoration(
|
|
labelText: 'Password',
|
|
border: OutlineInputBorder(borderRadius: BorderRadius.circular(12)),
|
|
filled: true,
|
|
fillColor: Colors.white,
|
|
),
|
|
),
|
|
|
|
if (errorMessage != null) ...[
|
|
const SizedBox(height: 12),
|
|
Text(errorMessage!, style: const TextStyle(color: Colors.red)),
|
|
],
|
|
|
|
const SizedBox(height: 24),
|
|
SizedBox(
|
|
width: double.infinity,
|
|
child: ElevatedButton(
|
|
onPressed: isLoading ? null : login,
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: AppColors.primary,
|
|
padding: const EdgeInsets.symmetric(vertical: 14),
|
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
|
),
|
|
child: isLoading
|
|
? const CircularProgressIndicator(color: Colors.white)
|
|
: const Text('Login', style: TextStyle(fontSize: 16, color: Colors.white)),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|