Skip to content

Commit 05854af

Browse files
authored
SearchAnchor search view clear button only shows up when text input is not empty (#141755)
1 parent 684247a commit 05854af

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

packages/flutter/lib/src/material/search_anchor.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ class _ViewContentState extends State<_ViewContent> {
816816
);
817817

818818
final List<Widget> defaultTrailing = <Widget>[
819-
IconButton(
819+
if (_controller.text.isNotEmpty) IconButton(
820820
icon: const Icon(Icons.close),
821821
onPressed: () {
822822
_controller.clear();

packages/flutter/test/material/search_anchor_test.dart

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,10 +1140,6 @@ void main() {
11401140
// Default search view has a leading back button on the start of the header.
11411141
expect(find.widgetWithIcon(IconButton, Icons.arrow_back), findsOneWidget);
11421142

1143-
// Default search view has a trailing close button on the end of the header.
1144-
// It is used to clear the input in the text field.
1145-
expect(find.widgetWithIcon(IconButton, Icons.close), findsOneWidget);
1146-
11471143
final Text helperText = tester.widget(find.text('hint text'));
11481144
expect(helperText.style?.color, colorScheme.onSurfaceVariant);
11491145
expect(helperText.style?.fontSize, 16.0);
@@ -1443,7 +1439,9 @@ void main() {
14431439
await tester.pumpWidget(buildAnchor());
14441440
await tester.tap(find.widgetWithIcon(IconButton, Icons.search));
14451441
await tester.pumpAndSettle();
1446-
// Default is a icon button with close icon.
1442+
// Default is a icon button with close icon when input is not empty.
1443+
await tester.enterText(findTextField(), 'a');
1444+
await tester.pump();
14471445
expect(find.widgetWithIcon(IconButton, Icons.close), findsOneWidget);
14481446

14491447
await tester.pumpWidget(Container());
@@ -3062,6 +3060,35 @@ void main() {
30623060
final RenderBox box = tester.renderObject(findHeader);
30633061
expect(box.size.height, 32);
30643062
});
3063+
3064+
testWidgets('The default clear button only shows when text input is not empty '
3065+
'on the search view', (WidgetTester tester) async {
3066+
await tester.pumpWidget(MaterialApp(
3067+
home: Center(
3068+
child: Material(
3069+
child: SearchAnchor(
3070+
builder: (BuildContext context, SearchController controller) {
3071+
return const Icon(Icons.search);
3072+
},
3073+
suggestionsBuilder: (BuildContext context, SearchController controller) {
3074+
return <Widget>[];
3075+
},
3076+
),
3077+
),
3078+
),
3079+
));
3080+
await tester.pump();
3081+
await tester.tap(find.byIcon(Icons.search)); // Open search view.
3082+
await tester.pumpAndSettle();
3083+
3084+
expect(find.widgetWithIcon(IconButton, Icons.close), findsNothing);
3085+
await tester.enterText(findTextField(), 'a');
3086+
await tester.pump();
3087+
expect(find.widgetWithIcon(IconButton, Icons.close), findsOneWidget);
3088+
await tester.enterText(findTextField(), '');
3089+
await tester.pump();
3090+
expect(find.widgetWithIcon(IconButton, Icons.close), findsNothing);
3091+
});
30653092
}
30663093

30673094
Future<void> checkSearchBarDefaults(WidgetTester tester, ColorScheme colorScheme, Material material) async {

0 commit comments

Comments
 (0)