tryParse static method

NexusInAppBlock? tryParse(
  1. Map<String, dynamic> j
)

Implementation

static NexusInAppBlock? tryParse(Map<String, dynamic> j) {
  final id = (j['id'] ?? '').toString();
  switch (j['type']) {
    case 'image':
      final url = j['url']?.toString() ?? '';
      if (url.isEmpty) return null;
      final tap = j['tap'] is Map ? _map(j['tap']) : null;
      return NexusInAppImageBlock(
        id,
        url: url,
        aspectRatio: switch (j['aspect']) {
          '16:9' => 16 / 9,
          '4:3' => 4 / 3,
          '1:1' => 1.0,
          '3:4' => 3 / 4,
          _ => null,
        },
        contain: j['fit'] == 'contain',
        radius: _num(j['radius'], 0),
        bleed: j['bleed'] == true,
        tap: tap == null
            ? null
            : NexusInAppTap(action: _str(tap['action'], 'dismiss'), url: tap['url']?.toString(), event: tap['event']?.toString()),
      );
    case 'badge':
      return NexusInAppBadgeBlock(
        id,
        text: _str(j['text'], ''),
        background: parseHex(j['bg']) ?? const Color(0xFFEEF2FF),
        foreground: parseHex(j['fg']) ?? const Color(0xFF4338CA),
        align: _str(j['align'], 'start'),
      );
    case 'heading':
      return NexusInAppHeadingBlock(
        id,
        text: _str(j['text'], ''),
        size: _num(j['size'], 22),
        weight: _num(j['weight'], 700).round(),
        color: parseHex(j['color']) ?? const Color(0xFF111827),
        align: _str(j['align'], 'start'),
      );
    case 'text':
      return NexusInAppTextBlock(
        id,
        text: _str(j['text'], ''),
        size: _num(j['size'], 15),
        color: parseHex(j['color']) ?? const Color(0xFF4B5563),
        align: _str(j['align'], 'start'),
        lineHeight: _num(j['lineHeight'], 1.45),
      );
    case 'buttons':
      final items = (j['items'] as List?)
              ?.whereType<Map>()
              .map((i) => NexusInAppDesignButton._parse(i.cast<String, dynamic>()))
              .toList() ??
          const <NexusInAppDesignButton>[];
      if (items.isEmpty) return null;
      return NexusInAppButtonsBlock(id, row: j['layout'] == 'row', items: items);
    case 'carousel':
      final cards = (j['cards'] as List?)
              ?.whereType<Map>()
              .map((c) => NexusInAppCarouselCard._parse(c.cast<String, dynamic>()))
              .toList() ??
          const <NexusInAppCarouselCard>[];
      if (cards.isEmpty) return null;
      final ind = _map(j['indicator']);
      return NexusInAppCarouselBlock(
        id,
        cards: cards,
        aspectRatio: switch (j['aspect']) {
          '16:9' => 16 / 9,
          '1:1' => 1.0,
          '3:4' => 3 / 4,
          _ => 4 / 3,
        },
        contain: j['fit'] == 'contain',
        peek: j['peek'] != false,
        bleed: j['bleed'] == true,
        radius: _num(j['radius'], 14),
        gap: _num(j['gap'], 10),
        padding: _num(j['padding'], 12),
        titleSize: _num(j['titleSize'], 17),
        bodySize: _num(j['bodySize'], 14),
        showIndicator: ind['show'] != false,
        indicatorColor: parseHex(ind['color']) ?? const Color(0xFF4F46E5),
        autoplaySeconds: _num(j['autoplay'], 0).round(),
      );
    case 'spacer':
      return NexusInAppSpacerBlock(id, height: _num(j['height'], 16));
    case 'divider':
      return NexusInAppDividerBlock(id, color: parseHex(j['color']) ?? const Color(0xFFE5E7EB), thickness: _num(j['thickness'], 1));
    default:
      return null;
  }
}