import 'package:flutter/material.dart'; import '../services/supabase_service.dart'; class RegisterScreen extends StatefulWidget { const RegisterScreen({super.key}); @override State createState() => _RegisterScreenState(); } class _RegisterScreenState extends State { 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 _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, ), const SizedBox(width: 10), Expanded( child: Text( pesan, style: const TextStyle( color: Colors.white, fontWeight: FontWeight.bold), ), ), ], ), backgroundColor: isError ? Colors.red.shade700 : Colors.green.shade700, 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( backgroundColor: Colors.black, appBar: AppBar( backgroundColor: Colors.transparent, elevation: 0, leading: IconButton( icon: const Icon(Icons.arrow_back, color: Colors.green), 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( color: Colors.green, fontSize: 32, fontWeight: FontWeight.bold), ), const Text( 'Lengkapi data diri Anda untuk memulai', style: TextStyle(color: Colors.grey, fontSize: 16), ), const SizedBox(height: 50), Container( padding: const EdgeInsets.all(20), decoration: BoxDecoration( color: const Color(0xFF1A1A1A), borderRadius: BorderRadius.circular(25), border: Border.all(color: Colors.grey.withOpacity(0.3)), ), child: Column( children: [ // Nama Lengkap TextField( controller: _nameController, style: const TextStyle(color: Colors.white), textCapitalization: TextCapitalization.words, decoration: const InputDecoration( prefixIcon: Icon(Icons.person, color: Colors.green), hintText: 'Nama Lengkap', hintStyle: TextStyle(color: Colors.grey), border: InputBorder.none, ), ), const Divider(color: Colors.grey), // Email TextField( controller: _emailController, keyboardType: TextInputType.emailAddress, style: const TextStyle(color: Colors.white), decoration: const InputDecoration( prefixIcon: Icon(Icons.email, color: Colors.green), hintText: 'Email', hintStyle: TextStyle(color: Colors.grey), border: InputBorder.none, ), ), const Divider(color: Colors.grey), // Password TextField( controller: _passwordController, obscureText: _obscurePassword, style: const TextStyle(color: Colors.white), decoration: InputDecoration( prefixIcon: const Icon(Icons.lock, color: Colors.green), suffixIcon: IconButton( icon: Icon( _obscurePassword ? Icons.visibility : Icons.visibility_off, color: Colors.grey, ), onPressed: () => setState( () => _obscurePassword = !_obscurePassword), ), hintText: 'Password (min. 6 karakter)', hintStyle: const TextStyle(color: Colors.grey), border: InputBorder.none, ), ), const SizedBox(height: 30), // Tombol Daftar SizedBox( width: double.infinity, height: 55, child: ElevatedButton( onPressed: _isLoading ? null : _handleRegister, style: ElevatedButton.styleFrom( backgroundColor: Colors.green, disabledBackgroundColor: Colors.green.withOpacity(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', style: TextStyle(color: Colors.green), ), ), ), ], ), ), ), ); } }