import 'package:flutter/material.dart';
class UserRegistration extends StatefulWidget {
@override
State<UserRegistration> createState() => _UserRegistrationState();
}
class _UserRegistrationState extends State<UserRegistration> {
final _formKey = GlobalKey<FormState>();
final _nameController = TextEditingController();
final _emailController = TextEditingController();
final _passwordController = TextEditingController();
bool _autoValidate = false;
bool get _isFormValid {
return _formKey.currentState?.validate() ?? false;
}
void _submit() {
setState(() {
_autoValidate = true;
});
if (_formKey.currentState?.validate() ?? false) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Registration Successful!')),
);
}
}
@override
void dispose() {
_nameController.dispose();
_emailController.dispose();
_passwordController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('User Registration')),
body: Padding(
padding: EdgeInsets.all(16),
child: Form(
key: _formKey,
autovalidateMode: _autoValidate ? AutovalidateMode.always : AutovalidateMode.disabled,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextFormField(
controller: _nameController,
decoration: InputDecoration(labelText: 'Name'),
validator: (value) {
if (value == null || value.trim().isEmpty) {
return 'Name cannot be empty';
}
return null;
},
),
SizedBox(height: 12),
TextFormField(
controller: _emailController,
decoration: InputDecoration(labelText: 'Email'),
keyboardType: TextInputType.emailAddress,
validator: (value) {
if (value == null || value.trim().isEmpty) {
return 'Email cannot be empty';
}
if (!value.contains('@') || !value.contains('.')) {
return 'Enter a valid email';
}
return null;
},
),
SizedBox(height: 12),
TextFormField(
controller: _passwordController,
decoration: InputDecoration(labelText: 'Password'),
obscureText: true,
validator: (value) {
if (value == null || value.length < 6) {
return 'Password must be at least 6 characters';
}
return null;
},
),
SizedBox(height: 24),
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: _isFormValid ? _submit : null,
child: Text('Submit'),
),
),
],
),
),
),
);
}
}
This screen uses a Form widget with TextFormFields for Name, Email, and Password. Each field has a validator function that checks the input:
- Name must not be empty.
- Email must contain '@' and '.' to be considered valid.
- Password must be at least 6 characters long.
The Submit button is disabled if the form is invalid, preventing bad data submission. When the user presses Submit, the form validates again and shows a success message if all data is good.
This validation ensures the app only accepts quality data, avoiding errors later and improving user experience.