buildSortedLinkedList method

void buildSortedLinkedList()

Builds a sorted linked list of children based on their paint order.

Children without a paint order default to 0 and are painted in their natural layout order. Children with a positive paint order are painted above them.

Implementation

void buildSortedLinkedList() {
  final first = paintOrderFirstChild;
  if (first == null) {
    firstSortedChild = null;
    lastSortedChild = null;
    return;
  }

  rendering.RenderBox? child = first;
  rendering.RenderBox? prev;
  bool needsSort = false;
  int childCount = 0;

  while (child != null) {
    final parentData = child.parentData! as PaintOrderParentDataMixin;
    final order = parentData.paintOrder;
    parentData.paintIndex = order ?? 0;
    if (order != null) {
      needsSort = true;
    }
    parentData.previousSortedSibling = prev;
    if (prev != null) {
      (prev.parentData! as PaintOrderParentDataMixin).nextSortedSibling =
          child;
    }
    prev = child;
    child = parentData.nextSibling;
    childCount++;
  }

  if (prev != null) {
    (prev.parentData! as PaintOrderParentDataMixin).nextSortedSibling = null;
  }

  firstSortedChild = first;
  lastSortedChild = prev;

  if (!needsSort) {
    return;
  }

  // Merge sort the linked list
  firstSortedChild = _mergeSort(first, childCount);

  // Fix previous pointers
  rendering.RenderBox? current = firstSortedChild;
  prev = null;
  while (current != null) {
    final parentData = current.parentData! as PaintOrderParentDataMixin;
    parentData.previousSortedSibling = prev;
    prev = current;
    current = parentData.nextSortedSibling;
  }
  lastSortedChild = prev;
}