adding validation and toast for login issue

This commit is contained in:
2026-09-18 18:23:17 +00:00
parent 3f74a779f3
commit 334efa1de5
@@ -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<RegistrationDetailsScreen> {
final Map<String, dynamic> formData = {};
final _formKey = GlobalKey<FormState>();
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,10 +55,13 @@ class _RegistrationDetailsScreenState extends State<RegistrationDetailsScreen> {
bool _isPasswordValid = true;
void _validatePassword(String password) {
final valid = password.isNotEmpty;
if (valid != _isPasswordValid) {
setState(() {
_isPasswordValid = password.isNotEmpty;
_isPasswordValid = valid;
});
}
}
bool _isEmailValid = true;
void _validateEmail(String email) {
@@ -86,12 +94,25 @@ class _RegistrationDetailsScreenState extends State<RegistrationDetailsScreen> {
@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<RegistrationDetailsScreen> {
padding: const EdgeInsets.all(16),
child: Form(
key: _formKey,
child: Container(
height: 500,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: getPadding(top: 19),
@@ -124,7 +144,7 @@ class _RegistrationDetailsScreenState extends State<RegistrationDetailsScreen> {
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<RegistrationDetailsScreen> {
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<RegistrationDetailsScreen> {
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<RegistrationDetailsScreen> {
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<RegistrationDetailsScreen> {
),
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<RegistrationDetailsScreen> {
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<RegistrationDetailsScreen> {
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,17 +315,33 @@ class _RegistrationDetailsScreenState extends State<RegistrationDetailsScreen> {
],
),
if (account_id != null)
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()) {
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');
@@ -318,30 +350,28 @@ class _RegistrationDetailsScreenState extends State<RegistrationDetailsScreen> {
formData['account_id'] = account_id;
formData['email'] = widget.email;
{
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(
if (!context.mounted) return;
showSuccessMessage(
'User created successfully');
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (context) =>
const LoginScreen()));
// moveToNextStep();
const LoginScreen()),
(route) => false);
} catch (e) {
showErrorMessage('Failed to create User: $e');
}
if (!context.mounted) return;
showErrorMessage(
'Failed to create User: $e');
} finally {
if (mounted) {
setState(() => _isSubmitting = false);
}
}
},
@@ -352,7 +382,6 @@ class _RegistrationDetailsScreenState extends State<RegistrationDetailsScreen> {
),
),
),
),
);
}
}
}