TKK_E32222428/lib/screens/register_screen.dart

279 lines
11 KiB
Dart

import 'package:flutter/material.dart';
import '../services/supabase_service.dart';
import '../constants/app_colors.dart'; // <-- IMPORT APP COLORS DITAMBAHKAN
class RegisterScreen extends StatefulWidget {
const RegisterScreen({super.key});
@override
State<RegisterScreen> createState() => _RegisterScreenState();
}
class _RegisterScreenState extends State<RegisterScreen> {
final _nameController = TextEditingController();
final _emailController = TextEditingController();
final _passwordController = TextEditingController();
bool _isLoading = false;
bool _obscurePassword = true;
@override
void dispose() {
_nameController.dispose();
_emailController.dispose();
_passwordController.dispose();
super.dispose();
}
Future<void> _handleRegister() async {
final nama = _nameController.text.trim();
final email = _emailController.text.trim();
final password = _passwordController.text.trim();
// ── Validasi kosong ──
if (nama.isEmpty || email.isEmpty || password.isEmpty) {
_showSnackBar('Semua field harus diisi!', isError: true);
return;
}
// ── Validasi format email ──
if (!email.contains('@') || !email.contains('.')) {
_showSnackBar('Format email tidak valid!', isError: true);
return;
}
// ── Validasi panjang password ──
if (password.length < 6) {
_showSnackBar('Password minimal 6 karakter!', isError: true);
return;
}
setState(() => _isLoading = true);
try {
await SupabaseService().signUp(email, password, nama);
if (mounted) {
_showSnackBar('Akun berhasil dibuat! Silakan login.', isError: false);
Future.delayed(const Duration(milliseconds: 1500), () {
if (mounted) Navigator.pop(context);
});
}
} catch (e) {
if (mounted) {
final errorStr = e.toString().toLowerCase();
String pesan = 'Pendaftaran gagal, coba lagi.';
if (errorStr.contains('user_already_exists') ||
errorStr.contains('already registered') ||
errorStr.contains('already been registered')) {
pesan = 'Email sudah terdaftar, silakan login.';
} else if (errorStr.contains('invalid') &&
errorStr.contains('email')) {
pesan = 'Format email tidak valid!';
} else if (errorStr.contains('password') &&
errorStr.contains('weak')) {
pesan = 'Password terlalu lemah, gunakan kombinasi huruf dan angka.';
} else if (errorStr.contains('network') ||
errorStr.contains('socket')) {
pesan = 'Tidak ada koneksi internet.';
}
_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 agar kontras dengan background
),
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: 3),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
// --- BERUBAH: Background Scaffold 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: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Buat Akun Baru',
style: TextStyle(
// --- BERUBAH: Warna Judul ---
color: AppColors.primary,
fontSize: 32,
fontWeight: FontWeight.bold),
),
const Text(
'Lengkapi data diri Anda untuk memulai',
// --- BERUBAH: Warna Sub-judul ---
style: TextStyle(color: AppColors.textSecondary, fontSize: 16),
),
const SizedBox(height: 50),
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: Column(
children: [
// Nama Lengkap
TextField(
controller: _nameController,
// --- BERUBAH: Warna Teks Inputan ---
style: const TextStyle(color: AppColors.textMain),
textCapitalization: TextCapitalization.words,
decoration: const InputDecoration(
// --- BERUBAH: Warna Ikon ---
prefixIcon:
Icon(Icons.person, color: AppColors.primary),
hintText: 'Nama Lengkap',
// --- BERUBAH: Warna Placeholder/Hint ---
hintStyle: TextStyle(color: AppColors.textSecondary),
border: InputBorder.none,
),
),
// --- BERUBAH: Garis Pemisah (Divider) ---
Divider(color: AppColors.textSecondary.withValues(alpha: 0.3)),
// Email
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)),
// Password
TextField(
controller: _passwordController,
obscureText: _obscurePassword,
// --- BERUBAH: Warna Teks Inputan ---
style: const TextStyle(color: AppColors.textMain),
decoration: InputDecoration(
// --- BERUBAH: Warna Ikon Kunci ---
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 (min. 6 karakter)',
// --- BERUBAH: Warna Placeholder/Hint ---
hintStyle: const TextStyle(color: AppColors.textSecondary),
border: InputBorder.none,
),
),
const SizedBox(height: 30),
// Tombol Daftar
SizedBox(
width: double.infinity,
height: 55,
child: ElevatedButton(
onPressed: _isLoading ? null : _handleRegister,
style: ElevatedButton.styleFrom(
// --- BERUBAH: Warna Tombol ---
backgroundColor: AppColors.primary,
// --- BERUBAH: Warna Tombol saat Nonaktif (Loading) ---
disabledBackgroundColor:
AppColors.primary.withValues(alpha: 0.3),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15),
),
),
child: _isLoading
? const CircularProgressIndicator(
color: Colors.white)
: const Text(
'Daftar Sekarang',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Colors.white),
),
),
),
],
),
),
const SizedBox(height: 30),
Center(
child: TextButton(
onPressed: () => Navigator.pop(context),
child: const Text(
'Sudah punya akun? Masuk',
// --- BERUBAH: Warna Teks Bawah ---
style: TextStyle(color: AppColors.primary),
),
),
),
],
),
),
),
);
}
}