Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigator inside PopupMenuItem does not work

Tags:

flutter

dart

When trying to call Navigator inside the onTap property of a PopupMenuItem it doesn't work:

PopupMenuButton(itemBuilder: (BuildContext context) {
  return [
    PopupMenuItem(
      child: Text('Edit'),
      onTap: () => Navigator.of(context)
          .push(MaterialPageRoute(builder: (context) => EditorPage())),
    ),
  ];
}),
like image 575
Nuqo Avatar asked Dec 06 '25 06:12

Nuqo


1 Answers

The popup menu is popping its own route before calling the callback, a way to fix this is to use the onSelected property of the PopupMenuButton itself:

//---add onSelected to your PopupMenuButton
PopupMenuButton(onSelected: (result) {
  if (result == 0) {
    Navigator.of(context)
        .push(MaterialPageRoute(builder: (context) => EditorPage()));
  }
}, itemBuilder: (BuildContext context) {
  return [
    PopupMenuItem(
      value: 0, //---add this line
      child: Text('Edit'),
    ),
  ];
}),
like image 165
Nuqo Avatar answered Dec 08 '25 00:12

Nuqo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!