From 6e50ccc80316286e45413e4f2c4f32acae2bbac2 Mon Sep 17 00:00:00 2001 From: hyjfine Date: Tue, 26 Mar 2019 15:26:13 +0800 Subject: [PATCH] Let CupertinoTextField's clear button also call onChanged (#29474) --- .../flutter/lib/src/cupertino/text_field.dart | 11 +++++-- .../test/cupertino/text_field_test.dart | 30 +++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/text_field.dart b/packages/flutter/lib/src/cupertino/text_field.dart index 11217e0313..2badd43762 100644 --- a/packages/flutter/lib/src/cupertino/text_field.dart +++ b/packages/flutter/lib/src/cupertino/text_field.dart @@ -660,9 +660,14 @@ class _CupertinoTextFieldState extends State with AutomaticK } else if (_showClearButton(text)) { rowChildren.add( GestureDetector( - onTap: widget.enabled ?? true - ? () => _effectiveController.clear() - : null, + onTap: widget.enabled ?? true ? () { + // Special handle onChanged for ClearButton + // Also call onChanged when the clear button is tapped. + final bool textChanged = _effectiveController.text.isNotEmpty; + _effectiveController.clear(); + if (widget.onChanged != null && textChanged) + widget.onChanged(_effectiveController.text); + } : null, child: const Padding( padding: EdgeInsets.symmetric(horizontal: 6.0), child: Icon( diff --git a/packages/flutter/test/cupertino/text_field_test.dart b/packages/flutter/test/cupertino/text_field_test.dart index eddaa6bab3..fc20441a30 100644 --- a/packages/flutter/test/cupertino/text_field_test.dart +++ b/packages/flutter/test/cupertino/text_field_test.dart @@ -749,6 +749,36 @@ void main() { }, ); + testWidgets( + 'tapping clear button also calls onChanged when text not empty', + (WidgetTester tester) async { + String value = 'text entry'; + final TextEditingController controller = TextEditingController(); + await tester.pumpWidget( + CupertinoApp( + home: Center( + child: CupertinoTextField( + controller: controller, + placeholder: 'placeholder', + onChanged: (String newValue) => value = newValue, + clearButtonMode: OverlayVisibilityMode.always, + ), + ), + ), + ); + + controller.text = value; + await tester.pump(); + + await tester.tap(find.byIcon(CupertinoIcons.clear_thick_circled)); + await tester.pump(); + + expect(controller.text, isEmpty); + expect(find.text('text entry'), findsNothing); + expect(value, isEmpty); + }, + ); + testWidgets( 'clear button yields precedence to suffix', (WidgetTester tester) async {