svgToRegionList method

Future<List<Region>> svgToRegionList(
  1. String svg
)

Implementation

Future<List<Region>> svgToRegionList(String svg) async {
  List<Region> regionList = [];

  XmlDocument document = XmlDocument.parse(svg);
  var paths = document.findAllElements('path');

  double width = 0;
  double height = 0;

  if (width == 0 || height == 0) {
    var svgs = document.findAllElements('svg');
    for (var element in svgs) {
      width = double.tryParse(element.getAttribute('width').toString()) ?? 0;
      height = double.tryParse(element.getAttribute('height').toString()) ?? 0;
    }
  }

  for (var element in paths) {
    String? partId = element.getAttribute('id')?.toString();
    String partPath = element.getAttribute('d').toString();
    String? title = element.getAttribute('title')?.toString();
    String? color = element.getAttribute('fill')?.toString();
    String? style = element.getAttribute('style')?.toString();

    element.getAttributeNode('d')?.getAttribute('fill').toColor(element.getAttributeNode('d')?.getAttribute('style'));
    final pathTitle = element.getAttributeNode('d')?.getAttribute('title');

    var region = Region(
        id: partId ?? 'nullId',
        path: ModelPath(
          path: parseSvgPath(partPath),
          title: pathTitle,
        ),
        color: color.toColor(style),
        pathString: partPath,
        title: title);
    sizeController.addBounds(region.path.path.getBounds());
    regionList.add(region);
  }

  if (width > 0 && height > 0) {
    sizeController.mapSize = Size(width, height);
  }
  return regionList;
}