import 'package:flutter/material.dart'; 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'; import '../../widgets/app_bar/custom_app_bar.dart'; import '../../widgets/custom_button.dart'; import '../../widgets/custom_text_form_field.dart'; import '../Login Screen/login_screen.dart'; import 'CreateAccount.dart'; import 'SignUpService.dart'; class RegistrationDetailsScreen extends StatefulWidget { var email; RegistrationDetailsScreen({required this.email}); @override _RegistrationDetailsScreenState createState() => _RegistrationDetailsScreenState(); } class _RegistrationDetailsScreenState extends State { final SignUpApiService userService = SignUpApiService(); 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 // Validate that the passwords match String? _validatePasswordMatch(String value) { if (value != newPassword) { print('value is $value and new is $newPassword'); return 'Passwords do not match'; } return null; } bool _newpasswordVisible = false; bool _confirmpasswordVisible = false; bool _isPasswordValid = true; void _validatePassword(String password) { setState(() { _isPasswordValid = password.isNotEmpty; }); } bool _isEmailValid = true; void _validateEmail(String email) { setState(() { _isEmailValid = RegExp(r'^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$').hasMatch(email); }); } void showSuccessMessage(String message) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text(message), duration: const Duration(seconds: 2), backgroundColor: Colors.green, ), ); } void showErrorMessage(String error) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text(error), duration: const Duration(seconds: 2), backgroundColor: Colors.red, ), ); } @override void initState() { super.initState(); } @override Widget build(BuildContext context) { _context = context; // Store the context return Scaffold( appBar: CustomAppBar( height: getVerticalSize(54), leadingWidth: 40, leading: AppbarImage( height: getSize(24), width: getSize(24), svgPath: ImageConstant.imgArrowleft, margin: getMargin(left: 16, top: 13, bottom: 17), onTap: () { Navigator.pop(context); }), centerTitle: true, title: AppbarTitle(text: "Registration")), //AppBar(title: const Text('Registration')), body: SingleChildScrollView( child: Padding( padding: const EdgeInsets.all(16), child: Form( key: _formKey, child: Container( height: 500, child: Column( children: [ Padding( padding: getPadding(top: 19), child: Text("First Name", overflow: TextOverflow.ellipsis, textAlign: TextAlign.left, style: AppStyle.txtGilroyMedium16Bluegray900)), CustomTextFormField( focusNode: FocusNode(), onsaved: (value) => formData['first_name'] = value, validator: (value) { if (value == null || value.isEmpty) { return 'Please enter First Name'; } return null; }, hintText: "enter First Name", margin: getMargin(top: 6), padding: TextFormFieldPadding.PaddingT12, textInputType: TextInputType.text ), Padding( padding: getPadding(top: 19), child: Text("Last Name", overflow: TextOverflow.ellipsis, textAlign: TextAlign.left, style: AppStyle.txtGilroyMedium16Bluegray900)), CustomTextFormField( focusNode: FocusNode(), onsaved: (value) => formData['last_name'] = value, validator: (value) { if (value == null || value.isEmpty) { return 'Please enter Last Name'; } return null; }, hintText: "enter Last Name", margin: getMargin(top: 6), padding: TextFormFieldPadding.PaddingT12, textInputType: TextInputType.text ), Padding( padding: getPadding(top: 19), child: Text("Mobile Number", overflow: TextOverflow.ellipsis, textAlign: TextAlign.left, style: AppStyle.txtGilroyMedium16Bluegray900)), CustomTextFormField( focusNode: FocusNode(), textInputType: TextInputType.phone, inputFormatters: [ FilteringTextInputFormatter.allow(RegExp(r'[0-9]')) ], onsaved: (value) => formData['mob_no'] = value, validator: (value) { if (value == null || value.isEmpty) { return 'Please enter Mobile Number'; } return null; }, hintText: "enter Mobile Number", margin: getMargin(top: 6), padding: TextFormFieldPadding.PaddingT12, ), Padding( padding: getPadding(top: 19), child: Text("New Password", overflow: TextOverflow.ellipsis, textAlign: TextAlign.left, style: AppStyle.txtGilroyMedium16Bluegray900)), CustomTextFormField( focusNode: FocusNode(), hintText: "New Password", margin: getMargin(top: 6), padding: TextFormFieldPadding.PaddingT12, textInputAction: TextInputAction.done, textInputType:TextInputType.visiblePassword, validator: (value) { if (value == null || value.isEmpty) { return 'Please enter Password'; }else if(!_isPasswordValid){ return 'Please enter a valid password'; } return null; }, suffix: IconButton( icon: Icon( _newpasswordVisible ? Icons.visibility : Icons.visibility_off, ), onPressed: () { setState(() { _newpasswordVisible = !_newpasswordVisible; }); }, ), onsaved: (value) => formData['new_password'] = value, onChanged: (value) { setState(() { newPassword = value!; }); _validatePassword; }, suffixConstraints: BoxConstraints( maxHeight: getVerticalSize(44)), isObscureText: !_newpasswordVisible), Padding( padding: getPadding(top: 19), child: Text("Confirm Password", overflow: TextOverflow.ellipsis, textAlign: TextAlign.left, style: AppStyle.txtGilroyMedium16Bluegray900)), CustomTextFormField( focusNode: FocusNode(), hintText: "Confirm Password", margin: getMargin(top: 6), padding: TextFormFieldPadding.PaddingT12, textInputAction: TextInputAction.done, textInputType:TextInputType.visiblePassword, validator: (value) { if (value == null || value.isEmpty) { return 'Please enter Password'; } return _validatePasswordMatch(confirmPassword); }, onsaved: (value) => formData['confirm_password'] = value, onChanged: (value) { setState(() { confirmPassword = value!; // Update confirmPassword }); }, suffix: IconButton( icon: Icon( _confirmpasswordVisible ? Icons.visibility : Icons.visibility_off, ), onPressed: () { setState(() { _confirmpasswordVisible = !_confirmpasswordVisible; }); }, ), suffixConstraints: BoxConstraints( maxHeight: getVerticalSize(44)), isObscureText: !_confirmpasswordVisible,), Row( children: [ const Expanded( child: Text('Add Account'), ), IconButton( onPressed: () async { final accountId = await Navigator.push( context, MaterialPageRoute( builder: (context) => CreateAccountScreen(), ), ); if (accountId != null) { setState(() { selectedAccount = accountId; formData['account_id'] = accountId; account_id = accountId; // Update the account_id here }); } }, icon: const Icon(Icons.add), ), ], ), if (account_id != null) Container( margin: const EdgeInsets.symmetric(vertical: 5), // Add margin child: CustomButton( height: getVerticalSize(50), width: getHorizontalSize(396), text: "SUBMIT", margin: getMargin(top: 25), onTap: () async { if (_formKey.currentState!.validate()) { _formKey.currentState!.save(); print('formdata is $formData'); formData['usrGrpId'] = 46; formData['account_id'] = account_id; formData['email'] = widget.email; { final token = await TokenManager.getToken(); try { print(formData); await userService .createuser(token!, formData) .then((_) => { const LoginScreen(), }); 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'); } } } }, ), ), ], ), ), ), ), ), ); } }