fromString static method

List<XmlAttribute> fromString(
  1. String? transform
)

Converts a transform value into a list of attributes that can be added to a group element.

eg:

TransformConverter.fromString('rotate(-10) translate(10 10)');

Implementation

static List<XmlAttribute> fromString(String? transform) {
  if (transform == null) return [];

  final attributes = <XmlAttribute>[];

  final transformRegExp = RegExp(r'((\w|\s)+)\(([^)]+)', multiLine: true);
  var startIndex = 0;
  var match = transformRegExp.allMatches(transform, startIndex);

  while (match.isNotEmpty) {
    startIndex = match.first.end;
    final entriesRegExp = RegExp(r'[,\s]+');
    final split = match.first.group(3)?.split(entriesRegExp);
    final transformName = match.first.group(1)?.trim();

    if (transformName == 'translate') {
      final x = _getElementAt(split, 0);
      final y = _getElementAt(split, 1);

      _addIfValid(attributes, x, _translate, AttributeName.androidTranslateX);
      _addIfValid(attributes, y, _translate, AttributeName.androidTranslateY);
    } else if (transformName == 'scale') {
      final x = _getElementAt(split, 0);
      final y = _getElementAt(split, 1);

      _addIfValid(attributes, x, _scale, AttributeName.androidScaleX);
      _addIfValid(attributes, y, _scale, AttributeName.androidScaleY);
    } else if (transformName == 'rotate') {
      final r = _getElementAt(split, 0);
      final x = _getElementAt(split, 1);
      final y = _getElementAt(split, 2);

      _addIfValid(attributes, r, _rotate, AttributeName.androidRotation);
      _addIfValid(attributes, x, _pivot, AttributeName.androidPivotX);
      _addIfValid(attributes, y, _pivot, AttributeName.androidPivotY);
    }

    match = transformRegExp.allMatches(transform, startIndex);
  }

  return attributes;
}