367 lines
12 KiB
Dart
367 lines
12 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'login.dart';
|
|
import 'screens/mobile_home_screen.dart';
|
|
import 'services/auth_service.dart';
|
|
|
|
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();
|
|
final _confirmPasswordController = TextEditingController();
|
|
bool _obscurePassword = true;
|
|
bool _obscureConfirmPassword = true;
|
|
bool _isLoading = false;
|
|
final _authService = AuthService();
|
|
|
|
@override
|
|
void dispose() {
|
|
_nameController.dispose();
|
|
_emailController.dispose();
|
|
_passwordController.dispose();
|
|
_confirmPasswordController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
backgroundColor: const Color(0xFFF5F5F5),
|
|
body: Column(
|
|
children: [
|
|
Container(
|
|
width: double.infinity,
|
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
|
|
decoration: const BoxDecoration(color: Color(0xFF1565C0)),
|
|
child: SafeArea(
|
|
bottom: false,
|
|
child: Row(
|
|
children: [
|
|
IconButton(
|
|
icon: const Icon(Icons.arrow_back, color: Colors.white),
|
|
onPressed: () => Navigator.pop(context),
|
|
),
|
|
const Spacer(),
|
|
Container(
|
|
width: 40,
|
|
height: 40,
|
|
decoration: const BoxDecoration(
|
|
color: Colors.white,
|
|
shape: BoxShape.circle,
|
|
),
|
|
child: const Icon(Icons.person, color: Color(0xFF1565C0)),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
Expanded(
|
|
child: Container(
|
|
color: Colors.white,
|
|
child: SingleChildScrollView(
|
|
padding: const EdgeInsets.all(24.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
const Text(
|
|
'REGISTER',
|
|
style: TextStyle(
|
|
fontSize: 32,
|
|
fontWeight: FontWeight.bold,
|
|
color: Colors.black87,
|
|
),
|
|
),
|
|
const SizedBox(height: 8),
|
|
Container(height: 1, color: Colors.grey[300]),
|
|
const SizedBox(height: 24),
|
|
RichText(
|
|
text: const TextSpan(
|
|
style: TextStyle(
|
|
fontSize: 18,
|
|
fontWeight: FontWeight.w600,
|
|
color: Colors.black87,
|
|
),
|
|
children: [
|
|
TextSpan(text: 'Buat '),
|
|
TextSpan(
|
|
text: 'Akun',
|
|
style: TextStyle(color: Color(0xFF1565C0)),
|
|
),
|
|
TextSpan(text: ' Baru'),
|
|
],
|
|
),
|
|
),
|
|
const SizedBox(height: 32),
|
|
_buildTextField(
|
|
label: 'Nama',
|
|
controller: _nameController,
|
|
prefixIcon: Icons.person_outline,
|
|
),
|
|
const SizedBox(height: 20),
|
|
_buildTextField(
|
|
label: 'Email',
|
|
controller: _emailController,
|
|
keyboardType: TextInputType.emailAddress,
|
|
prefixIcon: Icons.email_outlined,
|
|
),
|
|
const SizedBox(height: 20),
|
|
_buildTextField(
|
|
label: 'Password',
|
|
controller: _passwordController,
|
|
obscureText: _obscurePassword,
|
|
prefixIcon: Icons.lock_outline,
|
|
suffixIcon: IconButton(
|
|
icon: Icon(
|
|
_obscurePassword
|
|
? Icons.visibility_outlined
|
|
: Icons.visibility_off_outlined,
|
|
color: const Color(0xFF1565C0),
|
|
),
|
|
onPressed: () {
|
|
setState(() {
|
|
_obscurePassword = !_obscurePassword;
|
|
});
|
|
},
|
|
),
|
|
),
|
|
const SizedBox(height: 20),
|
|
_buildTextField(
|
|
label: 'Konfirmasi Password',
|
|
controller: _confirmPasswordController,
|
|
obscureText: _obscureConfirmPassword,
|
|
prefixIcon: Icons.lock_outline,
|
|
suffixIcon: IconButton(
|
|
icon: Icon(
|
|
_obscureConfirmPassword
|
|
? Icons.visibility_outlined
|
|
: Icons.visibility_off_outlined,
|
|
color: const Color(0xFF1565C0),
|
|
),
|
|
onPressed: () {
|
|
setState(() {
|
|
_obscureConfirmPassword = !_obscureConfirmPassword;
|
|
});
|
|
},
|
|
),
|
|
),
|
|
const SizedBox(height: 32),
|
|
SizedBox(
|
|
width: double.infinity,
|
|
child: ElevatedButton(
|
|
onPressed: _isLoading ? null : _handleRegister,
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: const Color(0xFF1565C0),
|
|
foregroundColor: Colors.white,
|
|
padding: const EdgeInsets.symmetric(vertical: 14),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
),
|
|
child: _isLoading
|
|
? const SizedBox(
|
|
height: 20,
|
|
width: 20,
|
|
child: CircularProgressIndicator(
|
|
valueColor:
|
|
AlwaysStoppedAnimation<Color>(Colors.white),
|
|
strokeWidth: 2,
|
|
),
|
|
)
|
|
: const Text(
|
|
'DAFTAR',
|
|
style: TextStyle(
|
|
fontSize: 15,
|
|
fontWeight: FontWeight.w600,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
const Text(
|
|
'Sudah punya akun? ',
|
|
style: TextStyle(fontSize: 14, color: Colors.black87),
|
|
),
|
|
GestureDetector(
|
|
onTap: () => Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => const LoginScreen(),
|
|
),
|
|
),
|
|
child: const Text(
|
|
'Masuk',
|
|
style: TextStyle(
|
|
fontSize: 14,
|
|
fontWeight: FontWeight.w600,
|
|
color: Color(0xFF1565C0),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 20),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildTextField({
|
|
required String label,
|
|
required TextEditingController controller,
|
|
TextInputType? keyboardType,
|
|
bool obscureText = false,
|
|
IconData? prefixIcon,
|
|
Widget? suffixIcon,
|
|
}) {
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
label,
|
|
style: const TextStyle(
|
|
fontSize: 15,
|
|
fontWeight: FontWeight.w500,
|
|
color: Colors.black87,
|
|
),
|
|
),
|
|
const SizedBox(height: 8),
|
|
TextField(
|
|
controller: controller,
|
|
keyboardType: keyboardType,
|
|
obscureText: obscureText,
|
|
decoration: InputDecoration(
|
|
prefixIcon: prefixIcon != null
|
|
? Icon(prefixIcon, color: const Color(0xFF1565C0))
|
|
: null,
|
|
suffixIcon: suffixIcon,
|
|
filled: true,
|
|
fillColor: Colors.white,
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(8),
|
|
borderSide: const BorderSide(
|
|
color: Color(0xFF1565C0),
|
|
width: 1.5,
|
|
),
|
|
),
|
|
enabledBorder: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(8),
|
|
borderSide: const BorderSide(
|
|
color: Color(0xFF1565C0),
|
|
width: 1.5,
|
|
),
|
|
),
|
|
focusedBorder: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(8),
|
|
borderSide: const BorderSide(
|
|
color: Color(0xFF1565C0),
|
|
width: 2,
|
|
),
|
|
),
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Future<void> _handleRegister() async {
|
|
final name = _nameController.text.trim();
|
|
final email = _emailController.text.trim();
|
|
final password = _passwordController.text;
|
|
final confirmPassword = _confirmPasswordController.text;
|
|
|
|
if (name.isEmpty) {
|
|
_showError('Nama tidak boleh kosong');
|
|
return;
|
|
}
|
|
if (email.isEmpty) {
|
|
_showError('Email tidak boleh kosong');
|
|
return;
|
|
}
|
|
if (password.isEmpty) {
|
|
_showError('Password tidak boleh kosong');
|
|
return;
|
|
}
|
|
if (password.length < 6) {
|
|
_showError('Password minimal 6 karakter');
|
|
return;
|
|
}
|
|
if (password != confirmPassword) {
|
|
_showError('Password tidak cocok');
|
|
return;
|
|
}
|
|
|
|
setState(() {
|
|
_isLoading = true;
|
|
});
|
|
|
|
try {
|
|
final result = await _authService.register(
|
|
name: name,
|
|
email: email,
|
|
password: password,
|
|
passwordConfirmation: confirmPassword,
|
|
);
|
|
|
|
if (!mounted) return;
|
|
|
|
if (result['success'] == true) {
|
|
_showSuccess('Registrasi berhasil! Selamat datang!');
|
|
await Future.delayed(const Duration(seconds: 1));
|
|
if (mounted) {
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(builder: (context) => const MobileHomeScreen()),
|
|
);
|
|
}
|
|
} else {
|
|
final message = result['message'] ?? 'Registrasi gagal';
|
|
_showError(message);
|
|
}
|
|
} catch (e) {
|
|
_showError('Error: $e');
|
|
} finally {
|
|
if (mounted) {
|
|
setState(() {
|
|
_isLoading = false;
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
void _showError(String message) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text(message),
|
|
backgroundColor: Colors.red,
|
|
duration: const Duration(seconds: 3),
|
|
),
|
|
);
|
|
}
|
|
|
|
void _showSuccess(String message) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text(message),
|
|
backgroundColor: Colors.green,
|
|
duration: const Duration(seconds: 2),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|