Skip to content

Commit

Permalink
Merge pull request #1267 from AlvBarros/main
Browse files Browse the repository at this point in the history
feat: add onTapOutside to FormBuilderTextField
  • Loading branch information
deandreamatias authored Jun 22, 2023
2 parents edcebe5 + 4498e6d commit 079d25a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
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);
}

0 comments on commit 079d25a

Please sign in to comment.