This repository has been archived by the owner on Sep 3, 2022. It is now read-only.
/
main.dart
108 lines (102 loc) · 3.33 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import 'package:flutter/material.dart';
import 'package:queen_validators/queen_validators.dart';
Future<void> main() async {
runApp(
MaterialApp(
title: 'Queen Validators 👑',
debugShowCheckedModeBanner: false,
home: HomePage(),
theme: ThemeData.light(),
),
);
}
class HomePage extends StatelessWidget {
HomePage({Key? key}) : super(key: key);
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Queen Validators 👑'),
centerTitle: true,
),
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.check),
onPressed: () async {
final isFormValid = _formKey.currentState!.validate();
if (isFormValid) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('the form is valid ✔'),
),
);
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('not valid form ❌'),
),
);
}
},
),
body: Form(
key: _formKey,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: ListView(
children: [
TextFormField(
decoration: const InputDecoration(labelText: 'IsRequired'),
validator: qValidator([
// if textFeild trimmed value lenght > 0 it will pass
IsRequired(),
]),
),
TextFormField(
decoration: const InputDecoration(labelText: 'IsOptional'),
validator: qValidator([
// if the textField contains value the rest of the validators
// will run else it will pass alidation with checking them
IsOptional(),
/// the input value must be a
/// valid (`well formatted`) email address
const IsEmail(),
]),
),
TextFormField(
decoration:
const InputDecoration(labelText: 'IsRequired AND IsEmail'),
validator: qValidator([
IsRequired(),
/// the input value must be a valid (`well formatted`)
/// email address
const IsEmail(),
]),
),
TextFormField(
decoration:
const InputDecoration(labelText: 'MinLenght AND IsEmail'),
validator: qValidator([
IsRequired(),
/// the input min length must be >= 5
MinLength(10),
/// the input max length must be <= 10
MaxLength(15),
]),
),
TextFormField(
decoration:
const InputDecoration(labelText: 'IsIn AND IsNotIn'),
validator: qValidator([
IsRequired(),
IsIn(['white', 'black', 'gray']),
IsNotIn(['red', 'blue', 'orange']),
]),
),
],
),
),
),
);
}
}