From de4e3d67a030c5bf2597f7197690118077abfbad Mon Sep 17 00:00:00 2001 From: Mairramer <50643541+Mairramer@users.noreply.github.com> Date: Wed, 25 Oct 2023 21:26:20 -0300 Subject: [PATCH] fix:: trigger onTapOutside only if has focus (#136291) Adds new feat and fix to https://github.com/flutter/flutter/issues/134341 --- .../lib/src/widgets/editable_text.dart | 2 +- .../test/material/text_form_field_test.dart | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index 13ede28663..50851e85e6 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -4770,7 +4770,7 @@ class EditableTextState extends State with AutomaticKeepAliveClien compositeCallback: _compositeCallback, enabled: _hasInputConnection, child: TextFieldTapRegion( - onTapOutside: widget.onTapOutside ?? _defaultOnTapOutside, + onTapOutside: _hasFocus ? widget.onTapOutside ?? _defaultOnTapOutside : null, debugLabel: kReleaseMode ? null : 'EditableText', child: MouseRegion( cursor: widget.mouseCursor ?? SystemMouseCursors.text, diff --git a/packages/flutter/test/material/text_form_field_test.dart b/packages/flutter/test/material/text_form_field_test.dart index 7d8de3ce11..40319670b1 100644 --- a/packages/flutter/test/material/text_form_field_test.dart +++ b/packages/flutter/test/material/text_form_field_test.dart @@ -773,6 +773,7 @@ void main() { children: [ const Text('Outside'), TextFormField( + autofocus: true, onTapOutside: (PointerEvent event) { tapOutsideCount += 1; }, @@ -793,6 +794,37 @@ void main() { expect(tapOutsideCount, 3); }); + // Regression test for https://github.com/flutter/flutter/issues/134341. + testWidgetsWithLeakTracking('onTapOutside is not called upon tap outside when field is not focused', (WidgetTester tester) async { + int tapOutsideCount = 0; + await tester.pumpWidget( + MaterialApp( + home: Material( + child: Center( + child: Column( + children: [ + const Text('Outside'), + TextFormField( + onTapOutside: (PointerEvent event) { + tapOutsideCount += 1; + }, + ), + ], + ), + ), + ), + ), + ); + await tester.pump(); + + expect(tapOutsideCount, 0); + await tester.tap(find.byType(TextFormField)); + await tester.tap(find.text('Outside')); + await tester.tap(find.text('Outside')); + await tester.tap(find.text('Outside')); + expect(tapOutsideCount, 0); + }); + // Regression test for https://github.com/flutter/flutter/issues/54472. testWidgetsWithLeakTracking('reset resets the text fields value to the initialValue', (WidgetTester tester) async { await tester.pumpWidget(