我有一个 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,)
)),
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)
],
),
)
试试这个,我希望这有帮助。
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,
),
)