mirror of
https://github.com/flutter/flutter
synced 2024-10-13 19:52:53 +00:00
Let CupertinoTextField's clear button also call onChanged (#29474)
This commit is contained in:
parent
b5700506fe
commit
6e50ccc803
|
@ -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(
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue