MIF_E31230069/spk_mobile/lib/register.dart

388 lines
13 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: Colors.white,
body: Column(
children: [
Container(
width: double.infinity,
padding: const EdgeInsets.fromLTRB(8, 0, 16, 24),
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Color(0xFF1565C0), Color(0xFF0D47A1)],
),
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(28),
bottomRight: Radius.circular(28),
),
),
child: SafeArea(
bottom: false,
child: Column(
children: [
Row(
children: [
IconButton(
icon: const Icon(Icons.arrow_back_rounded, color: Colors.white),
onPressed: () => Navigator.pop(context),
),
],
),
const SizedBox(height: 8),
Container(
padding: const EdgeInsets.all(18),
decoration: BoxDecoration(
color: Colors.white,
shape: BoxShape.circle,
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.1),
blurRadius: 20,
offset: const Offset(0, 6),
),
],
),
child: const Icon(
Icons.person_add_rounded,
color: Color(0xFF1565C0),
size: 36,
),
),
const SizedBox(height: 16),
const Text(
'Buat Akun Baru',
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.w700,
color: Colors.white,
letterSpacing: 0.3,
),
),
const SizedBox(height: 4),
Text(
'Daftar untuk mulai menggunakan aplikasi',
style: TextStyle(
fontSize: 13,
color: Colors.white.withOpacity(0.8),
),
),
],
),
),
),
Expanded(
child: SingleChildScrollView(
padding: const EdgeInsets.fromLTRB(24, 28, 24, 24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_buildTextField(
label: 'Nama',
controller: _nameController,
prefixIcon: Icons.person_outline,
),
const SizedBox(height: 16),
_buildTextField(
label: 'Email',
controller: _emailController,
keyboardType: TextInputType.emailAddress,
prefixIcon: Icons.email_outlined,
),
const SizedBox(height: 16),
_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: 16),
_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: 28),
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: 16),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(14),
),
elevation: 0,
),
child: _isLoading
? const SizedBox(
height: 20,
width: 20,
child: CircularProgressIndicator(
valueColor:
AlwaysStoppedAnimation<Color>(Colors.white),
strokeWidth: 2,
),
)
: const Text(
'DAFTAR',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w700,
letterSpacing: 0.5,
),
),
),
),
const SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Sudah punya akun? ',
style: TextStyle(fontSize: 14, color: Colors.grey[600]),
),
GestureDetector(
onTap: () => Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => const LoginScreen(),
),
),
child: const Text(
'Masuk',
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w700,
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: 14,
fontWeight: FontWeight.w600,
color: Color(0xFF1A1A2E),
),
),
const SizedBox(height: 8),
TextField(
controller: controller,
keyboardType: keyboardType,
obscureText: obscureText,
decoration: InputDecoration(
prefixIcon: prefixIcon != null
? Icon(prefixIcon, color: const Color(0xFF1565C0), size: 20)
: null,
suffixIcon: suffixIcon,
filled: true,
fillColor: const Color(0xFFF7F8FC),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(14),
borderSide: const BorderSide(color: Color(0xFFE0E0E0)),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(14),
borderSide: const BorderSide(color: Color(0xFFE0E0E0)),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(14),
borderSide: const BorderSide(color: Color(0xFF1565C0), width: 1.5),
),
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 15),
),
),
],
);
}
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: Row(
children: [
const Icon(Icons.error_outline_rounded, color: Colors.white, size: 20),
const SizedBox(width: 10),
Expanded(child: Text(message)),
],
),
backgroundColor: const Color(0xFFC62828),
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
margin: const EdgeInsets.all(16),
duration: const Duration(seconds: 3),
),
);
}
void _showSuccess(String message) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Row(
children: [
const Icon(Icons.check_circle_rounded, color: Colors.white, size: 20),
const SizedBox(width: 10),
Expanded(child: Text(message)),
],
),
backgroundColor: const Color(0xFF2E7D32),
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
margin: const EdgeInsets.all(16),
duration: const Duration(seconds: 2),
),
);
}
}