I have a StatelessWidget class :
class PopupMenu extends StatelessWidget {
final VoidCallback editTaskCallback;
const PopupMenu({
required this.editTaskCallback,
super.key,
});
@override
Widget build(BuildContext context) {
return PopupMenuButton(
itemBuilder:(context) => [
PopupMenuItem(
onTap: null,
child: TextButton.icon(
onPressed: editTaskCallback,
icon: const Icon(Icons.edit),
label: const Text('Edit'),
),
),
],
);
}
}
Just want to know why VoidCallback works fine with OnPressed
when i press the TextButton but not with onTap
when i tap the PopupMenuItem.
Here how i call the PopupMenu class :
PopupMenu(
editTaskCallback: () {_editTask(context)},
),
Here the _editTask(context)
method :
void _editTask(BuildContext context) {
showModalBottomSheet(
isScrollControlled: true,
context: context,
builder: (context) => SingleChildScrollView(
child: Container(
padding:
EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
child: EditTaskBottom(
oldTask: task,
),
),
),
);
}
Edit: The weird thing is when i added the print
statement to the callback,the OnTap
is printing the result but no showModalBottomSheet
appeared.
2
Answers
You are creating 2 actions and every single action have their "Size" PopupMenuItem are inside all the PopupMenuButton width with a random padding/margin in top and bottom, the TextButton default size are 20.
so where is the "problem" 🙂 ?
probably you are clicking/tapping only in TextButton, if you click/tap in the corner of PopupMenuItem you have onTap working. Try to click/tap in centerRight corner of PopupMenuItem.
Size of TextButton
Size of PopupMenuItem
If press and hold you can see the area, but TextButton are corvering the size of 20 in PopupMenuItem.
let me know if this help
It is not issue about
onTap
andonPressed
. If you want toshowDialog
orbottomsheet
. It shows dialog for few milliseconds and popup immediately.Try this code: