/// Where a dragged item of the open column would land. /// /// [position] is the dragged item's place in slots (its start index, plus /// the slots it moved up; fractional), [from] its start index, [count] the /// items, and [slot] the gap it opened so far (never past [lastSlot]). The /// others are drawn around that gap. The middle half of each one /// [canDropOnto] accepts is a drop on it; its outer quarters (or all of one /// that accepts none) move the gap before or after it. The /// gap itself holds while the item is within half a slot of it, so the /// answer is stable: an item that slides aside never lands back under the /// finger. ({int slot, int? onto}) chatStackDropTarget({ required double position, required int from, required int count, required int slot, bool Function(int index)? canDropOnto, int? lastSlot, }) { final last = lastSlot ?? count - 1; if (count < 2 || (position - slot).abs() <= .5) { return (slot: slot, onto: null); } // The neighbour drawn nearest the position: its index and drawn place. var nearest = -1; var drawn = 0; var distance = double.infinity; for (var i = 0; i < count; i++) { if (i == from) continue; final rank = i < from ? i : i - 1; final place = rank < slot ? rank : rank + 1; final d = (position - place).abs(); if (d < distance) { distance = d; nearest = i; drawn = place; } } if (distance <= .25 && (canDropOnto?.call(nearest) ?? false)) { return (slot: slot, onto: nearest); } final int next; if (position < drawn) { // Before it (below, toward the thumb). next = drawn > slot ? drawn - 1 : drawn; } else { next = drawn < slot ? drawn + 1 : drawn; } return (slot: next.clamp(0, last), onto: null); }