diff --git a/lib/screens/sign_up_screen/RegistrationDetails.dart b/lib/screens/sign_up_screen/RegistrationDetails.dart index 3a57a11..96265e5 100644 --- a/lib/screens/sign_up_screen/RegistrationDetails.dart +++ b/lib/screens/sign_up_screen/RegistrationDetails.dart @@ -4,7 +4,6 @@ import 'package:flutter/services.dart'; import '../../Utils/image_constant.dart'; import '../../Utils/size_utils.dart'; -import '../../providers/token_manager.dart'; import '../../theme/app_style.dart'; import '../../widgets/app_bar/appbar_image.dart'; import '../../widgets/app_bar/appbar_title.dart'; @@ -30,12 +29,18 @@ class _RegistrationDetailsScreenState extends State { final Map formData = {}; final _formKey = GlobalKey(); - late BuildContext _context; // Store the current context var account_id = null; // Initialize with null var selectedAccount; // Use nullable type var newPassword = ''; // Store the value of the confirm password field var confirmPassword = ''; // Store the value of the confirm password field + bool _isSubmitting = false; + + late final FocusNode _firstNameNode; + late final FocusNode _lastNameNode; + late final FocusNode _mobileNode; + late final FocusNode _newPasswordNode; + late final FocusNode _confirmPasswordNode; // Validate that the passwords match String? _validatePasswordMatch(String value) { if (value != newPassword) { @@ -50,9 +55,12 @@ class _RegistrationDetailsScreenState extends State { bool _isPasswordValid = true; void _validatePassword(String password) { - setState(() { - _isPasswordValid = password.isNotEmpty; - }); + final valid = password.isNotEmpty; + if (valid != _isPasswordValid) { + setState(() { + _isPasswordValid = valid; + }); + } } bool _isEmailValid = true; @@ -86,12 +94,25 @@ class _RegistrationDetailsScreenState extends State { @override void initState() { super.initState(); + _firstNameNode = FocusNode(); + _lastNameNode = FocusNode(); + _mobileNode = FocusNode(); + _newPasswordNode = FocusNode(); + _confirmPasswordNode = FocusNode(); + } + + @override + void dispose() { + _firstNameNode.dispose(); + _lastNameNode.dispose(); + _mobileNode.dispose(); + _newPasswordNode.dispose(); + _confirmPasswordNode.dispose(); + super.dispose(); } @override Widget build(BuildContext context) { - _context = context; // Store the context - return Scaffold( appBar: CustomAppBar( height: getVerticalSize(54), @@ -112,9 +133,8 @@ class _RegistrationDetailsScreenState extends State { padding: const EdgeInsets.all(16), child: Form( key: _formKey, - child: Container( - height: 500, - child: Column( + child: Column( + mainAxisSize: MainAxisSize.min, children: [ Padding( padding: getPadding(top: 19), @@ -124,7 +144,7 @@ class _RegistrationDetailsScreenState extends State { style: AppStyle.txtGilroyMedium16Bluegray900)), CustomTextFormField( - focusNode: FocusNode(), + focusNode: _firstNameNode, onsaved: (value) => formData['first_name'] = value, validator: (value) { if (value == null || value.isEmpty) { @@ -145,7 +165,7 @@ class _RegistrationDetailsScreenState extends State { style: AppStyle.txtGilroyMedium16Bluegray900)), CustomTextFormField( - focusNode: FocusNode(), + focusNode: _lastNameNode, onsaved: (value) => formData['last_name'] = value, validator: (value) { if (value == null || value.isEmpty) { @@ -167,7 +187,7 @@ class _RegistrationDetailsScreenState extends State { style: AppStyle.txtGilroyMedium16Bluegray900)), CustomTextFormField( - focusNode: FocusNode(), + focusNode: _mobileNode, textInputType: TextInputType.phone, inputFormatters: [ FilteringTextInputFormatter.allow(RegExp(r'[0-9]')) @@ -191,7 +211,7 @@ class _RegistrationDetailsScreenState extends State { style: AppStyle.txtGilroyMedium16Bluegray900)), CustomTextFormField( - focusNode: FocusNode(), + focusNode: _newPasswordNode, hintText: "New Password", margin: getMargin(top: 6), padding: TextFormFieldPadding.PaddingT12, @@ -219,10 +239,8 @@ class _RegistrationDetailsScreenState extends State { ), onsaved: (value) => formData['new_password'] = value, onChanged: (value) { - setState(() { - newPassword = value!; - }); - _validatePassword; + newPassword = value; + _validatePassword(value); }, suffixConstraints: BoxConstraints( maxHeight: getVerticalSize(44)), @@ -236,7 +254,7 @@ class _RegistrationDetailsScreenState extends State { style: AppStyle.txtGilroyMedium16Bluegray900)), CustomTextFormField( - focusNode: FocusNode(), + focusNode: _confirmPasswordNode, hintText: "Confirm Password", margin: getMargin(top: 6), padding: TextFormFieldPadding.PaddingT12, @@ -246,13 +264,11 @@ class _RegistrationDetailsScreenState extends State { if (value == null || value.isEmpty) { return 'Please enter Password'; } - return _validatePasswordMatch(confirmPassword); + return _validatePasswordMatch(value); }, onsaved: (value) => formData['confirm_password'] = value, onChanged: (value) { - setState(() { - confirmPassword = value!; // Update confirmPassword - }); + confirmPassword = value; // Update confirmPassword }, suffix: IconButton( icon: Icon( @@ -299,60 +315,73 @@ class _RegistrationDetailsScreenState extends State { ], ), - if (account_id != null) - Container( + if (account_id == null) + const Padding( + padding: EdgeInsets.only(top: 8), + child: Text( + 'Tap + to create an account first, then SUBMIT will be enabled.', + style: TextStyle(fontSize: 12, color: Colors.grey), + ), + ), + + Container( margin: const EdgeInsets.symmetric(vertical: 5), // Add margin child: CustomButton( height: getVerticalSize(50), width: getHorizontalSize(396), - text: "SUBMIT", + text: _isSubmitting ? "PLEASE WAIT..." : "SUBMIT", margin: getMargin(top: 25), - onTap: () async { - if (_formKey.currentState!.validate()) { - _formKey.currentState!.save(); + onTap: (account_id == null || _isSubmitting) + ? null + : () async { + final isValid = + _formKey.currentState!.validate(); + if (!isValid) { + showErrorMessage( + 'Please fix the red errors (check passwords match).'); + return; + } + _formKey.currentState!.save(); - print('formdata is $formData'); + print('formdata is $formData'); - formData['usrGrpId'] = 46; - formData['account_id'] = account_id; - formData['email'] = widget.email; + formData['usrGrpId'] = 46; + formData['account_id'] = account_id; + formData['email'] = widget.email; - { - try { - print(formData); + setState(() => _isSubmitting = true); + try { + print(formData); - await userService - .createuser(formData) - .then((_) => { - const LoginScreen(), - }); + await userService.createuser(formData); - await Future.delayed( - const Duration(seconds: 5)); - - showSuccessMessage('User created successfully'); - // ignore: use_build_context_synchronously - Navigator.push( - context, - MaterialPageRoute( - builder: (context) => - const LoginScreen())); - // moveToNextStep(); - } catch (e) { - showErrorMessage('Failed to create User: $e'); - } - } - } - }, + if (!context.mounted) return; + showSuccessMessage( + 'User created successfully'); + Navigator.pushAndRemoveUntil( + context, + MaterialPageRoute( + builder: (context) => + const LoginScreen()), + (route) => false); + } catch (e) { + if (!context.mounted) return; + showErrorMessage( + 'Failed to create User: $e'); + } finally { + if (mounted) { + setState(() => _isSubmitting = false); + } + } + }, ), ), ], - ), - ), - ), - ), - ), - ); - } -} + ), + ), + ), + ), + ); + } + }