Skip to content

Instantly share code, notes, and snippets.

@maheshj01
Last active May 29, 2023 17:03
Show Gist options
  • Save maheshj01/2a423b8d6ff5da3957da8f07836ca10b to your computer and use it in GitHub Desktop.
Save maheshj01/2a423b8d6ff5da3957da8f07836ca10b to your computer and use it in GitHub Desktop.
validator with custom input field
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: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
const MyHomePage({
Key? key,
required this.title,
}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
final List<GlobalKey<FormFieldState>> _formFieldKeys =
<GlobalKey<FormFieldState>>[
GlobalKey<FormFieldState>(),
GlobalKey<FormFieldState>(),
GlobalKey<FormFieldState>(),
GlobalKey<FormFieldState>(),
];
static FormFieldValidator<String> _minLengthValidator(int length) {
return (String? value) {
if (value == null || value.length < length) {
return 'fail';
}
return null;
};
}
void _onSubmit() {
if (!_formKey.currentState!.validate()) {
return;
}
}
bool _isValid() {
for (GlobalKey<FormFieldState> key in _formFieldKeys) {
final FormFieldState? state = key.currentState;
if (state == null || !state.isValid) {
return false;
}
}
return true;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Form(
key: _formKey,
onChanged: () {
setState(() {});
},
child: Column(
children: <Widget>[
CSInputField(
key: _formFieldKeys[0],
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: _minLengthValidator(5),
),
Column(
children: <Widget>[
CSInputField(
key: _formFieldKeys[1],
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: _minLengthValidator(5),
),
CSInputField(
key: _formFieldKeys[2],
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: _minLengthValidator(5),
),
],
),
CSInputField(
key: _formFieldKeys[3],
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: _minLengthValidator(5),
),
ElevatedButton(
onPressed: _isValid() ? _onSubmit : null,
child: const Text('Submit'),
),
],
),
),
],
),
),
);
}
}
class CSInputField extends StatefulWidget {
CSInputField(
{Key? key,
this.hasLabel = false,
this.hint,
this.autovalidateMode,
this.validator}):super(key:key);
final bool hasLabel;
final String? hint;
final AutovalidateMode? autovalidateMode;
String? Function(String?)? validator;
@override
State<CSInputField> createState() => _CSInputFieldState();
}
class _CSInputFieldState extends State<CSInputField> {
@override
Widget build(BuildContext context) {
return TextFormField(
keyboardType: TextInputType.visiblePassword,
autovalidateMode: widget.autovalidateMode,
validator: widget.validator,
);
}
}
@maheshj01
Copy link
Author

_isValid() method returns false because state is null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment