nex_validation 0.0.3
nex_validation: ^0.0.3 copied to clipboard
A set of extension methods for performing common validations on strings in Dart applications.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:nex_validation/extensions/nex_validations_extensions.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Validation Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: ValidationScreen(),
);
}
}
class ValidationScreen extends StatefulWidget {
const ValidationScreen({super.key});
@override
State<ValidationScreen> createState() => _ValidationScreenState();
}
class _ValidationScreenState extends State<ValidationScreen> {
final _formKey = GlobalKey<FormState>();
final _nameController = TextEditingController();
final _emailController = TextEditingController();
final _phoneController = TextEditingController();
final _passwordController = TextEditingController();
String? _nameError;
String? _emailError;
String? _phoneError;
String? _passwordError;
void _validateForm() {
setState(() {
_nameError = _nameController.text.nameValidations(
min: 3,
emptyMsg: 'Name cannot be empty',
lengthMsg: 'Name must be between 3 and 20 characters',
validMsg: 'Name contains invalid characters',
);
_emailError = _emailController.text.emailValidations(
emptyMsg: 'Email address cannot be empty',
validMsg: 'Invalid email address format',
);
_phoneError = _phoneController.text.phoneNumberValidations(
emptyMsg: 'Phone number cannot be empty',
minLengthMsg: 'Phone number must be at least 5 digits long',
maxLengthMsg: 'Phone number cannot exceed maximum length',
);
_passwordError = _passwordController.text.passwordValidations(
min: 6,
max: 12,
emptyMsg: 'Password cannot be empty',
minLengthMsg: 'Password must be at least 6 characters long',
maxLengthMsg: 'Password cannot exceed 12 characters',
);
});
if (_nameError == null &&
_emailError == null &&
_phoneError == null &&
_passwordError == null) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Form submitted successfully!')),
);
}
}
Widget _buildTextField({
required String label,
required TextEditingController controller,
String? errorText,
bool obscure = false,
TextInputType type = TextInputType.text,
}) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextField(
controller: controller,
obscureText: obscure,
keyboardType: type,
decoration: InputDecoration(
labelText: label,
border: const OutlineInputBorder(),
errorText: errorText?.isEmpty == true ? null : errorText,
),
),
const SizedBox(height: 16),
],
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Validation Example')),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: SingleChildScrollView(
child: Column(
children: [
_buildTextField(
label: "Name",
controller: _nameController,
errorText: _nameError,
),
_buildTextField(
label: "Email",
controller: _emailController,
errorText: _emailError,
type: TextInputType.emailAddress,
),
_buildTextField(
label: "Phone Number",
controller: _phoneController,
errorText: _phoneError,
type: TextInputType.phone,
),
_buildTextField(
label: "Password",
controller: _passwordController,
errorText: _passwordError,
obscure: true,
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: _validateForm,
style: ElevatedButton.styleFrom(
minimumSize: const Size(double.infinity, 50),
),
child: const Text("Submit"),
),
],
),
),
),
),
);
}
}