moveItemToGrid method

LayoutItem? moveItemToGrid({
  1. required DashboardController from,
  2. required DashboardController to,
  3. required String itemId,
  4. int? x,
  5. int? y,
})

Moves itemId from from to to, preserving its size and constraints.

When x/y are omitted the item is auto-placed by the target's compaction strategy. Returns the placed item, or null when the item was not found. All-or-nothing: on failure neither grid is modified.

Implementation

LayoutItem? moveItemToGrid({
  required DashboardController from,
  required DashboardController to,
  required String itemId,
  int? x,
  int? y,
}) {
  if (identical(from, to)) return null;
  LayoutItem? item;
  for (final i in from.layout.value) {
    if (i.id == itemId) {
      item = i;
      break;
    }
  }
  if (item == null) return null;
  assert(
    !to.layout.value.any((i) => i.id == itemId),
    'moveItemToGrid: target grid already contains an item with id "$itemId"',
  );

  from.removeItems([itemId]);
  // Explicit coordinates are honored as-is by the placement engine; when
  // omitted, (-1, -1) triggers auto-placement with the appendBottom strategy.
  final moved = (x != null && y != null)
      ? item.copyWith(x: x, y: y, moved: false)
      : item.copyWith(x: -1, y: -1, moved: false);
  to.addItem(moved);
  var placed = moved;
  for (final i in to.layout.value) {
    if (i.id == itemId) {
      placed = i;
      break;
    }
  }
  onItemMovedToGrid?.call(placed, from, to);
  return placed;
}