管理flutter中showMenu的位置

vwkv1x7d  于 2023-03-31  发布在  Flutter
关注(0)|答案(2)|浏览(267)

我有一个GestureDetector,在onPressed上我有一个showMenu,但我无法使菜单靠近我创建的按钮。

下面是我的代码:请注意,我已经使用RelativeRect.fill来运行这个应用程序。

ElevatedButton(
                style: ElevatedButton.styleFrom(
                  primary: ColorConstants.kContainerColor,
                  shape:  RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10),
                  ),
                ),
                onPressed: (){
                  showMenu(context: context, position: RelativeRect.fill, items: [
                    PopupMenuItem(child: Text('hello')),
                    PopupMenuItem(child: Text('hello')),
                    PopupMenuItem(child: Text('hello')),
                    PopupMenuItem(child: Text('hello')),
                  ]);
                }, child: Padding(
                  padding: const EdgeInsets.all(12),
                  child: SvgPicture.asset('images/menubar.svg',
                  color: Colors.white,
                  height: 27,
                  width: 27,)
                )),
z9ju0rcb

z9ju0rcb1#

来自Ayman的This answer可以帮助您!
他使用了PopupMenuButton而不是showMenu

PopupMenuButton<int>(
    offset: const Offset(0, -380),
    itemBuilder: (context) => [
      PopupMenuItem<int>(
          value: 0,
          child: PopUpMenuTile(
            isActive: true,
            icon: Icons.fiber_manual_record,
            title:'Stop recording',
          )),
      PopupMenuItem<int>(
          value: 1,
          child: PopUpMenuTile(
            isActive: true,
            icon: Icons.pause,
            title: 'Pause recording',
          )),
      PopupMenuItem<int>(
          value: 2,
          child: PopUpMenuTile(
            icon: Icons.group,
            title: 'Members',
          )),
      PopupMenuItem<int>(
          value: 3,
          child: PopUpMenuTile(
            icon: Icons.person_add,
            title: 'Invite members',
          )),
    ],
    child: Column(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        Icon(Icons.more_vert,
            color: Colors.white60),
        Text(translate('more'),
            style: Theme.of(context)
                .textTheme
                .caption)
      ],
    ),
  )
plicqrtu

plicqrtu2#

试试这个,希望有帮助。

GestureDetector(
 onTapDown: (details) async{
  final offset = details.globalPosition;

  showMenu(
    context: context,
    position: RelativeRect.fromLTRB(
      offset.dx,
      offset.dy,
      MediaQuery.of(context).size.width - offset.dx,
      MediaQuery.of(context).size.height - offset.dy,
    ),
    items: [
      PopupMenuItem(
        child: Text("0"),
      ),

      PopupMenuItem(
        child: Text("1"),
      ),

      PopupMenuItem(
        child: Text("2"),
      ),

    ]
);

},
 child: FaIcon(
   AppIcons.ellipseFa,
   size: 22,
 ),
)

相关问题