parseFloatWithUnits method

dynamic parseFloatWithUnits(
  1. dynamic string
)

Implementation

parseFloatWithUnits(string) {
  if (string == null) {
    return null;
  }

  var theUnit = 'px';

  // print("SvgLoader.parseFloatWithUnits ${string}  ");

  if (string is String) {
    for (var i = 0, n = units.length; i < n; i++) {
      var u = units[i];

      if (string.endsWith(u)) {
        theUnit = u;
        string = string.substring(0, string.length - u.length);
        break;
      }
    }
  }

  num scale;

  if (theUnit == 'px' && defaultUnit != 'px') {
    // Conversion scale from  pixels to inches, then to default units

    scale = unitConversion["in"]![defaultUnit]! / defaultDPI;
  } else {
    scale = unitConversion[theUnit]![defaultUnit]!;

    if (scale < 0) {
      // Conversion scale to pixels

      scale = unitConversion[theUnit]!['in']! * defaultDPI;
    }
  }

  String str = "$string";
  // if(_str.startsWith("-.")) {
  //   _str = _str.replaceFirst("-.", "-0.");
  // }

  List<String> strs = str.split(".");

  if (strs.length >= 3) {
    strs = strs.sublist(0, 2);

    str = strs.join(".");
  }

  // print(" string: ${_str} ");

  return scale * num.parse(str);
}