Repository: flutter
Issue: Flickering context menu when re-rendering
Labels: a: text input,framework,has reproducible steps,P2,workaround available,team-text-input,triaged-text-input,found in release: 3.22,found in release: 3.23

### Steps to reproduce

Flutter 3.22.2
Platoform: Windows

- Add SelectableText with custom contextMenuBuilder
- Select text and right click to oepn context menu
- The context menu flicks when re-rendering widget

### Expected results

Does not flicker when re-rendered

This problem did not occur in the flutter version of 3.19.3

### Actual results

Flickering on re-rendering

### Code sample

<details open><summary>debug_page.dart</summary>

```dart
import 'dart:async';
import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

import '../debug_page_state.dart';

final debugPageStateProvider =
    StateNotifierProvider.autoDispose<DebugPageStateNotifier, DebugPageState>(
        (ref) => DebugPageStateNotifier());

class DebugPageStateNotifier extends StateNotifier<DebugPageState> {
  DebugPageStateNotifier() : super(const DebugPageState());
  Timer? timer;

  startTimer() {

    // update state every 1 sec.
    timer ??= Timer.periodic(const Duration(seconds: 1), (timer) {
      state = state.copyWith(count: timer.tick);
    });
  }
}

class DebugPage extends HookConsumerWidget {
  const DebugPage({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final notifier = ref.watch(debugPageStateProvider.notifier);
    final state = ref.watch(debugPageStateProvider);

    notifier.startTimer();

    return Column(
      children: [
        Text(state.count.toString()),
        SelectableText('selectable text',
            contextMenuBuilder: (context, editableTextState) {
          log('contextMenuBuilder is called');
          return AdaptiveTextSelectionToolbar.buttonItems(
            buttonItems: [
              ContextMenuButtonItem(onPressed: () {}, label: 'menu'),
              ...editableTextState.contextMenuButtonItems,
            ],
            anchors: editableTextState.contextMenuAnchors,
          );
        }),
      ],
    );
  }
}

```

</details>

<details open><summary>debug_page_state.dart</summary>

```dart
import 'package:freezed_annotation/freezed_annotation.dart';
part 'debug_page_state.freezed.dart';

@freezed
class DebugPageState with _$DebugPageState {
  const DebugPageState._();
  const factory DebugPageState({
    @Default(0) int count,
  }) = _DebugPageState;
}

```

</details>


### Screenshots or Video

<details open>
<summary>Screenshots / Video demonstration</summary>

https://github.com/flutter/flutter/assets/5352478/c2ccb594-c660-4b04-a6a2-543858941261

</details>


### Logs

<details open><summary>Logs</summary>

```console
[log] contextMenuBuilder is called
[log] contextMenuBuilder is called
[log] contextMenuBuilder is called
...
```

Logs of `contextMenuBuilder is called` continue to be output while the menu is open.

</details>


### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
[√] Flutter (Channel stable, 3.22.2, on Microsoft Windows [Version 10.0.22631.3593], locale ja-JP)
    • Flutter version 3.22.2 on channel stable at C:\Users\ryosuke_hayashi\fvm\versions\3.22.2
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 761747bfc5 (2 weeks ago), 2024-06-05 22:15:13 +0200
    • Engine revision edd8546116
    • Dart version 3.4.3
    • DevTools version 2.34.3

[√] 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\ryosuke_hayashi\AppData\Local\Android\sdk
    X cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    X Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/windows#android-setup for more details.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop Windows apps (Visual Studio Professional 2022 17.9.6)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Professional
    • Visual Studio Professional 2022 version 17.9.34728.123
    • Windows 10 SDK version 10.0.22621.0

[√] Android Studio (version 2024.1)
    • Android Studio at C:\Users\ryosuke_hayashi\AppData\Local\Programs\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.10+0--11609105)

[√] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.22631.3593]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 126.0.6478.114
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 126.0.2592.61

[√] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

```

</details>