pd-android-app/lib/screens/authentication/change_password_screen.dart
2025-04-28 13:34:34 +05:30

190 lines
6.6 KiB
Dart

import 'package:cheminova/widgets/custom_button.dart';
import 'package:cheminova/widgets/input_field.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import 'package:google_fonts/google_fonts.dart';
import '../../utils/show_snackbar.dart';
import 'controller/auth_controller.dart';
class ChangePasswordScreen extends StatefulWidget {
const ChangePasswordScreen({super.key});
@override
State<ChangePasswordScreen> createState() => _ChangePasswordScreenState();
}
class _ChangePasswordScreenState extends State<ChangePasswordScreen> {
// Initialize the AuthController using GetX dependency injection
final authController = Get.put(AuthController());
// void dispose() {
// // Dispose of the text controllers when the screen is closed to free up resources
// authController.currentpassController.dispose();
// authController.newpassController.dispose();
// authController.confirmpassController.dispose();
// super.dispose();
// }
// Function to validate user input and initiate password change
void validateAndChangePassword() async {
String oldPassword = authController.currentpassController.text.trim();
String newPassword = authController.newpassController.text.trim();
String confirmPassword = authController.confirmpassController.text.trim();
// Check if any of the fields are empty
if (newPassword.isEmpty || confirmPassword.isEmpty || oldPassword.isEmpty) {
showSnackbar('All fields are required');
return;
}
// Check if the new password meets the minimum length requirement
if (newPassword.length < 8) {
showSnackbar('Password must be at least 8 characters long');
return;
}
// Check if the new password contains at least one special character
if (!RegExp(r'[!@#$%^&*(),.?":{}|<>]').hasMatch(newPassword)) {
showSnackbar('Password must contain at least one special character');
return;
}
// Check if new password and confirm password fields match
if (newPassword != confirmPassword) {
showSnackbar('New Password and Confirm Password do not match');
return;
}
// Call the changePassword method from AuthController
authController.changePassword();
}
@override
Widget build(BuildContext context) {
return Scaffold(
extendBodyBehindAppBar: true,
appBar: AppBar(
backgroundColor: Colors.transparent,
elevation: 0,
leading: GestureDetector(
onTap: () => Get.back(),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: SizedBox(
width: 20,
height: 20,
child: SvgPicture.asset('assets/svg/menu.svg')),
),
),
actions: [
GestureDetector(
onTap: () => Get.back(),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: SvgPicture.asset('assets/svg/back_arrow.svg'),
),
),
],
),
body: Stack(
alignment: Alignment.topCenter,
children: [
Container(
decoration: const BoxDecoration(
image: DecorationImage(
fit: BoxFit.cover,
image: AssetImage(
'assets/images/image_1.png',
),
),
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(50.0),
bottomRight: Radius.circular(50.0),
),
),
child: SizedBox(
width: Get.width,
height: Get.height * 0.7,
),
),
// Main content area with a scrollable card
Center(
child: SingleChildScrollView(
child: Card(
margin: const EdgeInsets.symmetric(horizontal: 24),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(19),
side: const BorderSide(color: Color(0xFFFDFDFD)),
),
color: const Color(0xFFB4D1E5).withOpacity(0.9),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
// Title for the Change Password screen
Container(
padding: const EdgeInsets.only(bottom: 10),
alignment: Alignment.centerLeft,
child: Text(
'Change Password',
style: GoogleFonts.getFont(
'Roboto',
fontWeight: FontWeight.w500,
fontSize: 30,
height: 1.2,
),
),
),
// Input field for current password
InputField(
hintText: "Current Password",
labelText: "Current Password",
controller: authController.currentpassController,
obscureText: false,
keyboardType: TextInputType.text,
),
const SizedBox(height: 15),
// Input field for new password
InputField(
hintText: "New Password",
labelText: "New Password",
obscureText: false,
controller: authController.newpassController,
keyboardType: TextInputType.text,
),
const SizedBox(height: 15),
// Input field for confirm password
InputField(
hintText: "Confirm Password",
labelText: "Confirm Password",
obscureText: false,
controller: authController.confirmpassController,
keyboardType: TextInputType.text,
),
const SizedBox(height: 30),
CustomButton(
text: "RESET PASSWORD",
onPressed: () async {
validateAndChangePassword();
},
isLoading: authController.isLoading.value,
)
],
),
),
),
),
),
],
),
);
}
}