Checkbox validation added

This commit is contained in:
VINISTAN 2024-11-12 20:16:43 +05:30
parent 4efcd311ba
commit cc24efd8cd
2 changed files with 59 additions and 286 deletions

View File

@ -33,7 +33,8 @@ class _ProfileScreenState extends State<ProfileScreen> {
'Canada'
];
String? _selectedCountry;
bool _termsAccepted = false;
bool isChecked = false;
bool showError = false;
final _picker = ImagePicker();
File? _profileImage;
@ -145,7 +146,7 @@ class _ProfileScreenState extends State<ProfileScreen> {
void _toggleCheckbox(bool? value) {
setState(() {
_termsAccepted = value ?? false;
isChecked = value ?? false;
});
}
@ -164,7 +165,7 @@ class _ProfileScreenState extends State<ProfileScreen> {
// String email = 'surendarsuri30@gmail.com';
String dateOfBirth = _dateController.text; // in DD/MM/YYYY format
String countryRegion = _selectedCountry ?? '';
bool termsAccepted = _termsAccepted;
bool termsAccepted = isChecked;
// Parse the date from dd/MM/yyyy format and convert to yyyy-MM-dd
// Parse the date from dd/MM/yyyy format and format to yyyy-MM-dd
@ -230,7 +231,7 @@ class _ProfileScreenState extends State<ProfileScreen> {
_fullNameController.clear();
_dateController.clear();
_selectedCountry = null;
_termsAccepted = false;
isChecked = false;
// Reset profile image
_profileImage = null;
@ -440,8 +441,17 @@ class _ProfileScreenState extends State<ProfileScreen> {
Row(
children: [
Checkbox(
value: _termsAccepted,
onChanged: _toggleCheckbox,
value: isChecked,
onChanged: (value) {
setState(() {
isChecked = value ?? false;
showError = false;
});
},
side: BorderSide(
color: showError ? Colors.red : Colors.grey,
width: 1.5,
),
),
Expanded(
child: Column(
@ -498,6 +508,23 @@ class _ProfileScreenState extends State<ProfileScreen> {
),
],
),
if (showError)
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(left: 10.0),
child: Text(
'Required',
style: TextStyle(
color: Colors.red[700],
fontSize: 12,
),
),
),
],
),
SizedBox(height: 20),
Center(
child: Text(
@ -510,18 +537,34 @@ class _ProfileScreenState extends State<ProfileScreen> {
SizedBox(height: 20),
ElevatedButton.icon(
onPressed: () {
if (_formKey.currentState?.validate() ?? false) {
if((_formKey.currentState?.validate() ?? false) && (isChecked)){
_formKey.currentState?.save();
if (_termsAccepted) {
_formKey.currentState?.save();
// _confirmSaveProfile();
showConfirmationDialog(context);
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Please accept the terms and conditions.',style: TextStyle(color: Colors.white),)),
);
}
showConfirmationDialog(context);
}
else {
setState(() {
showError = !isChecked; // Show error if the checkbox is not checked
});
}
// if ((_formKey.currentState?.validate() ?? false) && (isChecked)) {
// _formKey.currentState?.save();
// showConfirmationDialog(context);
// // if (isChecked) {
// // _formKey.currentState?.save();
// // // _confirmSaveProfile();
// // showConfirmationDialog(context);
// // }
// else {
// setState(() {
// showError = !isChecked; // Show error if the checkbox is not checked
// });
// ScaffoldMessenger.of(context).showSnackBar(
// SnackBar(content: Text('Please accept the terms and conditions.',style: TextStyle(color: Colors.white),)),
// );
//}
//}
},
icon: Icon(
Icons.save,

View File

@ -1,273 +1,3 @@
// import 'package:flutter/material.dart';
//
// class RegisterScreen extends StatefulWidget {
//
// @override
// _RegisterScreenState createState() => _RegisterScreenState();
// }
//
// class _RegisterScreenState extends State<RegisterScreen> {
// final _formKey = GlobalKey<FormState>();
// bool _obscurePassword = true;
// bool _obscureConfirmPassword = true;
// bool isChecked = false;
// bool showError = false;
//
// String? _validateUsername(String? value) {
// if (value == null || value.isEmpty) {
// return 'Required';
// } else if (RegExp(r'[^a-zA-Z0-9]').hasMatch(value)) {
// return 'Invalid Characters';
// }
// return null;
// }
//
// String? _validateEmail(String? value) {
// if (value == null || value.isEmpty) {
// return 'Required';
// } else if (!RegExp(r'^[^@]+@[^@]+\.[^@]+').hasMatch(value)) {
// return 'Invalid Email';
// }
// return null;
// }
//
// String? _validatePassword(String? value) {
// if (value == null || value.isEmpty) {
// return 'Required';
// } else if (value.length < 6) {
// return 'Password must be at least 6 characters';
// }
// return null;
// }
//
// @override
// Widget build(BuildContext context) {
// double screenheight = MediaQuery.of(context).size.height;
// double screenwidth = MediaQuery.of(context).size.width;
// return Scaffold(
// backgroundColor: Colors.white,
// body: Padding(
// padding: const EdgeInsets.all(20.0),
// child: Form(
// key: _formKey,
// child: SingleChildScrollView(
// child: Column(
// mainAxisAlignment: MainAxisAlignment.center,
// children: [
// SizedBox(height: screenheight / 6),
// Text(
// 'Register',
// style: TextStyle(fontSize: 32, fontWeight: FontWeight.w400),
// ),
// SizedBox(height: 10),
// Text('Please enter your details'),
// SizedBox(height: 20),
// TextFormField(
// decoration: InputDecoration(
// hintText: 'Username',
// prefixIcon: Icon(
// Icons.person,
// color: Colors.blue,
// ),
// border: OutlineInputBorder(),
// ),
// validator: _validateUsername,
// ),
// SizedBox(height: 15),
// TextFormField(
// decoration: InputDecoration(
// hintText: 'Enter your email',
// prefixIcon: Icon(
// Icons.email,
// color: Colors.blue,
// ),
// border: OutlineInputBorder(),
// ),
// validator: _validateEmail,
// ),
// SizedBox(height: 15),
// TextFormField(
// obscureText: _obscurePassword,
// decoration: InputDecoration(
// hintText: 'Enter your password',
// prefixIcon: Icon(
// Icons.lock,
// color: Colors.blue,
// ),
// suffixIcon: IconButton(
// icon: Icon(
// _obscurePassword
// ? Icons.visibility
// : Icons.visibility_off,
// color: Colors.blue,
// ),
// onPressed: () {
// setState(() {
// _obscurePassword = !_obscurePassword;
// });
// },
// ),
// border: OutlineInputBorder(),
// ),
// validator: _validatePassword,
// ),
// SizedBox(height: 15),
// TextFormField(
// obscureText: _obscureConfirmPassword,
// decoration: InputDecoration(
// hintText: 'Confirm password',
// prefixIcon: Icon(
// Icons.lock,
// color: Colors.blue,
// ),
// suffixIcon: IconButton(
// icon: Icon(
// _obscureConfirmPassword
// ? Icons.visibility
// : Icons.visibility_off,
// color: Colors.blue,
// ),
// onPressed: () {
// setState(() {
// _obscureConfirmPassword = !_obscureConfirmPassword;
// });
// },
// ),
// border: OutlineInputBorder(),
// ),
// validator: _validatePassword,
// ),
// SizedBox(height: 10),
// Row(
// children: [
// Checkbox(
// value: isChecked,
// onChanged: (value) {
// setState(() {
// isChecked = value ?? false;
// showError = false;
// });
// },
// side: BorderSide(
// color: showError ? Colors.red : Colors.grey,
// width: 1.5,
// ),
// ),
// Expanded(
// child: Text.rich(
// TextSpan(
// text: 'I agree to ',
// children: [
// TextSpan(
// text: 'Terms & Conditions',
// style: TextStyle(color: Colors.blue),
// ),
// TextSpan(text: ' and '),
// TextSpan(
// text: 'Privacy Policy',
// style: TextStyle(color: Colors.blue),
// ),
// ],
// ),
// ),
// ),
// ],
// ),
// if (showError)
// Row(
// mainAxisAlignment: MainAxisAlignment.start,
// children: [
// Padding(
// padding: const EdgeInsets.only(left: 10.0),
// child: Text(
// 'Required',
// style: TextStyle(
// color: Colors.red[700],
// fontSize: 12,
// ),
// ),
// ),
// ],
// ),
// SizedBox(height: 20),
// SizedBox(
// width: screenwidth/1.3,
// child: ElevatedButton(
// onPressed: () {
// if ((_formKey.currentState?.validate() ??
// false )&& isChecked) {
// // Proceed with registration if form is valid and checkbox is checked
// } else {
// setState(() {
// showError =
// !isChecked; // Show error if the checkbox is not checked
// });
// }
// },
// style: ElevatedButton.styleFrom(
// backgroundColor: Color(0xFFA7887A),
// // Brownish color for Register
//
// shape: RoundedRectangleBorder(
// borderRadius: BorderRadius.circular(20),
// ),
// ),
// child: Row(
// mainAxisSize: MainAxisSize.min,
// children: [
// Text(
// "Register",
// style: TextStyle(fontSize: 16, color: Colors.white),
// ),
// SizedBox(width: 8),
// Icon(Icons.arrow_forward, color: Colors.white),
// ],
// ),
// ),
// ),
// SizedBox(height: 10),
// Text(
// "Already have an account?",
// style: TextStyle(fontSize: 16, color: Colors.grey[600]),
// ),
// SizedBox(height: 10),
// SizedBox(
// width: screenwidth/1.3,
// child: ElevatedButton(
// onPressed: () {
// // Add your login logic here
// },
// style: ElevatedButton.styleFrom(
// backgroundColor: Color(0xFF82AFCB),
// // Blueish color for Login
// shape: RoundedRectangleBorder(
// borderRadius: BorderRadius.circular(20),
// ),
// ),
// child: Row(
// mainAxisSize: MainAxisSize.min,
// children: [
// Text(
// "Login",
// style: TextStyle(fontSize: 16, color: Colors.white),
// ),
// SizedBox(width: 8),
// Icon(Icons.arrow_forward, color: Colors.white),
// ],
// ),
// ),
// ),
// ],
// ),
// ),
// ),
// ),
// );
// }
// }
import 'package:flutter/material.dart';
import 'package:uae_stat/presentation/Screens/profilepage.dart';