Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter DragTarget小部件没有响应_Flutter - Fatal编程技术网

Flutter DragTarget小部件没有响应

Flutter DragTarget小部件没有响应,flutter,Flutter,我正在用flatter编写一个国际象棋游戏 这是我代码的相关部分: 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'.cod

我正在用flatter编写一个国际象棋游戏

这是我代码的相关部分:

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

}
class-Rank扩展了小部件{
最终编号;
等级(本编号);
@凌驾
小部件构建(构建上下文){
var-widgets=[];
对于(变量j='a'.codeunit(0);j打印(“离开”);
}
}
类BoardSquare扩展了无状态小部件{
最终字符串_坐标;
BoardSquare(此坐标);
@凌驾
小部件构建(构建上下文){
ChessBloc bloc=ChessBlocProvider.of(上下文);
返回
StreamBuilder(
流:bloc.chessState,
生成器:(上下文,异步){
返回DragablePieceWidget(chess.data.get(_坐标),_坐标);
});
}
}
类DragablePieceWidget扩展了无状态Widget{
最后一盘国际象棋。棋子;
最终字符串_坐标;
DragablePieceWidget(this.\u piece,String this.\u坐标);
@凌驾
小部件构建(构建上下文){
回拖式(
子项:PieceWidget(_-piece),
反馈:PieceWidget(_-piece),
ChildWhenDraging:PieceWidget(null),
数据:{“工件”:{u工件,“原点”:{u坐标},
);
}
}
现在的问题是,我可以很好地拖动工件,但不能放下它们。未调用
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.
}
我假设如果您将索引系统更改为您拥有的自定义对象,这也适用于您


请告诉我这是否有帮助。

构建树中的
Rank
DraggablePieceWidget
在哪里?看看这个链接,也许这会对你有所帮助。