这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/flutter/lib/src/widgets/form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ class FormField<T> extends StatefulWidget {
super.key,
required this.builder,
this.onSaved,
this.onReset,
this.forceErrorText,
this.validator,
this.errorBuilder,
Expand All @@ -485,6 +486,10 @@ class FormField<T> extends StatefulWidget {
/// [FormState.save].
final FormFieldSetter<T>? onSaved;

/// An optional method to call when the form field is reset via
/// [FormFieldState.reset].
final VoidCallback? onReset;

/// An optional property that forces the [FormFieldState] into an error state
/// by directly setting the [FormFieldState.errorText] property without
/// running the validator function.
Expand Down Expand Up @@ -631,6 +636,7 @@ class FormFieldState<T> extends State<FormField<T>> with RestorationMixin {
_hasInteractedByUser.value = false;
_errorText.value = null;
});
widget.onReset?.call();
Form.maybeOf(context)?._fieldDidChange();
}

Expand Down
26 changes: 26 additions & 0 deletions packages/flutter/test/widgets/form_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,32 @@ void main() {
await checkText('');
});

testWidgets('onReset callback is called', (WidgetTester tester) async {
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
bool resetCalled = false;

await tester.pumpWidget(
MaterialApp(
home: Form(
key: formKey,
child: FormField<String>(
builder: (_) => const SizedBox.shrink(),
onReset: () {
resetCalled = true;
},
),
),
),
);

expect(resetCalled, isFalse);

formKey.currentState!.reset();
await tester.pump();

expect(resetCalled, isTrue);
});

testWidgets('Validator sets the error text only when validate is called', (
WidgetTester tester,
) async {
Expand Down