moveCluster function

Layout moveCluster(
  1. Layout layout,
  2. Set<String> clusterIds,
  3. int targetX,
  4. int targetY, {
  5. required int cols,
  6. required CompactType compactType,
  7. bool preventCollision = false,
  8. DashboardPolicy? policy,
  9. bool allowAutoShrink = false,
})

Moves a group of items (cluster) together.

layout The full layout. clusterIds The IDs of the items to move. targetX The target X coordinate for the top-left of the bounding box. targetY The target Y coordinate for the top-left of the bounding box.

Implementation

Layout moveCluster(
  Layout layout,
  Set<String> clusterIds,
  int targetX,
  int targetY, {
  required int cols,
  required CompactType compactType,
  bool preventCollision = false,
  DashboardPolicy? policy,
  bool allowAutoShrink = false,
}) {
  if (clusterIds.isEmpty) return layout;

  // 1. Separate Cluster (excluding static items) and Obstacles (including static items)
  final cluster = layout
      .where((i) => clusterIds.contains(i.id) && !(i.isStatic && !i.isSectionBarrier))
      .toList();
  final obstacles = layout
      .where((i) => !clusterIds.contains(i.id) || (i.isStatic && !i.isSectionBarrier))
      .toList();

  if (cluster.isEmpty) return layout;

  // 2. Calculate Bounding Box
  // If the cluster represents a single item, we preserve its original ID
  // to allow the declarative policy to perform precise ID/type lookups during dragging.
  final bboxId = cluster.length == 1 ? cluster.first.id : 'cluster_bbox';
  final bbox = calculateBoundingBox(cluster, id: bboxId);

  // Create virtual moved BBox to calculate pre-collisions
  final targetBBox = bbox.copyWith(x: targetX, y: targetY);
  final directCollisions = getAllCollisions(obstacles, targetBBox);

  // 3. Move the Bounding Box against Obstacles
  // We treat the bbox as a single item being moved in a layout consisting of obstacles.
  // We add the bbox to the obstacles list for the moveElement function to work.
  var layoutForMove = [...obstacles, bbox];

  // If auto-shrink is enabled and collisions are detected with dynamic items,
  // we first try to dynamically contract the size of neighbors to avoid massive layout shifts.
  if (allowAutoShrink && directCollisions.isNotEmpty && !directCollisions.any((i) => i.isStatic)) {
    final shrunk = _tryShrinkMoveCollisions(layoutForMove, targetBBox, directCollisions);
    if (shrunk != null) {
      layoutForMove = shrunk;
    }
  }

  final resultLayoutWithBBox = moveElement(
    layoutForMove,
    bbox,
    targetX,
    targetY,
    cols: cols,
    compactType: compactType,
    preventCollision: preventCollision,
    force: true, // Force move to trigger collision resolution
    policy: policy,
  );

  // 4. Extract the new Bounding Box position
  // It might have been pushed by static items or boundaries
  final newBBox = resultLayoutWithBBox.firstWhere((i) => i.id == bbox.id);

  // 5. Calculate Delta (Movement vector)
  final dx = newBBox.x - bbox.x;
  final dy = newBBox.y - bbox.y;

  // 6. Apply Delta to Cluster Items
  final movedCluster = cluster.map((item) {
    return item.copyWith(
      x: item.x + dx,
      y: item.y + dy,
      moved: true, // Mark as moved
    );
  }).toList();

  // 7. Reconstruct Final Layout
  // Take the result from moveElement (which has pushed obstacles),
  // remove the virtual bbox, and add moved cluster items.
  final finalLayout = resultLayoutWithBBox
      .where((i) => i.id != bbox.id) // Remove virtual bbox
      .toList()
    ..addAll(movedCluster)
    // ID-based Index Stability: without this sort the dragged cluster is
    // appended at the tail, its sliver index changes on the first drag frame
    // and again on drop, and Flutter's child manager reorders/deactivates
    // elements (BuildOwner.finalizeTree / _InactiveElements._unmount spikes).
    ..sort((a, b) => a.id.compareTo(b.id));

  return finalLayout;
}