Repository: flutter
Issue: MenuAnchor is overlapping with button when opening upwards
Labels: d: examples,platform-web,a: desktop,has reproducible steps,P2,team-design,triaged-design,found in release: 3.27,found in release: 3.28

### Steps to reproduce

Simply run the sample code within documentation page code editor of sample #2.
On rendered UI, click button on top and click button on bottom and compare the layout.

---

I was checking documentation of PopupMenuButton https://api.flutter.dev/flutter/material/PopupMenuButton-class.html , since I wanted to build a popup menu that opens upwards when I click on bottom navbar button. I wanted menu to not overlap with the navbar.
Sample #2 (flutter create --sample=material.PopupMenuButton.2 mysample) caught my eye. I used the code provided in sample to test whether MenuAnchor would suit my needs. I modified it slightly, changing alignment of button and menu opening direction. I ran my code in documentation page editor
![зображення](https://github.com/user-attachments/assets/19bcad9c-57b2-423e-aa58-b8f52093c328)



### Expected results

- Green button opens context menu on click. This context menu opens downwards. Top border of opened menu does not overlap with bottom pixel of button.
- Red button opens context menu on click. This context menu opens upwnwards. Bottom border of opened menu does not overlap with top pixel of button.

### Actual results

- Green button opens context menu on click.✔️ This context menu opens downwards. ✔️Top border of opened menu does not overlap with bottom pixel of button. ✔️
- Red button opens context menu on click.✔️ This context menu opens upwnwards.✔️ Bottom border of opened menu **overlaps** with top pixels of button.❌

---

Instead of reporting bug, I could just add some padding to menu. And it might work if button has a constant height calculated in pixels. But if the button's height is flexible in pixels, and measured in % of screen height, then its not possible to handle this gracefully.

### Code sample

<details open><summary>Code sample</summary>

```
import 'package:flutter/material.dart';

/// Flutter code sample for [MenuAnchor].

void main() => runApp(const MenuAnchorApp());

// This is the type used by the menu below.
enum SampleItem { itemOne, itemTwo, itemThree }

class MenuAnchorApp extends StatelessWidget {
  const MenuAnchorApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: MenuAnchorExample(),
    );
  }
}

class MenuAnchorExample extends StatefulWidget {
  const MenuAnchorExample({super.key});

  @override
  State<MenuAnchorExample> createState() => _MenuAnchorExampleState();
}

class _MenuAnchorExampleState extends State<MenuAnchorExample> {
  SampleItem? selectedMenu;

  Widget _buildMenuButton(Color color) {
    return MenuAnchor(
      builder:
          (BuildContext context, MenuController controller, Widget? child) {
        return IconButton(
          style: IconButton.styleFrom(backgroundColor: color),
          onPressed: () {
            if (controller.isOpen) {
              controller.close();
            } else {
              controller.open();
            }
          },
          icon: const Icon(Icons.more_horiz, size: 40),
          tooltip: 'Show menu',
        );
      },
      menuChildren: List<MenuItemButton>.generate(
        3,
        (int index) => MenuItemButton(
          onPressed: () =>
              setState(() => selectedMenu = SampleItem.values[index]),
          child: Text('Item ${index + 1}'),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('MenuAnchorButton'),
        backgroundColor: Theme.of(context).primaryColorLight,
      ),
      body:
          Column(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
        _buildMenuButton(Colors.green),
        _buildMenuButton(Colors.red),
      ]),
    );
  }
}

```

</details>


### Screenshots or Video

<details open>
<summary>Screenshots / Video demonstration</summary>

![зображення](https://github.com/user-attachments/assets/e19be210-474d-49e8-b22d-82945017bd9f)
![зображення](https://github.com/user-attachments/assets/2c8ae3ed-e2cf-4900-8d20-3fb56c521b97)

</details>


### Logs

<details open><summary>Logs</summary>

```console
irrelevant - I ran code in documentation page editor
```

</details>


### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
irrelevant - I ran code in documentation page editor
```

</details>