Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter. Drag a Draggable item onto another Draggable item

I have the below board. There is an overlay (Stack) of 2 x 10 widgets, some transparent ones which are DragTargets and above them, the same width and height there are ones which are Draggable. The Draggable ones are the ones containing the images.

enter image description here

The problem that I'm facing is that the onWillAccept and onAccept methods from the ones below, are not called while I drag the ones above. I'm guessing because there are widgets overlaying them.

I've switched the widgets around, having the DragTargets overlay-ing the Draggable ones but now I can't drag them as they're below other widgets.

All examples of drag-and-drops are dragging an item from a place and placing it on another, one draggable, one dragTarget. I require a widget (or a zone on the screen) to be both draggable and a dragTarget. How could I accomplish this?

Here's the layout

          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,
          ),

Cheers.

EDIT

Solved it by following Patrick's suggestion. Here's the solution:

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");
    },
  );
}
like image 691
AndreiBogdan Avatar asked Oct 08 '19 17:10

AndreiBogdan


People also ask

Can we do drag and drop in flutter?

Flutter provides a widget called LongPressDraggable that provides the exact behavior that you need to begin a drag-and-drop interaction. A LongPressDraggable widget recognizes when a long press occurs and then displays a new widget near the user's finger.

How do you implement drag and drop in flutter?

Draggable is a Flutter widget that you can drag or move around. As soon as the user click and starts dragging the Draggable widget, a new feedback widget appears and follows the user's finger or mouse pointer. When the user lifts the finger or mouse pointer, the feedback widget disappears.

What is drag target in flutter?

As the name suggests, DragTarget means a widget which provides target or destination to Draggable, it also receives the data from a Draggable.


1 Answers

Wrap it in a Container() widget and put the image in a BoxDecoration() and use the child: property to hold your Draggable().

This won't obstruct your pointer events.

like image 81
Patrick Kelly Avatar answered Oct 01 '22 10:10

Patrick Kelly