Let CupertinoTextField's clear button also call onChanged (#29474)

This commit is contained in:
hyjfine 2019-03-26 15:26:13 +08:00 committed by xster
parent b5700506fe
commit 6e50ccc803
2 changed files with 38 additions and 3 deletions

View file

@ -660,9 +660,14 @@ class _CupertinoTextFieldState extends State<CupertinoTextField> 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(

View file

@ -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 {