Add an onChanged callback to InputFormField. (#7398)

Fixes https://github.com/flutter/flutter/issues/7210
This commit is contained in:
Matt Perry
2017-01-09 15:43:16 -08:00
committed by GitHub
parent 7c9f9be363
commit 12d5b26c3a
2 changed files with 37 additions and 1 deletions

View File

@@ -629,6 +629,7 @@ class InputFormField extends FormField<InputValue> {
InputValue initialValue: InputValue.empty,
FormFieldSetter<InputValue> onSaved,
FormFieldValidator<InputValue> validator,
ValueChanged<InputValue> onChanged,
}) : super(
key: key,
initialValue: initialValue,
@@ -647,7 +648,11 @@ class InputFormField extends FormField<InputValue> {
autofocus: autofocus,
maxLines: maxLines,
value: field.value,
onChanged: field.onChanged,
onChanged: (InputValue value) {
field.onChanged(value);
if (onChanged != null)
onChanged(value);
},
errorText: field.errorText,
);
},

View File

@@ -54,6 +54,37 @@ void main() {
await checkText('');
});
testWidgets('onChanged callback is called', (WidgetTester tester) async {
String fieldValue;
Widget builder() {
return new Center(
child: new Material(
child: new Form(
child: new InputFormField(
onChanged: (InputValue value) { fieldValue = value.text; },
),
)
)
);
}
await tester.pumpWidget(builder());
await showKeyboard(tester);
expect(fieldValue, isNull);
Future<Null> checkText(String testValue) async {
enterText(testValue);
await tester.idle();
// pump'ing is unnecessary because callback happens regardless of frames
expect(fieldValue, equals(testValue));
}
await checkText('Test');
await checkText('');
});
testWidgets('Validator sets the error text only when validate is called', (WidgetTester tester) async {
GlobalKey<FormState> formKey = new GlobalKey<FormState>();
GlobalKey inputKey = new GlobalKey();