Add an onChanged callback to InputFormField. (#7398)
Fixes https://github.com/flutter/flutter/issues/7210
This commit is contained in:
@@ -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,
|
||||
);
|
||||
},
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user