Skip to content

Instantly share code, notes, and snippets.

@Andrious
Last active September 21, 2020 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Andrious/863cc93191d9b479233783413706304d to your computer and use it in GitHub Desktop.
Save Andrious/863cc93191d9b479233783413706304d to your computer and use it in GitHub Desktop.
Build a form with validation
// TextFormField & Form Validation.
// https://flutter.dev/docs/cookbook/forms/validation
class FormValidation extends StatelessWidget {
@override
Widget build(BuildContext context) {
final appTitle = 'Form Validation Demo';
return MaterialApp(
title: appTitle,
home: Scaffold(
appBar: AppBar(
title: Text(appTitle),
),
body: _FormValidationForm(),
),
);
}
}
// Create a Form widget.
class _FormValidationForm extends StatefulWidget {
@override
State createState() => _FormValidationState();
}
// Create a corresponding State class.
// This class holds data related to the form.
class _FormValidationState extends State<_FormValidationForm> {
// Create a global key that uniquely identifies the Form widget
// and allows validation of the form.
//
// Note: This is a GlobalKey<FormState>,
// not a GlobalKey<MyCustomFormState>.
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
// Build a Form widget using the _formKey created above.
return Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
TextFormField(
validator: (value) {
if (value.isEmpty) {
return 'Please enter some text';
}
return null;
},
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: RaisedButton(
onPressed: () {
// Validate returns true if the form is valid, or false
// otherwise.
final FormState formState = _formKey.currentState;
if (formState.validate()) {
formState.save();
// If the form is valid, display a Snackbar.
Scaffold.of(context)
.showSnackBar(SnackBar(content: const Text('Processing Data')));
}
},
child: const Text('Submit'),
),
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment