Home > OS >  Flutter - PopupMenuItem onTap is not calling callback function
Flutter - PopupMenuItem onTap is not calling callback function

Time:04-28

in my flutter project here the same callback is working with when I press onPressed in the TextButton.icon widget itself but onTap is not working. I do know this when I press inside of PopupMenuItem but not exactly on TextButton in this case it's not working, but it works when I press on TextButton. I did onPressed:null but onTap still not working. My question is how can I call this callback by pressing anywhere inside PopupMenuItem?

PopupMenuItem(
  child: GestureDetector(
    behavior: HitTestBehavior.translucent,
    child: TextButton.icon(
      onPressed: editTaskCallback,
      icon: const Icon(Icons.edit),
      label: const Text('Edit'),
    ),
  ),
  onTap: editTaskCallback,
),

CodePudding user response:

You can combine the methods, use Navigator.of(context).pop() to close the PopupMenuItem.

 editTaskCallback(BuildContext context, bool fromOnTap) {
    debugPrint("tapped");
    if (!fromOnTap) Navigator.of(context).pop(); // will close the menu item
  }

///...

PopupMenuItem(
    child: TextButton.icon(
      onPressed: () {
        editTaskCallback(context, false);
      },
      icon: const Icon(Icons.edit),
      label: const Text('Edit'),
    ),
    onTap: () {
      editTaskCallback(context, true);
    }),

CodePudding user response:

I was able to find this solution to enable more pressable place in 'PopupMenuItem'

PopupMenuItem(
  child: IntrinsicWidth(
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        TextButton.icon(
          onPressed: () {
            Navigator.of(context).pop();
            editTaskCallback();
          },
          icon: const Icon(Icons.edit),
          label: const Text('Edit'),
        ),
      ],
    ),
  ),
  onTap: null,
),
  • Related