build method

ScalableImage build({
  1. bool last = false,
  2. void warnF(
    1. String
    )?,
  3. Color? currentColor,
})

Create a ScalableImage from dom. The intended usage of this method is to create a ScalableImage after modifications have been made to dom, so that it can be displayed. Later, further modifications can be made, and a new ScalableImage can be produced by calling this method again. That new ScalableImage can replace the old one. This method produces the faster DAG (directed acyclic graph) representation of the ScalableImage, that is, not the compact representation.

When a subsequent ScalableImage is produced in this way, the new DAG will share nodes with the last one produced, wherever possible. Notably, Flutter Path objects will be shared wherever possible. This may allow for more efficient rendering, if those Path objects are cached, e.g. in the GPU hardware.

If last is set true, this is the last time this method may be called on this instance. Subsequent invocations will fail with a StateError. However, setting this true makes the build process consume less memory, and be faster.

If warnF is non-null, it will be called if the SVG asset contains unrecognized tags and/or tag attributes. If it is null, the default behavior is to print nothing.

currentColor sets ScalableImage.currentColor.

Implementation

ScalableImage build(
    {bool last = false, void Function(String)? warnF, Color? currentColor}) {
  if (_lastCall) {
    throw StateError('build was previously called with last true');
  }
  _lastCall = last;
  final warnArg = warnF ?? nullWarn;
  final SvgDOM svg;
  if (last) {
    svg = dom;
    // The client might have messed up the ID lookup table, and might
    // have introduced instance sharing on attributes that we modify
    // when stylesheets are applied.
    svg.resetIDLookup();
    SvgDOMNotExported.cloneAttributes(svg);
  } else {
    svg = SvgDOMNotExported.clone(dom); // Builds ID lookup
  }
  final b = SIDagBuilder(warn: warnArg, currentColor: currentColor);
  if (_lastDag != null) {
    final lastPaths = _lastPaths!;
    SvgDOMNotExported.visitPaths(dom, (Object pathKey) {
      final Path? p = lastPaths[pathKey];
      if (p != null) {
        b.paths[pathKey] = p;
      }
    });
    ScalableImageDagNotExported.addAllToDagger(_lastDag!, b.dagger);
  }
  SvgDOMNotExported.build(svg, b);
  final si = b.si;
  if (last) {
    _lastDag = null;
    _lastPaths = null;
  } else {
    _lastDag = si;
    _lastPaths = b.paths;
  }
  return si;
}