From f6da3b2a959149e0263ca31b4cd6e6147354e799 Mon Sep 17 00:00:00 2001 From: Matt Perry Date: Tue, 27 Sep 2016 17:11:22 -0400 Subject: [PATCH] Add a Submit button to text fields demo. (#6102) Fixes https://github.com/flutter/flutter/issues/6056 --- .../lib/demo/text_field_demo.dart | 24 +++++++++++++++---- packages/flutter/lib/src/material/input.dart | 3 ++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/examples/flutter_gallery/lib/demo/text_field_demo.dart b/examples/flutter_gallery/lib/demo/text_field_demo.dart index ae6f6d52ff..1826e63d82 100644 --- a/examples/flutter_gallery/lib/demo/text_field_demo.dart +++ b/examples/flutter_gallery/lib/demo/text_field_demo.dart @@ -14,9 +14,9 @@ class TextFieldDemo extends StatefulWidget { } class PersonData { - String name; - String phoneNumber; - String password; + String name = ''; + String phoneNumber = ''; + String password = ''; } class TextFieldDemoState extends State { @@ -31,7 +31,14 @@ class TextFieldDemoState extends State { } 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) { @@ -66,7 +73,6 @@ class TextFieldDemoState extends State { title: new Text('Text fields') ), body: new Form( - onSubmitted: _handleSubmitted, child: new Block( padding: const EdgeInsets.all(8.0), children: [ @@ -112,6 +118,14 @@ class TextFieldDemoState extends State { ) ) ] + ), + new Container( + padding: const EdgeInsets.all(20.0), + align: const FractionalOffset(0.5, 0.5), + child: new RaisedButton( + child: new Text('SUBMIT'), + onPressed: _handleSubmitted, + ), ) ] ) diff --git a/packages/flutter/lib/src/material/input.dart b/packages/flutter/lib/src/material/input.dart index 79c4209570..c2b7850fe7 100644 --- a/packages/flutter/lib/src/material/input.dart +++ b/packages/flutter/lib/src/material/input.dart @@ -290,7 +290,8 @@ class _FormFieldData { void onSubmitted(InputValue value) { FormScope scope = FormScope.of(inputState.context); assert(scope != null); - scope.form.onSubmitted(); + if (scope.form.onSubmitted != null) + scope.form.onSubmitted(); scope.onFieldChanged(); } }