Forms provide more control over when they validate. (#7283)

Callers can manually validate by calling validate(), or tell the Form to
validate on every change by setting the `autovalidate` parameter.

Fixes https://github.com/flutter/flutter/issues/7219
This commit is contained in:
Matt Perry
2016-12-16 18:14:37 -05:00
committed by GitHub
parent 0d746ff155
commit 6d4191e98c
3 changed files with 80 additions and 24 deletions

View File

@@ -30,11 +30,13 @@ class TextFieldDemoState extends State<TextFieldDemo> {
));
}
bool _autovalidate = false;
GlobalKey<FormState> _formKey = new GlobalKey<FormState>();
GlobalKey<FormFieldState<InputValue>> _passwordFieldKey = new GlobalKey<FormFieldState<InputValue>>();
void _handleSubmitted() {
FormState form = _formKey.currentState;
if (form.hasErrors) {
if (!form.validate()) {
_autovalidate = true; // Start validating on every change.
showInSnackBar('Please fix the errors in red before submitting.');
} else {
form.save();
@@ -76,6 +78,7 @@ class TextFieldDemoState extends State<TextFieldDemo> {
),
body: new Form(
key: _formKey,
autovalidate: _autovalidate,
child: new Block(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
children: <Widget>[