auto_form_validate 1.0.2 auto_form_validate: ^1.0.2 copied to clipboard
Auto Form simplifies form management in Flutter apps by offering intuitive manipulation and input validation. It also supports the use of masks for custom data formatting, ensuring a more efficient an [...]
import 'dart:developer';
import 'package:auto_form_validate/auto_form_validate.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'AutoForm Example',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final formKey = GlobalKey<FormState>();
String phone = '';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('AutoForm Example'),
),
body: Padding(
padding: const EdgeInsets.all(20),
child: Form(
key: formKey,
child: Column(
children: [
//👋
AutoTextFormField(
formController: PhoneMandatory(context),
onChanged: (value) => phone = value,
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: () {
if (formKey.currentState!.validate()) {
log("Phone: $phone ✅");
}
},
child: const Text("Validate")),
],
),
),
),
);
}
}
class PhoneMandatory extends FormController {
PhoneMandatory(super.context);
@override
String? Function(String? value)? get validator => (value) {
if (value == null || value.isEmpty) {
return 'This field is required';
}
return null;
};
@override
List<String> get formaters => ["(##) ####-####"];
@override
TextInputType? get textInputType => TextInputType.number;
@override
RegExp get regexFilter => RegExp(r'[0-9]');
}