diff --git a/examples/flutter_gallery/lib/demo/material/text_form_field_demo.dart b/examples/flutter_gallery/lib/demo/material/text_form_field_demo.dart index f1bbafe2c8..77421b0044 100644 --- a/examples/flutter_gallery/lib/demo/material/text_form_field_demo.dart +++ b/examples/flutter_gallery/lib/demo/material/text_form_field_demo.dart @@ -184,6 +184,7 @@ class TextFormFieldDemoState extends State { labelText: 'Re-type Password *', ), obscureText: true, + onFieldSubmitted: (String value) { _handleSubmitted(); }, validator: _validatePassword, ), ), diff --git a/packages/flutter/lib/src/material/text_form_field.dart b/packages/flutter/lib/src/material/text_form_field.dart index df6e85eff9..a49fddbe66 100644 --- a/packages/flutter/lib/src/material/text_form_field.dart +++ b/packages/flutter/lib/src/material/text_form_field.dart @@ -53,6 +53,7 @@ class TextFormField extends FormField { bool obscureText: false, bool autocorrect: true, int maxLines: 1, + ValueChanged onFieldSubmitted, FormFieldSetter onSaved, FormFieldValidator validator, List inputFormatters, @@ -82,6 +83,7 @@ class TextFormField extends FormField { autocorrect: autocorrect, maxLines: maxLines, onChanged: field.onChanged, + onSubmitted: onFieldSubmitted, inputFormatters: inputFormatters, ); }, diff --git a/packages/flutter/test/material/text_form_field_test.dart b/packages/flutter/test/material/text_form_field_test.dart index 871e8b9bde..921905dd75 100644 --- a/packages/flutter/test/material/text_form_field_test.dart +++ b/packages/flutter/test/material/text_form_field_test.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { @@ -27,4 +28,25 @@ void main() { final TextField textFieldWidget = tester.widget(textFieldFinder); expect(textFieldWidget.textAlign, alignment); }); + + testWidgets('onFieldSubmit callbacks are called', (WidgetTester tester) async { + bool _called = false; + + await tester.pumpWidget( + new MaterialApp( + home: new Material( + child: new Center( + child: new TextFormField( + onFieldSubmitted: (String value) { _called = true; }, + ), + ), + ), + ), + ); + + await tester.showKeyboard(find.byType(TextField)); + tester.testTextInput.receiveAction(TextInputAction.done); + await tester.pump(); + expect(_called, true); + }); } diff --git a/packages/flutter_test/lib/src/test_text_input.dart b/packages/flutter_test/lib/src/test_text_input.dart index d2aefebee4..436a1c8cb8 100644 --- a/packages/flutter_test/lib/src/test_text_input.dart +++ b/packages/flutter_test/lib/src/test_text_input.dart @@ -106,6 +106,27 @@ class TestTextInput { )); } + /// Simulates the user pressing one of the [TextInputAction] buttons. + /// Does not check that the [TextInputAction] performed is an acceptable one + /// based on the `inputAction` [setClientArgs]. + void receiveAction(TextInputAction action) { + // Not using the `expect` function because in the case of a FlutterDriver + // test this code does not run in a package:test test zone. + if (_client == 0) { + throw new TestFailure('_client must be non-zero'); + } + BinaryMessages.handlePlatformMessage( + SystemChannels.textInput.name, + SystemChannels.textInput.codec.encodeMethodCall( + new MethodCall( + 'TextInputClient.performAction', + [_client, action.toString()], + ), + ), + (ByteData data) { /* response from framework is discarded */ }, + ); + } + /// Simulates the user hiding the onscreen keyboard. void hide() { _isVisible = false;