TKK_E32220332/lib/screens/forgot_password_screen.dart

339 lines
12 KiB
Dart

import 'package:flutter/material.dart';
import '../services/supabase_service.dart';
import '../constants/app_colors.dart'; // <-- IMPORT APP COLORS DITAMBAHKAN
class ForgotPasswordScreen extends StatefulWidget {
const ForgotPasswordScreen({super.key});
@override
State<ForgotPasswordScreen> createState() => _ForgotPasswordScreenState();
}
class _ForgotPasswordScreenState extends State<ForgotPasswordScreen> {
final _emailController = TextEditingController();
final _otpController = TextEditingController();
bool _isLoading = false;
bool _otpSent = false; // step 2: input OTP
String _email = '';
@override
void dispose() {
_emailController.dispose();
_otpController.dispose();
super.dispose();
}
// Step 1 — kirim OTP ke email
Future<void> _handleKirimOTP() async {
final email = _emailController.text.trim();
if (email.isEmpty) {
_showSnackBar('Email tidak boleh kosong!', isError: true);
return;
}
if (!email.contains('@') || !email.contains('.')) {
_showSnackBar('Format email tidak valid!', isError: true);
return;
}
setState(() => _isLoading = true);
try {
await SupabaseService().resetPassword(email);
if (mounted) {
setState(() {
_otpSent = true;
_email = email;
});
}
} catch (e) {
if (mounted) {
final err = e.toString().toLowerCase();
String pesan = 'Gagal mengirim kode, coba lagi.';
if (err.contains('network') || err.contains('socket')) {
pesan = 'Tidak ada koneksi internet.';
} else if (err.contains('rate limit') || err.contains('too many')) {
pesan = 'Terlalu banyak percobaan, tunggu beberapa menit.';
} else if (err.contains('user not found') || err.contains('invalid')) {
pesan = 'Email tidak terdaftar di sistem.';
}
_showSnackBar(pesan, isError: true);
}
} finally {
if (mounted) setState(() => _isLoading = false);
}
}
// Step 2 — verifikasi OTP dan masuk ke halaman ganti password
Future<void> _handleVerifikasiOTP() async {
final otp = _otpController.text.trim();
if (otp.isEmpty || otp.length != 6) {
_showSnackBar('Masukkan kode 6 angka dari email!', isError: true);
return;
}
setState(() => _isLoading = true);
try {
await SupabaseService().verifyOTP(_email, otp);
if (mounted) {
// OTP valid — langsung ke halaman ganti password
Navigator.pushReplacementNamed(
context,
'/change_password',
arguments: {'dari_reset': true},
);
}
} catch (e) {
if (mounted) {
final err = e.toString().toLowerCase();
String pesan = 'Kode tidak valid atau sudah kadaluarsa.';
if (err.contains('expired')) {
pesan = 'Kode sudah kadaluarsa. Kirim ulang kode baru.';
} else if (err.contains('invalid')) {
pesan = 'Kode salah. Periksa kembali kode di email.';
}
_showSnackBar(pesan, isError: true);
}
} finally {
if (mounted) setState(() => _isLoading = false);
}
}
void _showSnackBar(String pesan, {required bool isError}) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Row(children: [
Icon(
isError ? Icons.error_outline : Icons.check_circle_outline,
color: Colors.white, // Tetap putih untuk kontras
),
const SizedBox(width: 10),
Expanded(
child: Text(pesan,
style: const TextStyle(
color: Colors.white, fontWeight: FontWeight.bold)),
),
]),
// --- BERUBAH: Menggunakan warna notifikasi dari AppColors ---
backgroundColor:
isError ? AppColors.error : AppColors.success,
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
margin: const EdgeInsets.all(16),
duration: const Duration(seconds: 4),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
// --- BERUBAH: Background Utama ---
backgroundColor: AppColors.background,
appBar: AppBar(
backgroundColor: Colors.transparent,
elevation: 0,
leading: IconButton(
// --- BERUBAH: Warna Ikon Back ---
icon: const Icon(Icons.arrow_back, color: AppColors.primary),
onPressed: () => Navigator.pop(context),
),
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 30.0),
child: _otpSent ? _buildStepOTP() : _buildStepEmail(),
),
),
);
}
// ── Step 1: Input Email ──
Widget _buildStepEmail() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Lupa Password?',
style: TextStyle(
// --- BERUBAH: Warna Judul ---
color: AppColors.primary, fontSize: 32, fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
const Text(
'Masukkan email akun kamu. Kami akan kirim kode verifikasi 6 angka ke inbox kamu.',
// --- BERUBAH: Warna Sub-judul ---
style: TextStyle(color: AppColors.textSecondary, fontSize: 14),
),
const SizedBox(height: 50),
// Field email
Container(
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(
// --- BERUBAH: Latar Belakang Form ---
color: AppColors.cardBg,
borderRadius: BorderRadius.circular(25),
// --- BERUBAH: Garis Pinggir Form ---
border: Border.all(color: AppColors.textSecondary.withValues(alpha: 0.3)),
),
child: 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,
),
),
),
const SizedBox(height: 30),
// Tombol kirim
SizedBox(
width: double.infinity,
height: 55,
child: ElevatedButton(
onPressed: _isLoading ? null : _handleKirimOTP,
style: ElevatedButton.styleFrom(
// --- BERUBAH: Warna Tombol ---
backgroundColor: AppColors.primary,
// --- BERUBAH: Warna Tombol saat Loading ---
disabledBackgroundColor: AppColors.primary.withValues(alpha: 0.3),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15)),
),
child: _isLoading
? const CircularProgressIndicator(color: Colors.white)
: const Text(
'Kirim Kode Verifikasi',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
color: Colors.white),
),
),
),
const SizedBox(height: 20),
Center(
child: TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Kembali ke Login',
// --- BERUBAH: Warna Teks Bawah ---
style: TextStyle(color: AppColors.primary)),
),
),
],
);
}
// ── Step 2: Input OTP ──
Widget _buildStepOTP() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const CircleAvatar(
radius: 40,
// --- BERUBAH: Latar Belakang Ikon ---
backgroundColor: AppColors.cardBg,
// --- BERUBAH: Warna Ikon ---
child: Icon(Icons.mark_email_read, size: 40, color: AppColors.primary),
),
const SizedBox(height: 24),
const Text(
'Cek Email Kamu',
style: TextStyle(
// --- BERUBAH: Warna Judul ---
color: AppColors.primary, fontSize: 28, fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
Text(
'Kode verifikasi 6 angka sudah dikirim ke:\n$_email\n\nMasukkan kode tersebut di bawah ini.',
// --- BERUBAH: Warna Subteks ---
style: const TextStyle(color: AppColors.textSecondary, fontSize: 14),
),
const SizedBox(height: 40),
// Field OTP
Container(
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(
// --- BERUBAH: Latar Belakang Kotak OTP ---
color: AppColors.cardBg,
borderRadius: BorderRadius.circular(25),
// --- BERUBAH: Garis Pinggir Kotak OTP ---
border: Border.all(color: AppColors.primary.withValues(alpha: 0.5)),
),
child: TextField(
controller: _otpController,
keyboardType: TextInputType.number,
maxLength: 6,
textAlign: TextAlign.center,
style: const TextStyle(
// --- BERUBAH: Warna Teks Inputan ---
color: AppColors.textMain, fontSize: 28, letterSpacing: 12),
decoration: const InputDecoration(
// --- BERUBAH: Warna Ikon ---
prefixIcon: Icon(Icons.lock_open, color: AppColors.primary),
hintText: '000000',
// --- BERUBAH: Warna Placeholder/Hint ---
hintStyle: TextStyle(color: AppColors.textSecondary, letterSpacing: 8),
border: InputBorder.none,
counterText: '', // sembunyikan counter maxLength
),
),
),
const SizedBox(height: 30),
// Tombol verifikasi
SizedBox(
width: double.infinity,
height: 55,
child: ElevatedButton(
onPressed: _isLoading ? null : _handleVerifikasiOTP,
style: ElevatedButton.styleFrom(
// --- BERUBAH: Warna Tombol ---
backgroundColor: AppColors.primary,
// --- BERUBAH: Warna Tombol saat Loading ---
disabledBackgroundColor: AppColors.primary.withValues(alpha: 0.3),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15)),
),
child: _isLoading
? const CircularProgressIndicator(color: Colors.white)
: const Text(
'Verifikasi Kode',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
color: Colors.white),
),
),
),
const SizedBox(height: 16),
// Kirim ulang kode
Center(
child: TextButton(
onPressed: _isLoading
? null
: () => setState(() {
_otpSent = false;
_otpController.clear();
}),
child: const Text(
'Kirim ulang kode',
// --- BERUBAH: Warna Teks Bawah ---
style: TextStyle(color: AppColors.primary),
),
),
),
],
);
}
}