Repository: flutter
Issue: SearchAnchor doesn't update trailing icons
Labels: framework,f: material design,has reproducible steps,P2,team-design,triaged-design,found in release: 3.19,found in release: 3.21

### Steps to reproduce

I am in the process of integrating a search feature into my app and have stumbled upon a challenging issue. My implementation utilizes the SearchAnchor and SearchBar widgets, but I am facing difficulties in concealing the close icon within the search route view when there are no search queries. The goal is to ensure the close icon is only displayed when users have entered a query.

Why my initial approach failed while the latter one works?



### Expected results

I devised a workaround by developing a separate stateful widget that specifically handles the rebuilding of the trailing icons:

https://dartpad.dev/?id=0832ec3eee0b686b3f844092b0222217

### Actual results

To provide a clear picture of the issue, I have shared a DartPad snippet illustrating the problem:

https://dartpad.dev/?id=f65ca59d3cd5afee774e4ef7fdc0ffc9

### Code sample

<details open><summary>Code sample</summary>

```dart
import 'package:flutter/material.dart';

void main() {
  runApp(
    const MaterialApp(
      home: Scaffold(
        body: SafeArea(
          child: Padding(
            padding: EdgeInsets.all(16),
            child: CustomSearchBar(),
          ),
        ),
      ),
    ),
  );
}

class CustomSearchBar extends StatefulWidget {
  const CustomSearchBar({super.key});

  @override
  State<CustomSearchBar> createState() => _CustomSearchBarState();
}

class _CustomSearchBarState extends State<CustomSearchBar> {
  final SearchController _controller = SearchController();

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return ValueListenableBuilder<TextEditingValue>(
      valueListenable: _controller,
      builder: (context, value, _) {
        return SearchAnchor(
          searchController: _controller,
          viewTrailing: _controller.value.text.isNotEmpty
              ? [
                  IconButton(
                    icon: const Icon(Icons.clear),
                    onPressed: () {
                      _controller.clear();
                    },
                  )
                ]
              : [],
          builder: (context, controller) {
            return SearchBar(
              controller: controller,
              hintText: 'Search',
              onTap: controller.openView,
              leading: const Icon(Icons.search),
            );
          },
          suggestionsBuilder: (context, controller) {
            return [];
          },
        );
      },
    );
  }
}

```

</details>


### Screenshots or Video

<details open>
<summary>Screenshots / Video demonstration</summary>

[Upload media here]

</details>


### Logs

<details open><summary>Logs</summary>

```console
[Paste your logs here]
```

</details>


### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
[√] Flutter (Channel stable, 3.19.0, on Microsoft Windows [Version 10.0.22631.3235], locale en-US)
    • Flutter version 3.19.0 on channel stable at C:\tools\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision bae5e49bc2 (4 weeks ago), 2024-02-13 17:46:18 -0800
    • Engine revision 04817c99c9
    • Dart version 3.3.0
    • DevTools version 2.31.1

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at C:\Users\aarro\AppData\Local\Android\sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-9586694)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[√] Android Studio (version 2022.2)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-9586694)

[√] VS Code (version 1.87.1)
    • VS Code at C:\Users\aarro\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.84.0

[√] Connected device (2 available)
    • Chrome (web) • chrome • web-javascript • Google Chrome 122.0.6261.112
    • Edge (web)   • edge   • web-javascript • Microsoft Edge 122.0.2365.59

[√] Network resources
    • All expected network resources are available.

• No issues found!
```

</details>