I am trying to show a pop-up menu appear when an IconButton() is clicked I added a PopupMenuButton() to the on pressed method of the IconButton as you can see below:
but it does not show any pop-up menu when clicked.
If there’s anything I’m doing wrong I hope some one can point it out for me.
Here is the code:
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
String _selectedMenu = '';
enum Menu { itemOne, itemTwo, itemThree, itemFour }
class ListTileWithIcon extends StatefulWidget {
const ListTileWithIcon({super.key});
@override
State<ListTileWithIcon> createState() => _ListTileWithIconState();
}
class _ListTileWithIconState extends State<ListTileWithIcon> {
@override
Widget build(BuildContext context) {
return ListTile(
leading: const Icon(Icons.person),
title: const Text("The ListTile"),
trailing: IconButton(
// key: _icnBtnKey,
splashRadius: 20,
icon: const Icon(
Icons.more_vert_rounded,
size: 20.0,
color: Color.fromARGB(255, 135, 0, 212),
),
onPressed: () {
// showPopUpMenuAtTap();
if (kDebugMode) {
print("Inside icon button's clickt");
}
PopupMenuButton<Menu>(
// Callback that sets the selected popup menu item.
onSelected: (Menu item) {
setState(() {
_selectedMenu = item.name;
});
},
itemBuilder: (BuildContext context) => <PopupMenuEntry<Menu>>[
const PopupMenuItem<Menu>(
value: Menu.itemOne,
child: Text('Item 1'),
),
const PopupMenuItem<Menu>(
value: Menu.itemTwo,
child: Text('Item 2'),
),
const PopupMenuItem<Menu>(
value: Menu.itemThree,
child: Text('Item 3'),
),
const PopupMenuItem<Menu>(
value: Menu.itemFour,
child: Text('Item 4'),
),
],
);
},
),
onTap: () {},
);
}
}
Thanks in advance.
2
Answers
you should use the PopUpMenuButton atribute ‘icon’ instead of using the icon button.
You might be looking for
showMenu
You can use a GlobalKey to open popUp menu from others button pressed
And call
Test snippet