Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add onTapOutside to FormBuilderTextField #1267

Merged
merged 2 commits into from
Jun 22, 2023
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
13 changes: 13 additions & 0 deletions lib/src/fields/form_builder_text_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,17 @@ class FormBuilderTextField extends FormBuilderFieldDecoration<String> {
/// {@endtemplate}
final GestureTapCallback? onTap;

/// {@template flutter.material.textfield.onTapOutside}
/// A callback to be invoked when a tap is detected outside of this TapRegion
/// and any other region with the same groupId, if any.
///
/// The PointerDownEvent passed to the function is the event that caused the
/// notification. If this region is part of a group (i.e. groupId is set),
/// then it's possible that the event may be outside of this immediate region,
/// although it will be within the region of one of the group members.
/// {@endtemplate}
final TapRegionCallback? onTapOutside;

/// The cursor for a mouse pointer when it enters or is hovering over the
/// widget.
///
Expand Down Expand Up @@ -322,6 +333,7 @@ class FormBuilderTextField extends FormBuilderFieldDecoration<String> {
this.minLines,
this.showCursor,
this.onTap,
this.onTapOutside,
this.enableSuggestions = false,
this.textAlignVertical,
this.dragStartBehavior = DragStartBehavior.start,
Expand Down Expand Up @@ -381,6 +393,7 @@ class FormBuilderTextField extends FormBuilderFieldDecoration<String> {
expands: expands,
maxLength: maxLength,
onTap: onTap,
onTapOutside: onTapOutside,
onEditingComplete: onEditingComplete,
onSubmitted: onSubmitted,
inputFormatters: inputFormatters,
Expand Down
23 changes: 23 additions & 0 deletions test/form_builder_text_field_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:flutter_test/flutter_test.dart';

Expand Down Expand Up @@ -43,6 +44,11 @@ void main() {
initialValueOnForm: 'ok',
),
);

testWidgets(
'FormBuilderTextField triggers onTapOutside',
(tester) => _testFormBuilderTextFieldOnTapOutsideCallback(tester),
);
}

Future<void> _testFormBuilderTextFieldWithInitialValue(
Expand Down Expand Up @@ -75,3 +81,20 @@ Future<void> _testFormBuilderTextFieldWithInitialValue(

expect(changedCount, 1);
}

Future<void> _testFormBuilderTextFieldOnTapOutsideCallback(
WidgetTester tester) async {
const textFieldName = 'Hello 🪐';
bool triggered = false;

var testWidget = FormBuilderTextField(
name: textFieldName,
onTapOutside: (event) => triggered = true,
);
await tester.pumpWidget(buildTestableFieldWidget(
testWidget,
));
final textField = tester.firstWidget(find.byType(TextField)) as TextField;
textField.onTapOutside?.call(const PointerDownEvent());
expect(triggered, true);
}