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())),
),
];
}),
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'),
),
];
}),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With