【发布时间】:2021-04-07 15:57:33
【问题描述】:
我有一个生成以下内容的 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),
),
],
),
),
),
);
【问题讨论】:
-
嘿,您的
position: new RelativeRect.fromLTRB(x, 0, y + h, 0),似乎不合理。您将 R 设置为 y+h。我猜你想要 T 或 B 那样 -
@ch271828n 我尝试了许多不同的组合,但无法让 x 轴正常工作。
标签: flutter dart flutter-layout