DrawPath.fromJson constructor
DrawPath.fromJson(})
Implementation
factory DrawPath.fromJson(
Map<String, dynamic> data, {
OperationStep Function(
String type, Map<String, dynamic> jsonStepMap, Path genPath)?
stepFactory,
}) {
final List<OperationStep> steps = <OperationStep>[];
final List<dynamic> jsonSteps = data['steps'] as List<dynamic>;
final Path genPath = Path();
for (final dynamic jsonStep in jsonSteps) {
final Map<String, dynamic> jsonStepMap = jsonStep as Map<String, dynamic>;
final String type = jsonStepMap['type'] as String;
switch (type) {
case 'arcTo':
final ArcTo arcTo = ArcTo.fromJson(jsonStepMap);
steps.add(arcTo);
genPath.arcTo(arcTo.rect, arcTo.startAngle, arcTo.sweepAngle,
arcTo.forceMoveTo);
case 'arcToPoint':
// steps.add(ArcToPoint.fromJson(jsonStepMap));
final ArcToPoint arcToPoint = ArcToPoint.fromJson(jsonStepMap);
steps.add(arcToPoint);
genPath.arcToPoint(
arcToPoint.arcEnd,
radius: arcToPoint.radius,
rotation: arcToPoint.rotation,
largeArc: arcToPoint.largeArc,
clockwise: arcToPoint.clockwise,
);
case 'conicTo':
// steps.add(ConicTo.fromJson(jsonStepMap));
final ConicTo conicTo = ConicTo.fromJson(jsonStepMap);
steps.add(conicTo);
genPath.conicTo(
conicTo.x1, conicTo.y1, conicTo.x2, conicTo.y2, conicTo.w);
case 'cubicTo':
// steps.add(CubicTo.fromJson(jsonStepMap));
final CubicTo cubicTo = CubicTo.fromJson(jsonStepMap);
steps.add(cubicTo);
genPath.cubicTo(cubicTo.x1, cubicTo.y1, cubicTo.x2, cubicTo.y2,
cubicTo.x3, cubicTo.y3);
case 'lineTo':
// steps.add(LineTo.fromJson(jsonStepMap));
final LineTo lineTo = LineTo.fromJson(jsonStepMap);
steps.add(lineTo);
genPath.lineTo(lineTo.x, lineTo.y);
case 'moveTo':
// steps.add(MoveTo.fromJson(jsonStepMap));
final MoveTo moveTo = MoveTo.fromJson(jsonStepMap);
steps.add(moveTo);
genPath.moveTo(moveTo.x, moveTo.y);
case 'close':
// steps.add(PathClose.fromJson(jsonStepMap));
final PathClose pathClose = PathClose.fromJson(jsonStepMap);
steps.add(pathClose);
genPath.close();
case 'shift':
// steps.add(PathShift.fromJson(jsonStepMap));
final PathShift pathShift = PathShift.fromJson(jsonStepMap);
steps.add(pathShift);
genPath.shift(pathShift.offset);
case 'quadraticBezierTo':
// steps.add(QuadraticBezierTo.fromJson(jsonStepMap));
final QuadraticBezierTo quadraticBezierTo =
QuadraticBezierTo.fromJson(jsonStepMap);
steps.add(quadraticBezierTo);
genPath.quadraticBezierTo(
quadraticBezierTo.x1,
quadraticBezierTo.y1,
quadraticBezierTo.x2,
quadraticBezierTo.y2,
);
case 'relativeArcToPoint':
// steps.add(RelativeArcToPoint.fromJson(jsonStepMap));
final RelativeArcToPoint relativeArcToPoint =
RelativeArcToPoint.fromJson(jsonStepMap);
steps.add(relativeArcToPoint);
genPath.relativeArcToPoint(
relativeArcToPoint.arcEndDelta,
radius: relativeArcToPoint.radius,
rotation: relativeArcToPoint.rotation,
largeArc: relativeArcToPoint.largeArc,
clockwise: relativeArcToPoint.clockwise,
);
case 'relativeConicTo':
// steps.add(RelativeConicTo.fromJson(jsonStepMap));
final RelativeConicTo relativeConicTo =
RelativeConicTo.fromJson(jsonStepMap);
steps.add(relativeConicTo);
genPath.relativeConicTo(
relativeConicTo.x1,
relativeConicTo.y1,
relativeConicTo.x2,
relativeConicTo.y2,
relativeConicTo.w,
);
case 'relativeCubicTo':
// steps.add(RelativeCubicTo.fromJson(jsonStepMap));
final RelativeCubicTo relativeCubicTo =
RelativeCubicTo.fromJson(jsonStepMap);
steps.add(relativeCubicTo);
genPath.relativeCubicTo(
relativeCubicTo.x1,
relativeCubicTo.y1,
relativeCubicTo.x2,
relativeCubicTo.y2,
relativeCubicTo.x3,
relativeCubicTo.y3,
);
case 'relativeLineTo':
// steps.add(RelativeLineTo.fromJson(jsonStepMap));
final RelativeLineTo relativeLineTo =
RelativeLineTo.fromJson(jsonStepMap);
steps.add(relativeLineTo);
genPath.relativeLineTo(relativeLineTo.dx, relativeLineTo.dy);
case 'relativeMoveTo':
// steps.add(RelativeMoveTo.fromJson(jsonStepMap));
final RelativeMoveTo relativeMoveTo =
RelativeMoveTo.fromJson(jsonStepMap);
steps.add(relativeMoveTo);
genPath.relativeMoveTo(relativeMoveTo.dx, relativeMoveTo.dy);
case 'relativeQuadraticBezierTo':
// steps.add(RelativeQuadraticBezierTo.fromJson(jsonStepMap));
final RelativeQuadraticBezierTo relativeQuadraticBezierTo =
RelativeQuadraticBezierTo.fromJson(jsonStepMap);
steps.add(relativeQuadraticBezierTo);
genPath.relativeQuadraticBezierTo(
relativeQuadraticBezierTo.x1,
relativeQuadraticBezierTo.y1,
relativeQuadraticBezierTo.x2,
relativeQuadraticBezierTo.y2,
);
default:
final OperationStep? step =
stepFactory?.call(type, jsonStepMap, genPath);
if (step != null) {
steps.add(step);
} else {
throw Exception('Unknown operation step type: $type');
}
}
}
return DrawPath(
steps: steps,
type: data['fillType'] == null
? null
: PathFillType.values[data['fillType'] as int],
path: genPath,
);
}