【问题标题】:Flutter showMenu() x axis positionFlutter showMenu() x 轴位置
【发布时间】: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


【解决方案1】:

这对我有用,我将width / 2 用于leftright,并将top 设置为y + h + 20(20 给它额外的空间)来自top .

                    position: new RelativeRect.fromLTRB(
                        w / 2, y + h + 20, w / 2, 0),

这是Container 上的完整工作 Code,还有一些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")),
              ),
            ),

【讨论】:

  • 您好,非常感谢您的帮助。但是它不起作用。当我按下右边的卡片时,它没有显示在右边。我每行有两张牌。
  • 嘿@latil68469 我认为您在错误的小部件上实现了onLongPress,请仔细检查您的代码,可能代码实现有误。
  • 我已经确定它是正确的并且还尝试了你的代码。会不会是建筑商的原因?我已经用最后的代码更新了我的帖子,谢谢!
  • @latil68469 到目前为止,您是否尝试删除扩展的小部件?如果这也不起作用,请粘贴您的 CustomCard 小部件的代码。
  • 我需要使用扩展的,因为我在列中使用 GridviewBuilder。
猜你喜欢
  • 2020-08-28
  • 1970-01-01
  • 1970-01-01
  • 2021-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-13
相关资源
最近更新 更多