sortDrawOrder method Null safety

void sortDrawOrder()

Implementation

void sortDrawOrder() {
  // Clear out rule first/last items.
  for (final rule in _sortedDrawRules) {
    rule.first = rule.last = null;
  }

  _firstDrawable = null;
  Drawable? lastDrawable;
  for (final drawable in _drawables) {
    var rules = drawable.flattenedDrawRules;

    var target = rules?.activeTarget;
    if (target != null) {
      if (target.first == null) {
        target.first = target.last = drawable;
        drawable.prev = drawable.next = null;
      } else {
        target.last?.next = drawable;
        drawable.prev = target.last;
        target.last = drawable;
        drawable.next = null;
      }
    } else {
      drawable.prev = lastDrawable;
      drawable.next = null;
      if (lastDrawable == null) {
        lastDrawable = _firstDrawable = drawable;
      } else {
        lastDrawable.next = drawable;
        lastDrawable = drawable;
      }
    }
  }

  for (final rule in _sortedDrawRules) {
    if (rule.first == null) {
      continue;
    }
    switch (rule.placement) {
      case DrawTargetPlacement.before:
        if (rule.drawable?.prev != null) {
          rule.drawable!.prev?.next = rule.first;
          rule.first?.prev = rule.drawable!.prev;
        }
        if (rule.drawable == _firstDrawable) {
          _firstDrawable = rule.first;
        }
        rule.drawable?.prev = rule.last;
        rule.last?.next = rule.drawable;
        break;
      case DrawTargetPlacement.after:
        if (rule.drawable?.next != null) {
          rule.drawable!.next!.prev = rule.last;
          rule.last?.next = rule.drawable?.next;
        }
        if (rule.drawable == lastDrawable) {
          lastDrawable = rule.last;
        }
        rule.drawable?.next = rule.first;
        rule.first?.prev = rule.drawable;
        break;
    }
  }

  _firstDrawable = lastDrawable;
}