buildValue method

  1. @override
String buildValue(
  1. dynamic value
)
override

Builds the value for the specific type.

Make sure to only override this method with any new DesignTokenParser you create. Please avoid directly calling this method outside DesignTokenParser. Prefer DesignTokenParser.parse.

Implementation

@override
String buildValue(value) {
  if (value is String) {
    final parseDimension = DimensionParser().parse;
    final spacingValues = value.split(' ');

    switch (spacingValues.length) {
      case 1:
        final space = parseDimension(spacingValues[0]);
        return 'EdgeInsets.all($space)';
      case 2:
        final vSpace = parseDimension(spacingValues[0]);
        final hSpace = parseDimension(spacingValues[1]);

        return 'EdgeInsets.symmetric(\n${indent()}vertical: $vSpace,\n${indent()}horizontal: $hSpace,\n${indent(-1)})';
      case 3:
        final top = parseDimension(spacingValues[0]);
        final hSpace = parseDimension(spacingValues[1]);
        final bottom = parseDimension(spacingValues[2]);

        return 'EdgeInsets.only(\n${indent()}top: $top,\n${indent()}right: $hSpace,\n${indent()}bottom: $bottom,\n${indent()}left: $hSpace,\n${indent(-1)})';
      case 4:
        final top = parseDimension(spacingValues[0]);
        final right = parseDimension(spacingValues[1]);
        final bottom = parseDimension(spacingValues[2]);
        final left = parseDimension(spacingValues[3]);

        return 'EdgeInsets.only(\n${indent()}top: $top,\n${indent()}right: $right,\n${indent()}bottom: $bottom,\n${indent()}left: $left,\n${indent(-1)})';
      default:
        throw Exception(
          'Cannot parse spacing since there are ${spacingValues.length} values. Please provide 1-4 values.',
        );
    }
  }

  throw Exception('Unable to parse spacing with data: $value');
}