flutter_validators 1.1.0
flutter_validators: ^1.1.0 copied to clipboard
A pure dart package of String validators and sanitizers. Inspired by validator.js.
import 'package:flutter/material.dart';
import 'package:flutter_validators/flutter_validators.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Validators Example',
theme: ThemeData(
primarySwatch: Colors.blue,
inputDecorationTheme: const InputDecorationTheme(
border: OutlineInputBorder(),
),
),
home: const ExampleScreen(),
);
}
}
class ExampleScreen extends StatefulWidget {
const ExampleScreen({super.key});
@override
State<ExampleScreen> createState() => _ExampleScreenState();
}
class _ExampleScreenState extends State<ExampleScreen> {
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Flutter Validators Example')),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: Column(
children: [
TextFormField(
decoration: const InputDecoration(labelText: 'Email'),
validator: Validator.email(),
autovalidateMode: AutovalidateMode.onUserInteraction,
),
const SizedBox(height: 16),
TextFormField(
decoration: const InputDecoration(labelText: 'URL'),
validator: Validator.url(errorMessage: 'Please provide a valid URL'),
autovalidateMode: AutovalidateMode.onUserInteraction,
),
const SizedBox(height: 16),
TextFormField(
decoration: const InputDecoration(labelText: 'Credit Card'),
validator: Validator.creditCard(),
autovalidateMode: AutovalidateMode.onUserInteraction,
),
const SizedBox(height: 16),
TextFormField(
decoration: const InputDecoration(labelText: 'Age (Numeric)'),
validator: Validator.numeric(),
autovalidateMode: AutovalidateMode.onUserInteraction,
),
const SizedBox(height: 32),
ElevatedButton(
onPressed: () {
if (_formKey.currentState!.validate()) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Form is valid!')),
);
}
},
child: const Text('Submit'),
),
],
),
),
),
);
}
}