2

Here is my PopupMenuButton

                        PopupMenuButton(
                            icon:
                                Icon(Icons.more_vert, color: Colors.white),
                            itemBuilder: (context) => [
                                  PopupMenuItem(
                                    onTap: () {
                                      showMyDialog();
                                    },
                                    child: Text(
                                      "Edit Profile Picture",
                                      style: TextStyle(
                                          color: AppColors.pinkColor),
                                    ),
                                    value: 1,
                                  ),
                                  PopupMenuItem(
                                   
                                    child: Text(
                                      "Edit  Cover Picture",
                                      style: TextStyle(
                                          color: AppColors.pinkColor),
                                    ),
                                    value: 2,
                                  ),
                                  PopupMenuItem(
                                  
                                    child: Text(
                                      "Setting",
                                      style: TextStyle(
                                          color: AppColors.pinkColor),
                                    ),
                                    value: 3,
                                  )
                                ])

And this is my Dilaogue function

Future<void> _showMyDialog() async {
    return showDialog<void>(
      context: context,
      barrierDismissible: false, // user must tap button!
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text('AlertDialog Title'),
          content: SingleChildScrollView(
            child: ListBody(
              children: const <Widget>[
                Text('This is a demo alert dialog.'),
                Text('Would you like to approve of this message?'),
              ],
            ),
          ),
          actions: <Widget>[
            TextButton(
              child: const Text('Approve'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }
1
  • 1
    Your method declaration says _showMyDialog() and your call says showMyDialog() without _. Try to call it as you declared the function. Dec 13, 2021 at 13:57

2 Answers 2

1

onTap of PopupMenuItem call Navigator.pop after tapping on any item on PopupMenuButton to close it.

 void handleTap() {
    widget.onTap?.call();

    Navigator.pop<T>(context, widget.value);
  }

When you tap the item it shows _showMyDialog() but close it immediately because of Navigator.pop called and that's why you can find PopupMenuItem is open after tap.

Simply Navigator.pop is closing the showDialog instead of PopupMenuButton's items. We can provide some delay to close the PopupMenuItem then can call _showMyDialog();

PopupMenuItem(
  onTap: () {
    Future.delayed(Duration.zero).then((value) {
      _showMyDialog();
    });
  },
2
  • it worked thanks a lot Yeasin Sheikh Dec 14, 2021 at 6:05
  • feel free to accept as an answer Dec 14, 2021 at 12:44
0

Let's say that if you've declared function as _showMyDialogue(), then you should use _showMyDialogue() when calling that function. In your case, you Ignored "_" while calling it. That's the problem.

5
  • It won't solve the issue Dec 13, 2021 at 15:16
  • Any error messages??
    – AML
    Dec 13, 2021 at 15:23
  • There will be no error messages, but you can't see dialog . Dec 13, 2021 at 15:24
  • Please test the issue before posting an answer. I don't like to down vote someone precious time. Dec 13, 2021 at 15:39
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Dec 13, 2021 at 21:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.