Skip to content

Conversation

@0xNF
Copy link

@0xNF 0xNF commented Apr 15, 2024

This is kind of related to #2, though I didn't fully understand what they were trying to do.

I've added an AcceptDetails class that contains the old index, and the new index, of the reordered data on OnWillAccept and OnAccept. This lets users allow their own list to track changes, for example:

ReorderableStaggeredScrollView.grid(
          crossAxisCount: 4,
          onAccept: (item1, item2, value, {AcceptDetails? acceptDetails}) {
            print("Moved From: ${acceptDetails?.oldIndex} To ${acceptDetails?.newIndex}");
            if (acceptDetails != null) {
              setState(() {
                final shifted = cards.removeAt(acceptDetails.oldIndex);
                cards.insert(acceptDetails.newIndex, shifted);
              });
            }
          },
          children: _gridItems,
        );
      })

The core of this is the AcceptDetails class:

class AcceptDetails {
  /// The previous index an item was located at
  final int oldIndex;

  /// The new index an item was relocated to
  final int newIndex;

  const AcceptDetails({required this.oldIndex, required this.newIndex});
}

A field of this type is added to the _DragContainerState, which keeps track of where this item is being pulled and pushed to:

class _DragContainerState<T extends ReorderableStaggeredScrollViewListItem> extends State<DragContainer> {
  [...]
  int? originalindex;
  AcceptDetails? acceptDetails;
  [...]
}

This field is reset at the same time as endWillAccept(), and is set during setWillAccept() in the non-null moveData case.

Additionally, an originalIndex field is added, which helps keep track of final moves, otherwise I found I was unintentionally tracking all potential accepts as well, which wasn't what I wanted. OriginalIndex is updated in the setDragStart method.

I'd be happy to talk about better implementations, this is just what I'm working with in my own fork, I tried to keep the changes minimal, especially with modifying the public API, but if you were willing to change it a bit, then I think we could probably get better function signatures if you wanted.

…ing index location for where an item was taken from, and where it was relocated to
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant