10
    showMenu(
      context: context,
      position: RelativeRect.fromLTRB(5000, appBar.preferredSize.height + statusBarHeight!, 0.0, 0.0),
      items: [
        PopupMenuItem(
          child: Text('Cancel'),
          value: 0,
          onTap: () {
            cancelDialog();
          },
        ),
      ],
    );
  cancelDialog() {
    return () {
      showDialog(
        context: context,
        builder: (BuildContext context) => OrderCancel(order!.orderId!),
      );
    };
  }

cancelDialog not open using previously code. But its opens properly from another button click

HasanToufiqAhamed
  • 751
  • 1
  • 5
  • 17

7 Answers7

5

Try this

onTap: () {
           Future.delayed(
                  Duration.zero,
                  () => cancelDialog();
           )
          },
sippinman
  • 51
  • 1
  • 9
3

I faced the same issue today and after several minutes, I realised that you can't use onTap for that. You have to use onSelected of PopupMenuButton.

See: enter link description here

1

you can use PopupMenuButton

      PopupMenuButton<String>(
        onSelected: onItemSelected,
        shape: RoundedRectangleBorder(
          borderRadius: 8.circularRadius,
        ),
        itemBuilder: (context) => const [
          PopupMenuItem(
            value: 'add',
            child: Text(
              'Add',
            ),
          ),
        ],
        child: const Icon(
          Icons.menu_rounded,
          color: Colors.white,
          size: 16,
        ),
      ),
Ahamed
  • 33
  • 5
1

You can follow this solution : enter link description here

in short, you should warp your function with that :

WidgetsBinding?.instance?.addPostFrameCallback((_) {/* your function */}
0

You can check the following steps:

  1. To investigate we would need a minimal runnable reproduction as a single file so that we can just copy your code into lib/main.dart of a new project and run to reproduce

  2. Without additional information, we are unfortunately not sure how to resolve this issue. We are therefore reluctantly going to close this bug for now. Please don't hesitate to comment on the bug if you have any more information for us; we will reopen it right away! Thanks for your contribution.

  3. Your itemBuilder function does not return anything. It's missing the return keyword.

0

I also encountered the same issue today. On debugging, I got to know that after executing the onTap method, Navigator.of(context).pop(); gets called once. So that is why your cancelDialog is not visible. Actually it opens when the showDialog method is called but it also gets popped out very quickly. Coming to the solution, you can just call the cancelDialog method twice. So even if the latter one gets pops out, the former remains visible on the screen.

onTap: () {
     cancelDialog();
     cancelDialog();
},

Replacing the old onTap method with the above one should do.

0

According to this answer, the onTap function for the PopUpMenuItem must be called as shown below and not to directly call the showDialog. Instead, call the widgetsBidingInstance first so as to display the dialog after the ontap function that closes the PopUpMenuItem when tapped has been called.

   onTap: (){

 WidgetsBiding.instance.addPostFrameCallback((){
    showDialog(
    context: context,
    builder:(BuildContext context)=> AlertDialog(title: Text("This is Title"),),
    
    ),
        });
}
desertnaut
  • 57,590
  • 26
  • 140
  • 166