pdfThumbnailDropIndexAt function
int?
pdfThumbnailDropIndexAt({
- required BuildContext? panelContext,
- required Map<
int, GlobalKey< tileKeys,State< >StatefulWidget> > - required int pageCount,
- required Axis axis,
- required Offset globalPosition,
- bool reversed = false,
The insertion index a drop at globalPosition lands on for a panel
whose tiles are laid out along axis, or null when the point is
outside the panel.
panelContext bounds the hit to the panel's own box; tileKeys holds
the panel's per-page tile keys (only the built ones answer - a
scrolled-away tile has no render object, and the nearest built tile
then wins). The result is a slot: 0..pageCount, the position the
dropped pages would take.
Implementation
int? pdfThumbnailDropIndexAt({
required BuildContext? panelContext,
required Map<int, GlobalKey> tileKeys,
required int pageCount,
required Axis axis,
required Offset globalPosition,
bool reversed = false,
}) {
final panel = panelContext?.findRenderObject();
if (panel is! RenderBox || !panel.hasSize || !panel.attached) return null;
final local = panel.globalToLocal(globalPosition);
if (!(Offset.zero & panel.size).contains(local)) return null;
if (pageCount <= 0) return 0;
int? nearest;
Rect? nearestRect;
var nearestDistance = double.infinity;
for (final entry in tileKeys.entries) {
if (entry.key >= pageCount) continue;
final box = entry.value.currentContext?.findRenderObject();
if (box is! RenderBox || !box.hasSize || !box.attached) continue;
final rect = box.localToGlobal(Offset.zero) & box.size;
final distance = _distanceTo(rect, globalPosition);
if (distance < nearestDistance) {
nearest = entry.key;
nearestRect = rect;
nearestDistance = distance;
}
}
if (nearest == null || nearestRect == null) return null;
final centre = nearestRect.center;
var after = axis == Axis.vertical
? globalPosition.dy > centre.dy
: globalPosition.dx > centre.dx;
if (reversed) after = !after;
return (nearest + (after ? 1 : 0)).clamp(0, pageCount);
}