skip to Main Content

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


  1. 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

    Login or Signup to reply.
  2. It is not issue about onTap and onPressed. If you want to showDialog or bottomsheet. It shows dialog for few milliseconds and popup immediately.

    Try this code:

                       PopupMenuButton(
                            itemBuilder: (context) => [
                              PopupMenuItem(
                                onTap: () {
                                  Future.delayed(
                                      Duration(
                                        seconds: 0,
                                      ),
                                      () {
                                        showModalBottomSheet(
                                          isScrollControlled: true,
                                          context: context,
                                          builder: (context) => SingleChildScrollView(
                                            child: Container(
                                              padding:
                                              EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
                                              child: EditTaskBottom(
                                                oldTask: task,
                                              ),
                                            ),
                                          ),
                                        );
                                      });
                                },
                                child: const Text('Edit'),
                              ),
                            ],
                          );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search