onReorder method

OnReorderParam<K> onReorder(
  1. int oldIndex,
  2. int newIndex
)

Returns OnReorderParam object for the callback function given as a parameter to the ExpandableReorderableListView.

Implementation

OnReorderParam<K> onReorder(int oldIndex, int newIndex) {
  var updatedOldIndex = oldIndex; // Normalized old index
  var updatedNewIndex = newIndex; // Normalized new index
  if (hasLeads) {
    updatedOldIndex--;
    updatedNewIndex--;
  }
  if (hasTails) {
    if (newIndex > childrenNumber + 1) {
      updatedNewIndex--;
    }
  }
  // Index of the previous item
  final newPreviousItemIndex = updatedNewIndex - 1;

  final item = itemsTree.itemFromIndex(
      index: updatedOldIndex,
      modelsController: modelsController)!; // The moved item
  ExpandableReorderableListItem<K>?
      newPreviousItem; // The item before the moved item after the drop
  ExpandableReorderableListItem<K>?
      newNextItem; // The item after the moved item after the drop

  final newNextItemIndex = newPreviousItemIndex + 1;
  if (!(newPreviousItemIndex < 0)) {
    newPreviousItem = itemsTree.itemFromIndex(
        index: newPreviousItemIndex, modelsController: modelsController);
  }
  if (!(newNextItemIndex >= childrenNumber ||
      (newNextItemIndex == childrenNumber && hasTails))) {
    newNextItem = itemsTree.itemFromIndex(
        index: newNextItemIndex, modelsController: modelsController);
  }

  return OnReorderParam<K>(
    item: item,
    newPreviousItem: newPreviousItem,
    newNextItem: newNextItem,
    oldIndex: updatedOldIndex,
    newIndex: updatedNewIndex,
  );
}