parseFloatWithUnits method

dynamic parseFloatWithUnits(
  1. dynamic string
)

Implementation

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

  var theUnit = 'px';

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

  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);
}