6

Any ideas how I can remove the huge padding from a Flutter PopupmenuButton? Something like a shrinkWrap or even an alternative widget that can use? It's ruining the alignment of my elements.

I tried setting the padding to 0 but no effect at all.

padding: EdgeInsets.all(0)

enter image description here

enter image description here

4

5 回答 5

7

Supplying child rather than icon will allow you to use a custom widget with any desired size/padding.

Note: Icons.more_vert carries its own padding, but any custom icon can be used to avoid that.

PopupMenuButton(
  child: Container(
    height: 36,
    width: 48,
    alignment: Alignment.centerRight,
    child: Icon(
      Icons.more_vert,
    ),
  ),
  onSelected: (value) {},
  itemBuilder: (context) => [],
),
于 2021-07-19T16:52:27.483 回答
1

If you carefully see the padding is not in the PopupmenuButton the padding there is coming from the IconButton.IconButton is a Material Design widget that follows the spec that tappable objects need to be at least 48px on each side. So it's better you create your own widget to reduce the padding.

A simple workaround to avoid it will be to use Icon wrapped with GestureDetector.

You can refer to this post for more details.

于 2020-07-18T12:31:01.823 回答
0

From the Flutter Docs for the PopopMenuButton class,

padding → EdgeInsetsGeometry Matches IconButton's 8 dps padding by default. In some cases, notably where this button appears as the trailing element of a list item, it's useful to be able to set the padding to zero.

You can change the padding to 0 as suggested by the docs.

Read More here - https://api.flutter.dev/flutter/material/PopupMenuButton-class.html

于 2020-07-18T12:26:39.697 回答
0

A solution was provided but you have to edit non project files (PopupMenuItem class)

return InkWell(
      onTap: widget.enabled ? handleTap : null,
      child: Container(
        height: widget.height,
        padding: const EdgeInsets.symmetric(horizontal: _kHorizontalMenuPadding), // setting this to 0 worked 
        child: item,
      ),
    );

P.S: Not really recommended but still working

于 2020-07-18T12:32:06.757 回答
0

Solution: Wrap it with SizedBox:

SizedBox(
  height:,
  child: Row(
    children: [
      PopupMenuButton(        
      )
    ],
  ),
)
于 2021-06-30T11:04:31.863 回答