CssSpacing.sides constructor

CssSpacing.sides({
  1. CssLength? top,
  2. CssLength? right,
  3. CssLength? bottom,
  4. CssLength? left,
})

Create spacing from individual sides.

Only non-null values are included. If all four are provided, uses TRBL syntax. This is a convenience factory for cases where you want named parameters.

Implementation

factory CssSpacing.sides({
  CssLength? top,
  CssLength? right,
  CssLength? bottom,
  CssLength? left,
}) {
  // If all four are the same and provided
  if (top != null && right == null && bottom == null && left == null) {
    return _CssSpacingAll(top);
  }

  // Count non-null values
  final hasTop = top != null;
  final hasRight = right != null;
  final hasBottom = bottom != null;
  final hasLeft = left != null;

  if (hasTop && hasRight && hasBottom && hasLeft) {
    return _CssSpacingFour(top, right, bottom, left);
  }

  // For partial values, use raw with the values we have
  // This is a fallback - ideally users use the specific factories
  final parts = <String>[];
  if (hasTop) parts.add(top.toCss());
  if (hasRight) parts.add(right.toCss());
  if (hasBottom) parts.add(bottom.toCss());
  if (hasLeft) parts.add(left.toCss());

  if (parts.isEmpty) {
    return _CssSpacingAll(CssLength.zero);
  }

  return _CssSpacingRaw(parts.join(' '));
}