fromJson static method

SpotlightStep? fromJson(
  1. Map<String, dynamic> json, {
  2. DesignTokenCatalog designTokens = DesignTokenCatalog.empty,
})

Implementation

static SpotlightStep? fromJson(
  Map<String, dynamic> json, {
  DesignTokenCatalog designTokens = DesignTokenCatalog.empty,
}) {
  final canvas = _guideCanvas(json, designTokens);
  if (canvas == null) return null;

  final target = optMap(json, 'target');
  var anchorlessTarget = target?['type'] == 'anchorless' ? target : null;
  final targetVersion = anchorlessTarget?['version'];
  if (targetVersion is int && targetVersion < 1) {
    throw FormatException('Old anchorless target version $targetVersion');
  }
  if (targetVersion is int && targetVersion > 1) {
    _log.e('Dropped — unsupported anchorless target version $targetVersion',
        stage: TimelineStage.parse,
        reason: TimelineReason.schemaVersionTooNew,
        extras: {'required': '$targetVersion', 'supported': '1'});
    throw const ReportedParseFailure();
  }
  if (anchorlessTarget != null && anchorlessTarget.containsKey('variants')) {
    final platform = switch (defaultTargetPlatform) {
      TargetPlatform.android => 'android',
      TargetPlatform.iOS => 'ios',
      _ => null,
    };
    final variants = optMap(anchorlessTarget, 'variants');
    final variant = platform == null || variants == null
        ? null
        : optMap(variants, platform);
    if (variant == null || variant['devicePlatform'] != platform) {
      return null;
    }
    anchorlessTarget = {...variant, 'type': 'anchorless', 'version': 1};
  }
  final configuredAnchorKey = optString(json, 'anchorKey').trim();
  final anchorKey = configuredAnchorKey.isEmpty ? null : configuredAnchorKey;
  if ((anchorKey == null) == (anchorlessTarget == null)) return null;
  final stepId = _optStringOrNull(json, 'stepId');
  if (anchorlessTarget != null && stepId == null) return null;
  return SpotlightStep(
    stepId: stepId,
    anchorKey: anchorKey,
    anchorlessTarget: anchorlessTarget,
    delayInMs:
        json.containsKey('delayInMs') ? optInt(json, 'delayInMs', 0) : null,
    calloutPosition: optString(json, 'calloutPosition', 'below'),
    calloutGap: optDouble(json, 'calloutGap', 8),
    overlayColor: colorOr(
      optString(json, 'overlayColor'),
      const Color(0xFF000000),
    ),
    overlayOpacity: optDouble(json, 'overlayOpacity', 0.7),
    highlightShape: optString(json, 'highlightShape', 'rect'),
    highlightCornerRadius: optDouble(json, 'highlightCornerRadius', 8),
    highlightPadding: optDouble(json, 'highlightPadding', 8),
    highlightGlowColor: colorOr(
      optString(json, 'highlightGlowColor'),
      const Color(0x00000000),
    ),
    highlightGlowWidth: optDouble(json, 'highlightGlowWidth', 0),
    calloutCornerRadius: optDouble(json, 'calloutCornerRadius', 8),
    calloutPadding: optDouble(json, 'calloutPadding', 12),
    calloutShadow: optBool(json, 'calloutShadow', true),
    calloutBorderColor: colorOr(
      optString(json, 'calloutBorderColor'),
      const Color(0x00000000),
    ),
    calloutBorderWidth: optDouble(json, 'calloutBorderWidth', 0),
    showArrow: optBool(json, 'showArrow', true),
    arrowSize: optDouble(json, 'arrowSize', 8),
    canvas: canvas,
  );
}