我有一个Gridview构建器,它生成以下内容,我有一个带有onLongPress的GestureDetector,它显示了这样的菜单:
我可以将菜单与gridview的相应元素对齐,例如,如果我长按绿色框,它应该显示框下方的菜单:
但是,我无法设置水平x位置。
如果我设置L或R的值,它会将菜单移到最左边或最右边,这不是我想要的。我想将菜单居中到绿色框的x轴的中间。
onLongPress: () {
RenderBox box = key.currentContext.findRenderObject();
double h = double.parse(box.size.height.toString());
Offset position =
box.localToGlobal(Offset.zero); //this is global position
double y = position.dy;
double x = position.dx;
double w = double.parse(box.size.width.toString());
print(x);
showMenu(
context: context,
position: new RelativeRect.fromLTRB(x, 0, y + h, 0),
items: <PopupMenuEntry>[
PopupMenuItem(
value: 1,
child: Row(
children: <Widget>[
Icon(Icons.delete),
Text("Delete"),
],
),
)
]);
},
简而言之,我想做一些类似的事情:
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: GridView.builder(
shrinkWrap: false,
itemCount: data.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
childAspectRatio: 10.0 / 10.0,
crossAxisCount: 2,
),
itemBuilder: (BuildContext context, int index) {
GlobalKey _key = GlobalKey();
return Padding(
padding: EdgeInsets.all(10),
child: CustomCard(data[index], _key),
);
},
),
),
],
),
);
海关卡
return GestureDetector(
onTapDown: _storePosition,
onLongPress: () {
showMenu(
context: context,
position:null,
items: <PopupMenuEntry>[
PopupMenuItem(
value: 1,
child: Row(
children: <Widget>[
Icon(Icons.delete),
Text("Delete"),
],
),
)
]);
},
child: Card(
color: d.color,
elevation: 5,
semanticContainer: true,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
clipBehavior: Clip.antiAlias,
child: Padding(
padding: EdgeInsets.symmetric(vertical: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text(
d.title,
style: TextStyle(fontSize: 20, fontWeight: FontWeight.w500),
),
],
),
),
),
);
2条答案
按热度按时间kyxcudwk1#
width / 2
用于left
和right
,并将top
从top
设置为y + h + 20
(20以给予额外的空间)。Code
* 在一个Container
与一些height
和width
。fykwrbwg2#
试试这个,希望有帮助。