MIF_E31231033/lib/screens/auth/reset_password_screen.dart

206 lines
6.0 KiB
Dart

import 'package:flutter/material.dart';
import '../../services/auth_service.dart';
class ResetPasswordScreen extends StatefulWidget {
final String email;
final String otp;
const ResetPasswordScreen({
super.key,
required this.email,
required this.otp,
});
@override
State<ResetPasswordScreen> createState() => _ResetPasswordScreenState();
}
class _ResetPasswordScreenState extends State<ResetPasswordScreen> {
final passwordController = TextEditingController();
final confirmPasswordController = TextEditingController();
bool loading = false;
bool showPassword = false;
bool showConfirmPassword = false;
Future<void> resetPassword() async {
if (passwordController.text.isEmpty ||
confirmPasswordController.text.isEmpty) {
_showSnackBar("Password tidak boleh kosong", Colors.orange);
return;
}
if (passwordController.text != confirmPasswordController.text) {
_showSnackBar("Password tidak sama", Colors.red);
return;
}
setState(() => loading = true);
final result = await AuthService.resetPassword(
email: widget.email,
otp: widget.otp,
password: passwordController.text,
);
setState(() => loading = false);
if (!mounted) return;
if (result['success']) {
_showSnackBar("Password berhasil diubah", Colors.green);
Navigator.popUntil(context, (route) => route.isFirst);
} else {
_showSnackBar(result['message'], Colors.red);
}
}
void _showSnackBar(String msg, Color color) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(msg),
backgroundColor: color,
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
),
);
}
Widget passwordField({
required TextEditingController controller,
required String label,
required bool showPassword,
required VoidCallback toggle,
}) {
return TextField(
controller: controller,
obscureText: !showPassword,
decoration: InputDecoration(
labelText: label,
border: OutlineInputBorder(borderRadius: BorderRadius.circular(12)),
suffixIcon: IconButton(
icon: Icon(showPassword ? Icons.visibility : Icons.visibility_off),
onPressed: toggle,
),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFF5F7FA),
body: SafeArea(
child: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: Column(
children: [
Image.asset('assets/images/logopolije.png', height: 100),
const SizedBox(height: 16),
const Text(
"Reset Password",
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
const Text(
"Masukkan password baru untuk akun Anda",
style: TextStyle(color: Colors.grey),
textAlign: TextAlign.center,
),
const SizedBox(height: 30),
Container(
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(20),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.05),
blurRadius: 20,
offset: const Offset(0, 10),
),
],
),
child: Column(
children: [
passwordField(
controller: passwordController,
label: "Password Baru",
showPassword: showPassword,
toggle: () {
setState(() {
showPassword = !showPassword;
});
},
),
const SizedBox(height: 20),
passwordField(
controller: confirmPasswordController,
label: "Konfirmasi Password",
showPassword: showConfirmPassword,
toggle: () {
setState(() {
showConfirmPassword = !showConfirmPassword;
});
},
),
const SizedBox(height: 25),
SizedBox(
width: double.infinity,
height: 55,
child: ElevatedButton(
onPressed: loading ? null : resetPassword,
style: ElevatedButton.styleFrom(
backgroundColor: const Color(0xFF2F5BEA),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
child:
loading
? const CircularProgressIndicator(
color: Colors.white,
)
: const Text(
"Reset Password",
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
),
),
],
),
),
],
),
),
),
),
);
}
}