Repository: flutter
Issue: `showMenu` menu is unable to be traversed if opened via space or enter keys
Labels: framework,f: material design,customer: google,a: desktop,P2,team-design,triaged-design

If you have a `showMenu` menu, and try to open it via the spacebar and then traverse it using the tab traversal, it will fail to traverse the items in the menu, and traverse the items behind the menu instead.

This is not the case if you open the menu via a mouse click: if you click on the menu and use tab traversal, it does the correct thing, and starts traversing at the first item.

Here's a reproduction case that shows the problem:

```dart
import 'package:flutter/material.dart';

const Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Row(
          children: <Widget>[
            const MyWidget(),
            ElevatedButton(onPressed: () {}, child: const Text('Other Button')),
            ElevatedButton(onPressed: () {}, child: const Text('Other Button')),
            ElevatedButton(onPressed: () {}, child: const Text('Other Button')),
            ElevatedButton(onPressed: () {}, child: const Text('Other Button')),
            ElevatedButton(onPressed: () {}, child: const Text('Other Button')),
            ElevatedButton(onPressed: () {}, child: const Text('Other Button')),
            ElevatedButton(onPressed: () {}, child: const Text('Other Button')),
          ],
        ),
      ),
    );
  }
}

class MyWidget extends StatelessWidget {
  const MyWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: _DropdownButton<int>(
        label: 'Open Me',
        items: List<PopupMenuItem<int>>.generate(10, (int index) => Item(value: index)),
        onSelected: (int value) {},
      ),
    );
  }
}

class Item extends PopupMenuItem<int> {
  Item({super.key, required int value})
      : super(
          value: value,
          onTap: () {
            debugPrint('Selected $value');
          },
          child: Text('Item $value'),
        );
}

class _DropdownButton<T> extends StatefulWidget {
  const _DropdownButton({
    super.key,
    required this.label,
    this.semanticsLabel,
    required this.items,
    required this.onSelected,
  });

  final String label;
  final String? semanticsLabel;
  final List<PopupMenuEntry<T>> items;
  final void Function(T) onSelected;

  @override
  State<_DropdownButton<T>> createState() => _DropdownButtonState<T>();
}

class _DropdownButtonState<T> extends State<_DropdownButton<T>> {
  bool menuExpanded = false;

  @override
  Widget build(BuildContext context) {
    const EdgeInsetsDirectional padding = EdgeInsetsDirectional.only(start: 16, end: 8);
    const Size minimumSize = Size(0, 32);
    final Semantics buttonContents = Semantics(
      excludeSemantics: true,
      label: widget.semanticsLabel ?? widget.label,
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(widget.label),
          const SizedBox(width: 8),
          Icon(
            menuExpanded ? Icons.arrow_drop_up_outlined : Icons.arrow_drop_down_outlined,
            size: 18,
          ),
        ],
      ),
    );

    if (menuExpanded) {
      return FilledButton(
        style: const ButtonStyle(
          minimumSize: MaterialStatePropertyAll<Size>(minimumSize),
          padding: MaterialStatePropertyAll<EdgeInsetsGeometry>(padding),
        ),
        // In general it should not be possible to tap the button when the menu
        // is expanded, because all taps in that state simply dismiss the menu.
        // However, setting a non-null onPressed handler here to avoid
        // defaulting to the disabled button UI.
        onPressed: () => setState(() {
          menuExpanded = false;
        }),
        child: buttonContents,
      );
    }
    return Builder(
      builder: (BuildContext context) {
        return ElevatedButton(
          style: const ButtonStyle(
            minimumSize: MaterialStatePropertyAll<Size>(minimumSize),
            padding: MaterialStatePropertyAll<EdgeInsetsGeometry>(padding),
          ),
          child: buttonContents,
          onPressed: () async {
            // Display a pop-up menu positioned at the bottom edge of the button.
            final RenderBox button = context.findRenderObject()! as RenderBox;
            final RenderBox overlay = Navigator.of(context).overlay!.context.findRenderObject()! as RenderBox;
            final RelativeRect position = RelativeRect.fromRect(
              Rect.fromPoints(
                button.localToGlobal(
                  button.size.bottomLeft(Offset.zero),
                  ancestor: overlay,
                ),
                button.localToGlobal(
                  button.size.bottomRight(Offset.zero),
                  ancestor: overlay,
                ),
              ),
              Offset.zero & overlay.size,
            );
            setState(() {
              menuExpanded = true;
            });

            final T? result = await showMenu<T>(
              context: context,
              position: position,
              items: widget.items,
            );
            if (result != null) {
              widget.onSelected(result);
            }

            setState(() {
              menuExpanded = false;
            });
          },
        );
      },
    );
  }
}
```

Opening the menu via the keyboard should autofocus the FocusScope for the menu.

Internal reference: [b/242104188](http://b/242104188)