parse method
Implementation
CampaignCanvas parse(
Map<String, dynamic> json, {
Set<String> allowedWidgetTypes = supportedCampaignCanvasWidgetTypes,
}) {
final version = optInt(json, 'version', 1);
if (version != 2) {
throw FormatException('Unsupported canvas version $version');
}
final width = _propertyDouble(json, 'canvasWidth', 360);
final height = _propertyDouble(json, 'canvasHeight', 420);
final canvasWidth = width > 0 ? width : 360.0;
final canvasHeight = height > 0 ? height : 420.0;
final children = <CampaignCanvasChild>[];
for (final raw in optList(json, 'children') ?? const []) {
if (raw is! Map) continue;
final child = raw.cast<String, dynamic>();
final rectJson = optMap(child, 'rect');
if (rectJson == null) continue;
final rawX = _propertyDouble(rectJson, 'x', 0);
final rawY = _propertyDouble(rectJson, 'y', 0);
final rawWidth = _propertyDouble(rectJson, 'width', 0);
final rawHeight = _propertyDouble(rectJson, 'height', 0);
final rect = CampaignCanvasRect(
rawX * canvasWidth,
rawY * canvasHeight,
rawWidth * canvasWidth,
rawHeight * canvasHeight,
);
final id = optString(child, 'id');
switch (optString(child, 'kind')) {
case 'tapRegion':
final actions =
const EngageActionParser().parse(optMap(child, 'onClick'));
if (actions.isNotEmpty) {
children.add(
CampaignCanvasTapRegionChild(
id,
rect,
actions,
isPrimary: optBool(child, 'isPrimary', false),
),
);
}
case 'widget':
final widgetJson = optMap(child, 'widget');
if (widgetJson == null) continue;
final type = optString(widgetJson, 'type');
if (!allowedWidgetTypes.contains(type)) continue;
final widget = _widget(type, widgetJson);
if (widget != null) {
children.add(CampaignCanvasWidgetChild(id, rect, widget));
}
}
}
return CampaignCanvas(
version: version,
width: canvasWidth,
height: canvasHeight,
background: _background(optMap(json, 'background')),
children: children,
);
}