我正在用flutter编写一个国际象棋游戏代码。
这就是我代码的相关部分:

class Rank extends StatelessWidget {
  final _number;

  Rank(this._number);

  @override
  Widget build(BuildContext context) {
    var widgets = <Widget>[];
    for (var j = 'a'.codeUnitAt(0); j <= 'h'.codeUnitAt(0); j++) {
      widgets
          .add(
              DroppableBoardSquare(String.fromCharCode(j) + this._number.toString())
          );
          //
    }
    return Row(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: widgets);
  }
}
class DroppableBoardSquare extends StatelessWidget {
  final String _coordinate;

  const DroppableBoardSquare(this._coordinate) ;

  @override
  Widget build(BuildContext context) {
    return DragTarget(
      builder:(BuildContext context, List candidate, List rejectedData){
      return BoardSquare(_coordinate);
    },
        onAccept: (data ) {
          print('Accepted');
        },
    onWillAccept: (data){
    return true;
    },
    onLeave: (data) => print("leave"),);

  }

}
class BoardSquare extends StatelessWidget {
  final String _coordinate;

  BoardSquare(this._coordinate);

  @override
  Widget build(BuildContext context) {
    ChessBloc bloc = ChessBlocProvider.of(context);
    return
        StreamBuilder<chess.Chess>(
        stream: bloc.chessState,
        builder: (context, AsyncSnapshot<chess.Chess> chess) {
          return DraggablePieceWidget(chess.data.get(_coordinate), _coordinate);

        });
  }
}

class DraggablePieceWidget extends StatelessWidget {
  final chess.Piece _piece;

  final String _coordinate;


  DraggablePieceWidget(this._piece, String this._coordinate);

  @override
  Widget build(BuildContext context) {
    return Draggable(
      child: PieceWidget(_piece),
      feedback: PieceWidget(_piece),
      childWhenDragging: PieceWidget(null),
      data: {"piece": _piece, "origin": _coordinate} ,
    );
  }

}

现在的问题是,我可以很好地拖动它,但不能将其放下。未调用DragTarget上的任何方法。
我做错了什么?

最佳答案

我开发了一个拖放式照片网格,您可以在其中拖动照片以根据数字索引对其重新排序。
基本上,我认为,这和你所拥有的棋盘概念是一样的。
该问题可能是由于Draggable (DraggablePieceWidget)元素位于DragTarget (DroppableBoardSquare)内部而发生的。
在我的应用程序中,我用另一种方法实现了它——我把DragTarget放到了Draggable中。
以一些伪代码为例:

int _dragSelectedIndex;
int _draggingIndex;

// Basically this is what you'd use to build every chess item
Draggable(
  maxSimultaneousDrags: 1,
  data: index,
  onDragStarted: () { _draggingIndex = index; print("Debug: drag started"); }, // Use setState for _draggingIndex, _dragSelectedIndex.
  onDragEnd: (details) { onDragEnded(); _draggingIndex = null; print("Debug: drag ended; $details"); },
  onDraggableCanceled: (_, __) { onDragEnded(); _draggingIndex = null; print("Debug: drag cancelled."); },
  feedback: Material(type: MaterialType.transparency, child: Opacity(opacity: 0.85, child: Transform.scale(scale: 1.1, child: createDraggableBlock(index, includeTarget: false)))),
  child: createDraggableBlock(index, includeTarget: true),
);

// This func is used in 2 places - Draggable's `child` & `feedback` props.
// Creating dynamic widgets through functions is a bad practice, switch to StatefulWidget if you'd like.
Widget createDraggableBlock(int index, { bool includeTarget = true }) {
  if (includeTarget) {
    return DragTarget(builder: (context, candidateData, rejectedData) {
      if (_draggingIndex == index || candidateData.length > 0) {
        return Container(); // Display empty widget in the originally selected cell, and in any cell that we drag the chess over.
      }
      // Display a chess, but wrapped in DragTarget widget. All chessboard cells will be displayed this way, except for the one you start dragging.
      return ChessPiece(..., index: index);
    }, onWillAccept: (int elemIndex) {
      if (index == _draggingIndex) {
        return false; // Do not accept the chess being dragged into it's own widget
      }
      setState(() { _dragSelectedIndex = index; });
      return true;
    }, onLeave: (int elemIndex) {
      setState(() { _dragSelectedIndex = null; });
    });
  }
  // Display a chess without DragTarget wrapper, e.g. for the draggable(feedback) widget
  return ChessPiece(..., index: index);
}

onDragEnded() {
  // Check whether _draggingIndex & _dragSelectedIndex are not null and act accordingly.
}

我假设如果您将索引系统更改为您拥有的自定义对象,这也适用于您。
如果这有帮助,请告诉我。

关于flutter - DragTarget小部件没有响应,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55090488/

10-10 19:57