dart Flutter showMenu()x轴位置

qnzebej0  于 2023-03-27  发布在  Flutter
关注(0)|答案(2)|浏览(149)

我有一个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),
              ),
            
             
            ],
          ),
        ),
      ),
    );
kyxcudwk

kyxcudwk1#

  • 这对我来说很有效,我将width / 2用于leftright,并将toptop设置为y + h + 20(20以给予额外的空间)。
position: new RelativeRect.fromLTRB(
                        w / 2, y + h + 20, w / 2, 0),
  • 这是完整的工作Code* 在一个Container与一些heightwidth
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")),
              ),
            ),
fykwrbwg

fykwrbwg2#

试试这个,希望有帮助。

GestureDetector(
 onLongPressStart: (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,
 ),
)

相关问题