SpacingUnit class abstract
Abstract base class for spacing units used in margins, padding, and gaps.
SpacingUnit defines units that calculate spacing values (margins, padding, gaps) for elements within a layout container. Spacing units determine the whitespace around and between elements.
Spacing units can reference various layout properties:
- Fixed pixel values
- Viewport dimensions
- Child element sizes
- Constrained values with min/max bounds
Spacing units support mathematical operations and can be combined to create dynamic spacing expressions that respond to layout context.
Common uses include:
- Fixed padding/margins (e.g., 8px, 16px)
- Responsive spacing based on viewport size
- Gaps between flex items
- Dynamic padding based on content
Spacing is computed during layout after size constraints are known but before final positioning, allowing spacing to adapt to available space.
Example:
// Fixed spacing
final padding = SpacingUnit.fixed(16.0);
// Responsive spacing: 5% of viewport
final gap = SpacingUnit.viewportSize * 0.05;
// Constrained spacing with min/max
final adaptive = SpacingUnit.constrained(
spacing: SpacingUnit.viewportSize * 0.02,
min: SpacingUnit.fixed(8.0),
max: SpacingUnit.fixed(32.0),
);
- Implementers
- Available extensions
Constructors
- SpacingUnit()
-
Creates a const spacing unit.
const
- SpacingUnit.calc(SpacingUnit a, SpacingUnit b, CalculationOperation operation)
-
Creates a calculated spacing unit combining two spacing units with an operation.
constfactory
- SpacingUnit.childSize([Object? key])
-
Creates a spacing unit based on a child element's size.
constfactory
- SpacingUnit.constrained({required SpacingUnit spacing, SpacingUnit min, SpacingUnit max})
-
Creates a constrained spacing with min/max bounds.
constfactory
- SpacingUnit.fixed(double value)
-
Creates a fixed spacing with the specified value.
constfactory
- SpacingUnit.relative(double factor)
-
Creates a relative spacing unit with the specified factor.
constfactory
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
clamp(
{SpacingUnit min = const SpacingFixed(0), SpacingUnit max = const SpacingFixed(double.infinity)}) → SpacingUnit -
Available on SpacingUnit, provided by the SpacingUnitExtension extension
Constrains this spacing unit within the specified min and max bounds. -
computeSpacing(
{required ParentLayout parent, required LayoutAxis axis, required double viewportSize}) → double - Computes the actual spacing value.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toCodeString(
) → String - Returns a code string representation of this spacing unit.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator *(
Object other) → SpacingUnit -
Available on SpacingUnit, provided by the SpacingUnitExtension extension
Multiplies two spacing units. -
operator +(
SpacingUnit other) → SpacingUnit -
Available on SpacingUnit, provided by the SpacingUnitExtension extension
Adds two spacing units together. -
operator -(
SpacingUnit other) → SpacingUnit -
Available on SpacingUnit, provided by the SpacingUnitExtension extension
Subtracts one spacing unit from another. -
operator /(
SpacingUnit other) → SpacingUnit -
Available on SpacingUnit, provided by the SpacingUnitExtension extension
Divides one spacing unit by another. -
operator ==(
Object other) → bool -
The equality operator.
inherited
-
operator unary-(
) → SpacingUnit -
Available on SpacingUnit, provided by the SpacingUnitExtension extension
Negates this spacing unit (equivalent to 0 - this).
Static Methods
-
lerp(
SpacingUnit a, SpacingUnit b, double t) → SpacingUnit - Linearly interpolates between two spacing units.
Constants
- viewportSize → const SpacingUnit
- Spacing equal to the viewport size along the axis.
- zero → const SpacingUnit
- Zero spacing.