Jaga-Jalan/android/lib/forgot_password_page.dart

282 lines
9.7 KiB
Dart

import 'package:flutter/material.dart';
import './services/auth_service.dart';
import './config/theme_colors.dart';
import './reset_password_page.dart';
class ForgotPasswordPage extends StatefulWidget {
const ForgotPasswordPage({Key? key}) : super(key: key);
@override
_ForgotPasswordPageState createState() => _ForgotPasswordPageState();
}
class _ForgotPasswordPageState extends State<ForgotPasswordPage> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
final TextEditingController _emailController = TextEditingController();
final _authService = AuthService();
bool _isLoading = false;
Future<void> _sendResetLink() async {
if (!_formKey.currentState!.validate()) {
return;
}
setState(() => _isLoading = true);
try {
final result = await _authService.forgotPassword(
email: _emailController.text,
);
if (!mounted) return;
setState(() => _isLoading = false);
if (result['success']) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Row(
children: [
Icon(Icons.check_circle, color: Colors.white),
SizedBox(width: 8),
Expanded(
child: Text('Link reset password telah dikirim ke email Anda. Silakan cek email Anda dan klik link yang diberikan.'),
),
],
),
backgroundColor: Colors.green,
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
duration: Duration(seconds: 6),
),
);
// Kembali ke halaman login
Navigator.of(context).pop();
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Row(
children: [
Icon(Icons.error_outline, color: Colors.white),
SizedBox(width: 8),
Expanded(
child: Text(result['message'] ?? 'Gagal mengirim link reset password.'),
),
],
),
backgroundColor: Colors.red,
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
duration: Duration(seconds: 4),
),
);
}
} catch (e) {
if (!mounted) return;
setState(() => _isLoading = false);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Row(
children: [
Icon(Icons.warning, color: Colors.white),
SizedBox(width: 8),
Expanded(
child: Text('Terjadi kesalahan: Tidak dapat terhubung ke server'),
),
],
),
backgroundColor: Colors.orange,
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
duration: Duration(seconds: 3),
action: SnackBarAction(
label: 'COBA LAGI',
textColor: Colors.white,
onPressed: () {
_sendResetLink();
},
),
),
);
}
}
@override
Widget build(BuildContext context) {
final isDarkMode = Theme.of(context).brightness == Brightness.dark;
return Scaffold(
backgroundColor: ThemeColors.primary(context),
body: Stack(
children: [
// Background image
Positioned.fill(
child: Image.asset(
isDarkMode
? 'assets/images/background_dark.png'
: 'assets/images/background_light.png',
fit: BoxFit.cover,
),
),
// SizedBox(height: 30),
// Logo di bagian atas
Positioned(
top: 120,
left: 0,
right: 0,
child: Column(
children: [
Container(
width: 200,
height: 200,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
isDarkMode
? 'assets/images/logo_dark.png'
: 'assets/images/logo_light.png'
),
fit: BoxFit.contain,
),
),
),
],
),
),
// Form container di bagian bawah
Positioned(
bottom: 0,
left: 0,
right: 0,
child: Container(
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(
color: ThemeColors.background(context),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(30),
topRight: Radius.circular(30),
),
boxShadow: [
BoxShadow(
color: ThemeColors.shadowColor(context),
spreadRadius: 5,
blurRadius: 15,
),
],
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Lupa Password',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
color: ThemeColors.text(context),
),
),
SizedBox(height: 10),
Text(
'Masukkan email Anda untuk menerima link reset password',
style: TextStyle(
fontSize: 14,
color: ThemeColors.textGrey(context),
),
),
SizedBox(height: 30),
Form(
key: _formKey,
autovalidateMode: AutovalidateMode.onUserInteraction,
child: Column(
children: [
TextFormField(
controller: _emailController,
enabled: !_isLoading,
decoration: InputDecoration(
hintText: 'Masukkan email Anda',
prefixIcon: Icon(Icons.email, color: ThemeColors.inputIcon(context)),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide.none,
),
filled: true,
fillColor: ThemeColors.inputFill(context),
errorStyle: TextStyle(
color: ThemeColors.error,
fontSize: 12,
),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Email tidak boleh kosong';
}
if (!value.contains('@')) {
return 'Format email tidak valid';
}
final emailRegex = RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$');
if (!emailRegex.hasMatch(value)) {
return 'Format email tidak sesuai';
}
return null;
},
),
SizedBox(height: 30),
SizedBox(
width: double.infinity,
height: 50,
child: ElevatedButton(
onPressed: _isLoading ? null : _sendResetLink,
style: ElevatedButton.styleFrom(
backgroundColor: ThemeColors.accent(context),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
disabledBackgroundColor: ThemeColors.buttonDisabled(context),
),
child: _isLoading
? SizedBox(
width: 24,
height: 24,
child: CircularProgressIndicator(
color: ThemeColors.buttonText(context),
strokeWidth: 2,
),
)
: Text(
'KIRIM LINK RESET',
style: TextStyle(
color: ThemeColors.buttonText(context),
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
),
),
SizedBox(height: 20),
],
),
),
],
),
),
),
],
),
);
}
@override
void dispose() {
_emailController.dispose();
super.dispose();
}
}