eventDoRollingInDragging method

void eventDoRollingInDragging(
  1. SortableItemState beHitItemState,
  2. SortableElement holdingElement
)

Events

Implementation

void eventDoRollingInDragging(SortableItemState beHitItemState, SortableElement holdingElement) {
  assert(draggingElement != null, 'Dragging status is a mess now, please check it out.');
  assert(draggingElement == holdingElement, 'Got a different dragging view, please check it out.');

  SortableElement dragging = draggingElement!;
  SortableElement element = beHitItemState.widget.element;

  int toIndex = animationElements.indexOf(element);
  int draggingIndex = animationElements.indexOf(dragging);
  bool isDraggingInSameRow = toIndex ~/ elementCountPerRow == draggingIndex ~/ elementCountPerRow;

  /// To lower index means user dragging to left, user dragging to left or top, the hit target should animate to right
  bool isDraggingToLowerIndex = toIndex < draggingIndex;
  int i = isDraggingToLowerIndex ? draggingIndex - 1 : draggingIndex + 1;
  for (; isDraggingToLowerIndex ? i >= toIndex : i <= toIndex; isDraggingToLowerIndex ? i-- : i++) {
    SortableElement e = animationElements[i];

    /// Swap the index in cached data
    int sourceIndex = i;
    int destinationIndex = isDraggingToLowerIndex ? i + 1 : i - 1;
    animationElements.swap(sourceIndex, destinationIndex);

    /// Handle animation by corresponding item's state
    SortableItemState itemState = e.state;
    itemState.sourceIndex = sourceIndex;
    itemState.destinationIndex = destinationIndex;
    itemState.startAnimation(isDraggingInSameRow);
  }

  /// Make sure you see the right thing on the right position
  setState(() {});
}