expressionToTextShadow static method

List<Shadow> expressionToTextShadow(
  1. List<Expression> value
)

Implementation

static List<Shadow> expressionToTextShadow(List<css.Expression> value) {
  List<Shadow> shadow = [];
  List<int> indices = [];
  List<List<css.Expression>> valueList = [];
  for (css.Expression e in value) {
    if (e is css.OperatorComma) {
      indices.add(value.indexOf(e));
    }
  }
  indices.add(value.length);
  int previousIndex = 0;
  for (int i in indices) {
    valueList.add(value.sublist(previousIndex, i));
    previousIndex = i + 1;
  }
  for (List<css.Expression> list in valueList) {
    css.Expression? offsetX;
    css.Expression? offsetY;
    css.Expression? blurRadius;
    css.Expression? color;
    int expressionIndex = 0;
    for (var element in list) {
      if (element is css.HexColorTerm || element is css.FunctionTerm) {
        color = element;
      } else if (expressionIndex == 0) {
        offsetX = element;
        expressionIndex++;
      } else if (expressionIndex++ == 1) {
        offsetY = element;
        expressionIndex++;
      } else {
        blurRadius = element;
      }
    }
    RegExp nonNumberRegex = RegExp(r'\s+(\d+\.\d+)\s+');
    if (offsetX is css.LiteralTerm && offsetY is css.LiteralTerm) {
      if (color != null && ExpressionMapping.expressionToColor(color) != null) {
        shadow.add(Shadow(
            color: expressionToColor(color)!,
            offset: Offset(
                double.tryParse((offsetX).text.replaceAll(nonNumberRegex, ''))!,
                double.tryParse((offsetY).text.replaceAll(nonNumberRegex, ''))!),
            blurRadius: (blurRadius is css.LiteralTerm) ? double.tryParse((blurRadius).text.replaceAll(nonNumberRegex, ''))! : 0.0,
        ));
      } else {
        shadow.add(Shadow(
            offset: Offset(
                double.tryParse((offsetX).text.replaceAll(nonNumberRegex, ''))!,
                double.tryParse((offsetY).text.replaceAll(nonNumberRegex, ''))!),
            blurRadius: (blurRadius is css.LiteralTerm) ? double.tryParse((blurRadius).text.replaceAll(nonNumberRegex, ''))! : 0.0,
        ));
      }
    }
  }
  List<Shadow> finalShadows = shadow.toSet().toList();
  return finalShadows;
}