1

I want to implement a right click menu in flutter and the code look like this:

Listener(
  onPointerDown: (event) {
    if (event.buttons == 2) {
      double dy = event.position.dy.ceilToDouble();
      double dx = event.position.dx.ceilToDouble();
      showMenu(
        context: context,
        position: RelativeRect.fromLTRB(dx, dy, dx, 0),
        items: [
          PopupMenuItem(child: const Text("Parent Menu 1"), onTap: () {}),
        ],
      );
    }
  }
  child: ExpansionTile(
    initiallyExpanded: true,
    iconColor: Colors.white,
    leading: const Icon(Icons.book),
    title: const Text(
      "Note",
      style: TextStyle(fontSize: 16.0, color: Colors.white),
    ),
    children: <Widget>[
      Listener(
        onPointerDown: (event) {
          if (event.buttons == 2) {
            double dy = event.position.dy.ceilToDouble();
            double dx = event.position.dx.ceilToDouble();
            showMenu(
              context: context,
              position: RelativeRect.fromLTRB(dx, dy, dx, 0),
              items: [
                PopupMenuItem(child: const Text("Child Menu 1"), onTap: () {}),
                PopupMenuItem(child: const Text("Child Menu 2"), onTap: () {}),
              ],
            );
          }
        }        
        child: ListTile(
          title: Text(
            formatTitle(),
            style: TextStyle(color: Colors.white, fontSize: fontSize ?? 14.0),
          ),
          dense: true,
          onTap: () {},
        ),
      )
    ],
  ),
)

But apparently, the parent's onPointerDown is also triggered when child's onPointerDown is triggered.

This is what I wanted:

enter image description here

But this is what I got:

enter image description here

I'm running this on flutter linux desktop

BookShell527
  • 176
  • 2
  • 8

0 Answers0