Виджет DragTarget не отвечает

Я думаю, что когда вы определяете String, вы определяете объект. Поэтому вам нужно использовать .equals(). Когда вы используете примитивные типы данных, вы используете ==, но с String (и любым объектом) вы должны использовать .equals().

2
задан Arash 10 March 2019 в 17:37
поделиться

1 ответ

Я разработал сетку для перетаскивания фотографий, где вы можете перетаскивать фотографии, чтобы изменить их порядок на основе числовых индексов.

По сути, я полагаю, это то же самое, что у вас есть концепция шахматной доски.

Возможно, проблема возникает из-за того, что элемент 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.
}

Я предполагаю, что если вы измените систему индексации на ваши собственные объекты - это будет работать и для вас.

Пожалуйста, дайте мне знать, если это помогло.

0
ответ дан Smagold 10 March 2019 в 17:37
поделиться
Другие вопросы по тегам:

Похожие вопросы: