Commit f6da3b2a authored by Matt Perry's avatar Matt Perry Committed by GitHub

Add a Submit button to text fields demo. (#6102)

Fixes https://github.com/flutter/flutter/issues/6056
parent a426b6b3
...@@ -14,9 +14,9 @@ class TextFieldDemo extends StatefulWidget { ...@@ -14,9 +14,9 @@ class TextFieldDemo extends StatefulWidget {
} }
class PersonData { class PersonData {
String name; String name = '';
String phoneNumber; String phoneNumber = '';
String password; String password = '';
} }
class TextFieldDemoState extends State<TextFieldDemo> { class TextFieldDemoState extends State<TextFieldDemo> {
...@@ -31,7 +31,14 @@ class TextFieldDemoState extends State<TextFieldDemo> { ...@@ -31,7 +31,14 @@ class TextFieldDemoState extends State<TextFieldDemo> {
} }
void _handleSubmitted() { void _handleSubmitted() {
showInSnackBar('${person.name}\'s phone number is ${person.phoneNumber}'); // TODO(mpcomplete): Form could keep track of validation errors?
if (_validateName(person.name) != null ||
_validatePhoneNumber(person.phoneNumber) != null ||
_validatePassword(person.password) != null) {
showInSnackBar('Please fix the errors in red before submitting.');
} else {
showInSnackBar('${person.name}\'s phone number is ${person.phoneNumber}');
}
} }
String _validateName(String value) { String _validateName(String value) {
...@@ -66,7 +73,6 @@ class TextFieldDemoState extends State<TextFieldDemo> { ...@@ -66,7 +73,6 @@ class TextFieldDemoState extends State<TextFieldDemo> {
title: new Text('Text fields') title: new Text('Text fields')
), ),
body: new Form( body: new Form(
onSubmitted: _handleSubmitted,
child: new Block( child: new Block(
padding: const EdgeInsets.all(8.0), padding: const EdgeInsets.all(8.0),
children: <Widget>[ children: <Widget>[
...@@ -112,6 +118,14 @@ class TextFieldDemoState extends State<TextFieldDemo> { ...@@ -112,6 +118,14 @@ class TextFieldDemoState extends State<TextFieldDemo> {
) )
) )
] ]
),
new Container(
padding: const EdgeInsets.all(20.0),
align: const FractionalOffset(0.5, 0.5),
child: new RaisedButton(
child: new Text('SUBMIT'),
onPressed: _handleSubmitted,
),
) )
] ]
) )
......
...@@ -290,7 +290,8 @@ class _FormFieldData { ...@@ -290,7 +290,8 @@ class _FormFieldData {
void onSubmitted(InputValue value) { void onSubmitted(InputValue value) {
FormScope scope = FormScope.of(inputState.context); FormScope scope = FormScope.of(inputState.context);
assert(scope != null); assert(scope != null);
scope.form.onSubmitted(); if (scope.form.onSubmitted != null)
scope.form.onSubmitted();
scope.onFieldChanged(); scope.onFieldChanged();
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment