管理showMenu在flutter中的位置

问题描述 投票:0回答:2

我有一个 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,)
                )),
flutter flutter-layout flutter-dependencies flutter-animation
2个回答
0
投票

Ayman的这个答案可以帮到你!

他使用 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)
      ],
    ),
  )

0
投票

试试这个,我希望这有帮助。

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,
 ),
)

© www.soinside.com 2019 - 2024. All rights reserved.