Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjohnsullivan/63277aefd0e212cfd1628fe85488bc35 to your computer and use it in GitHub Desktop.
Save mjohnsullivan/63277aefd0e212cfd1628fe85488bc35 to your computer and use it in GitHub Desktop.
void determineDragPosition(BuildContext itemContext,
DragUpdateDetails details, Item draggedItem, double itemHeight) {
_dragOperation.offset.value = details.globalPosition;
// Find the fixed extent list renderer.
RenderSliverFixedExtentList extentList;
for (var p = itemContext.findRenderObject();
p != null;
p = p is RenderObject ? p.parent as RenderObject : null) {
if (p is RenderSliverFixedExtentList) {
extentList = p;
break;
}
}
if (extentList == null) {
// We failed.
return;
}
// We've got an extent list. Get the first child and find its top.
var first = extentList.firstChild;
// Compute the scroll offset of the first visible item (N.B. this is not
// the first item in the list, it's the first one that's probably
// visible).
var offsetOfFirst = extentList.childScrollOffset(first);
// We know this item's in the tree, so we can call globalToLocal on it.
var local = first.globalToLocal(details.globalPosition);
// Now we know where our cursor is relative to the first visible item.
var y = offsetOfFirst + local.dy;
// Compute index of item the cursor is over.
var index = (y / itemHeight).floor();
// Compute offset over that row (how close are we to the top of the row
// we're hovering).
var localOffset = y % itemHeight;
// Use index to compute drop target.
var dropTarget = index >= 0 && index < _flat.length ? _flat[index] : null;
// Drop logic goes here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment