Flutter 颤振显示菜单()x轴位置
我有一个生成以下内容的Gridview构建器,我有一个带onLongPress的GestureDetector,它可以显示这样的菜单: 我能够将菜单与gridview的各个元素对齐,例如,如果我长按绿色框,则应在框下方显示菜单: 但是,我无法设置水平x位置 如果我设置L或R的值,它会将菜单移动到最左边或最右边,这不是我想要的。我想把菜单放在绿色框x轴的中间Flutter 颤振显示菜单()x轴位置,flutter,dart,flutter-layout,Flutter,Dart,Flutter Layout,我有一个生成以下内容的Gridview构建器,我有一个带onLongPress的GestureDetector,它可以显示这样的菜单: 我能够将菜单与gridview的各个元素对齐,例如,如果我长按绿色框,则应在框下方显示菜单: 但是,我无法设置水平x位置 如果我设置L或R的值,它会将菜单移动到最左边或最右边,这不是我想要的。我想把菜单放在绿色框x轴的中间 onLongPress: () { RenderBox box = key.currentContext.findRen
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"),
],
),
)
]);
},
海关卡
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),
),
],
),
),
),
);
返回手势检测器(
onTapDown:\u存储位置,
onLongPress:(){
展示菜单(
上下文:上下文,
位置:空,
项目:[
PopupMenuItem(
价值:1,
孩子:排(
儿童:[
图标(Icons.delete),
文本(“删除”),
],
),
)
]);
},
孩子:卡片(
颜色:d.color,
标高:5,
语义容器:对,
形状:圆形矩形边框(
边界半径:边界半径。圆形(10.0),
),
clipBehavior:Clip.antiAlias,
孩子:填充(
填充:边缘组。对称(垂直:20),
子:列(
crossAxisAlignment:crossAxisAlignment.center,
mainAxisAlignment:mainAxisAlignment.space,
儿童:[
正文(
d、 头衔,
样式:TextStyle(fontSize:20,fontWeight:fontWeight.w500),
),
],
),
),
),
);
这对我很有效,我将左侧和右侧的宽度/2
,并将顶部设置为y+h+20
(20给它额外的空间)从顶部设置为y+h+20
这是在一个容器
上的完整工作代码
,具有一些高度
和宽度
GestureDetector(
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());
showMenu(
context: context,
position: new RelativeRect.fromLTRB(
w / 2, y + h + 20, w / 2, 0),
items: <PopupMenuEntry>[
PopupMenuItem(
value: 1,
child: Row(
children: <Widget>[
Icon(Icons.delete),
Text("Delete"),
],
),
)
]);
},
child: new Container(
key: key,
color: Colors.red,
height: 120,
width: 300,
child: Center(child: new Text("Tap Me")),
),
),
手势检测器(
onLongPress:(){
RenderBox=key.currentContext.FindEnderObject();
double h=double.parse(box.size.height.toString());
偏移位置=box.localToGlobal(
Offset.zero);//这是全局位置
双y=position.dy;
双x=位置.dx;
double w=double.parse(box.size.width.toString());
展示菜单(
上下文:上下文,
位置:new.fromLTRB(
w/2,y+h+20,w/2,0),
项目:[
PopupMenuItem(
价值:1,
孩子:排(
儿童:[
图标(Icons.delete),
文本(“删除”),
],
),
)
]);
},
子容器:新容器(
钥匙:钥匙,
颜色:颜色,红色,
身高:120,
宽度:300,
子对象:中心(子对象:新文本(“点击我”),
),
),
嘿,你的位置:新位置。从ltrb(x,0,y+h,0),
看起来不合理。您正在将R设置为y+h。我猜你想要T或Bthat@ch271828n我尝试了许多不同的组合,但无法使x轴正常工作。嗨,非常感谢你的帮助。但是它不起作用。当我按下右边的卡片时,它没有显示在右边。我每行有两张卡。嘿@latil68469我想你实现的onLongPress on小部件是错误的,请仔细检查你的代码,也许代码实现中有错误。我已经确定它是正确的,并且尝试了你的代码。可能是建筑商的原因吗?我已经在最后用代码更新了我的帖子,谢谢@latil68469到目前为止,您是否尝试删除扩展的小部件?如果这也不起作用,那么请粘贴CustomCard小部件的代码。我需要使用扩展的,因为我在列中使用GridviewBuilder。
position: new RelativeRect.fromLTRB(
w / 2, y + h + 20, w / 2, 0),
GestureDetector(
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());
showMenu(
context: context,
position: new RelativeRect.fromLTRB(
w / 2, y + h + 20, w / 2, 0),
items: <PopupMenuEntry>[
PopupMenuItem(
value: 1,
child: Row(
children: <Widget>[
Icon(Icons.delete),
Text("Delete"),
],
),
)
]);
},
child: new Container(
key: key,
color: Colors.red,
height: 120,
width: 300,
child: Center(child: new Text("Tap Me")),
),
),