asEdgeSpacing property

EdgeSpacingGeometry get asEdgeSpacing

Converts Flutter's EdgeInsetsGeometry to the flexbox EdgeSpacingGeometry equivalent.

This conversion enables seamless integration between Flutter's standard edge insets and the flexbox library's spacing system. The method handles both absolute (EdgeInsets) and directional (EdgeInsetsDirectional) insets.

For EdgeInsets, converts to EdgeSpacing preserving left, top, right, and bottom values. For EdgeInsetsDirectional, converts to EdgeSpacingDirectional preserving start, top, end, and bottom values.

Throws UnimplementedError if the edge insets type is not supported.

Example:

final padding = EdgeInsets.all(8.0);
final flexSpacing = padding.asEdgeSpacing; // EdgeSpacing with 8.0 spacing on all sides

Implementation

EdgeSpacingGeometry get asEdgeSpacing => switch (this) {
  EdgeInsets edgeInsets => EdgeSpacing.only(
    left: SpacingUnit.fixed(edgeInsets.left),
    top: SpacingUnit.fixed(edgeInsets.top),
    right: SpacingUnit.fixed(edgeInsets.right),
    bottom: SpacingUnit.fixed(edgeInsets.bottom),
  ),
  EdgeInsetsDirectional edgeInsetsDirectional => EdgeSpacingDirectional.only(
    start: SpacingUnit.fixed(edgeInsetsDirectional.start),
    top: SpacingUnit.fixed(edgeInsetsDirectional.top),
    end: SpacingUnit.fixed(edgeInsetsDirectional.end),
    bottom: SpacingUnit.fixed(edgeInsetsDirectional.bottom),
  ),
  _ => throw UnimplementedError(
    'EdgeInsetsGeometry type $runtimeType is not supported',
  ),
};