Unverified Commit 05854afa authored by Qun Cheng's avatar Qun Cheng Committed by GitHub

`SearchAnchor` search view clear button only shows up when text input is not empty (#141755)

parent 684247a3
...@@ -816,7 +816,7 @@ class _ViewContentState extends State<_ViewContent> { ...@@ -816,7 +816,7 @@ class _ViewContentState extends State<_ViewContent> {
); );
final List<Widget> defaultTrailing = <Widget>[ final List<Widget> defaultTrailing = <Widget>[
IconButton( if (_controller.text.isNotEmpty) IconButton(
icon: const Icon(Icons.close), icon: const Icon(Icons.close),
onPressed: () { onPressed: () {
_controller.clear(); _controller.clear();
......
...@@ -1140,10 +1140,6 @@ void main() { ...@@ -1140,10 +1140,6 @@ void main() {
// Default search view has a leading back button on the start of the header. // Default search view has a leading back button on the start of the header.
expect(find.widgetWithIcon(IconButton, Icons.arrow_back), findsOneWidget); expect(find.widgetWithIcon(IconButton, Icons.arrow_back), findsOneWidget);
// Default search view has a trailing close button on the end of the header.
// It is used to clear the input in the text field.
expect(find.widgetWithIcon(IconButton, Icons.close), findsOneWidget);
final Text helperText = tester.widget(find.text('hint text')); final Text helperText = tester.widget(find.text('hint text'));
expect(helperText.style?.color, colorScheme.onSurfaceVariant); expect(helperText.style?.color, colorScheme.onSurfaceVariant);
expect(helperText.style?.fontSize, 16.0); expect(helperText.style?.fontSize, 16.0);
...@@ -1443,7 +1439,9 @@ void main() { ...@@ -1443,7 +1439,9 @@ void main() {
await tester.pumpWidget(buildAnchor()); await tester.pumpWidget(buildAnchor());
await tester.tap(find.widgetWithIcon(IconButton, Icons.search)); await tester.tap(find.widgetWithIcon(IconButton, Icons.search));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
// Default is a icon button with close icon. // Default is a icon button with close icon when input is not empty.
await tester.enterText(findTextField(), 'a');
await tester.pump();
expect(find.widgetWithIcon(IconButton, Icons.close), findsOneWidget); expect(find.widgetWithIcon(IconButton, Icons.close), findsOneWidget);
await tester.pumpWidget(Container()); await tester.pumpWidget(Container());
...@@ -3062,6 +3060,35 @@ void main() { ...@@ -3062,6 +3060,35 @@ void main() {
final RenderBox box = tester.renderObject(findHeader); final RenderBox box = tester.renderObject(findHeader);
expect(box.size.height, 32); expect(box.size.height, 32);
}); });
testWidgets('The default clear button only shows when text input is not empty '
'on the search view', (WidgetTester tester) async {
await tester.pumpWidget(MaterialApp(
home: Center(
child: Material(
child: SearchAnchor(
builder: (BuildContext context, SearchController controller) {
return const Icon(Icons.search);
},
suggestionsBuilder: (BuildContext context, SearchController controller) {
return <Widget>[];
},
),
),
),
));
await tester.pump();
await tester.tap(find.byIcon(Icons.search)); // Open search view.
await tester.pumpAndSettle();
expect(find.widgetWithIcon(IconButton, Icons.close), findsNothing);
await tester.enterText(findTextField(), 'a');
await tester.pump();
expect(find.widgetWithIcon(IconButton, Icons.close), findsOneWidget);
await tester.enterText(findTextField(), '');
await tester.pump();
expect(find.widgetWithIcon(IconButton, Icons.close), findsNothing);
});
} }
Future<void> checkSearchBarDefaults(WidgetTester tester, ColorScheme colorScheme, Material material) async { Future<void> checkSearchBarDefaults(WidgetTester tester, ColorScheme colorScheme, Material material) async {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment