颤动。将一个可拖动项拖到另一个可拖项上

人气:913 发布:2022-10-16 标签: flutter flutter-layout

问题描述

我有下面的黑板。有一个覆盖(Stack)的2x10小部件,一些透明的小部件DragTargets,在它们上面,相同的宽度和高度的小部件DraggableDraggable是包含图像的那些。

我面临的问题是,当我拖动上面的方法时,下面这些方法中的onWillAcceptonAccept方法没有被调用。我猜是因为有小工具覆盖了它们。

我已调换小工具,使DragTargets覆盖Draggable小工具,但现在无法拖动它们,因为它们位于其他小工具的下方。

拖放的所有示例都是将一个项目从一个位置拖放到另一个位置,一个可拖动的,一个DragTarget。我需要一个小部件(或屏幕上的一个区域)既是可拖动的,也是DragTarget的。我如何才能做到这一点?

这是布局

          Align(
            child: Container(
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage("images/user_board.jpg"),
                  alignment: Alignment.bottomCenter,
                ),
              ),
              height: _userBoardH,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  Container(
                      height: _tileH,
                      child: Stack(children: <Widget>[
                        Row(
                          children: _buildSlots(ScreenBoard.FIRST_ROW),
                        ),
                        Row(
                          children: _buildTiles(ScreenBoard.FIRST_ROW),
                        ),
                      ])),
                  Container(
                      height: _tileH,
                      child: Stack(children: <Widget>[
                        Row(
                          children: _buildSlots(ScreenBoard.SECOND_ROW),
                        ),
                        Row(
                          children: _buildTiles(ScreenBoard.SECOND_ROW),
                        ),
                      ])),
                ],
              ),
            ),
            alignment: Alignment.bottomCenter,
          ),

干杯。

编辑

按照帕特里克的建议解决了这个问题。以下是解决方案:

Widget _buildTile(Tile widget) {
  return DragTarget<Map>(
    builder: (context, candidateData, rejectedData) {
      return Container(
        child: Draggable<Map>(
          child: _getTile(widget),
          feedback: _getTile(widget),
          childWhenDragging: Container(
            width: widget._dimenW,
            height: widget._dimenH,
          ),
          data: {
            Tile.DATA_TILE_ROW_TYPE: widget._rowType,
            Tile.DATA_TILE_ROW_POS: widget._rowPosition,
            Tile.DATA_TILE_ID: widget._tileId,
          },
        ),
      );
    },
    onWillAccept: (data) {
      UDebug.log("onWillAccept");
      return false;
    },
    onAccept: (data) {
      UDebug.log("onAccept");
    },
  );
}

推荐答案

将其包装在容器()小部件中,并将图像放入BoxDecotion()中,然后使用Child:属性保存Draggable()。

这不会妨碍您的指针事件。

553