DynamicCardConfig.fromJson constructor

DynamicCardConfig.fromJson(
  1. Map<String, dynamic> json
)

Implementation

factory DynamicCardConfig.fromJson(Map<String, dynamic> json) {
  // Parse layout type
  var layoutType = CardLayout.basic;
  if (json['layout'] != null) {
    switch (json['layout']) {
      case 'header':
        layoutType = CardLayout.header;
      case 'headerWithActions':
        layoutType = CardLayout.headerWithActions;
      case 'grid':
        layoutType = CardLayout.grid;
      case 'tabbed':
        layoutType = CardLayout.tabbed;
      case 'twoColumn':
        layoutType = CardLayout.twoColumn;
      case 'custom':
        layoutType = CardLayout.custom;
      default:
        layoutType = CardLayout.basic;
    }
  }

  // Parse header
  CardHeader? header;
  if (json['header'] != null) {
    header = CardHeader.fromJson(json['header'] as Map<String, dynamic>);
  }

  // Parse actions
  List<CardAction>? actions;
  if (json['actions'] != null) {
    actions =
        (json['actions'] as List)
            .map(
              (action) => CardAction.fromJson(action as Map<String, dynamic>),
            )
            .toList();
  }

  // Parse content
  List<CardContentItem>? content;
  if (json['content'] != null) {
    content =
        (json['content'] as List)
            .map(
              (item) =>
                  CardContentItem.fromJson(item as Map<String, dynamic>),
            )
            .toList();
  }

  // Parse tabs
  List<CardTab>? tabs;
  if (json['tabs'] != null) {
    tabs =
        (json['tabs'] as List)
            .map((tab) => CardTab.fromJson(tab as Map<String, dynamic>))
            .toList();
  }

  // Parse columns
  List<CardContentItem>? leftColumn;
  if (json['leftColumn'] != null) {
    leftColumn =
        (json['leftColumn'] as List)
            .map(
              (item) =>
                  CardContentItem.fromJson(item as Map<String, dynamic>),
            )
            .toList();
  }

  List<CardContentItem>? rightColumn;
  if (json['rightColumn'] != null) {
    rightColumn =
        (json['rightColumn'] as List)
            .map(
              (item) =>
                  CardContentItem.fromJson(item as Map<String, dynamic>),
            )
            .toList();
  }

  return DynamicCardConfig(
    title: json['title'] as String?,
    header: header,
    actions: actions,
    content: content,
    tabs: tabs,
    leftColumn: leftColumn,
    rightColumn: rightColumn,
    leftColumnFlex: json['leftColumnFlex'] as int?,
    rightColumnFlex: json['rightColumnFlex'] as int?,
    gridColumns: json['gridColumns'] as int?,
    gridChildAspectRatio:
        json['gridChildAspectRatio'] != null
            ? double.tryParse(json['gridChildAspectRatio'].toString())
            : null,
    tabContentHeight:
        json['tabContentHeight'] != null
            ? double.tryParse(json['tabContentHeight'].toString())
            : null,
    elevation:
        json['elevation'] != null
            ? double.tryParse(json['elevation'].toString()) ?? 1.0
            : 1.0,
    borderRadius:
        json['borderRadius'] != null
            ? double.tryParse(json['borderRadius'].toString()) ?? 8.0
            : 8.0,
    borderWidth:
        json['borderWidth'] != null
            ? double.tryParse(json['borderWidth'].toString()) ?? 1.0
            : 1.0,
    backgroundColor:
        json['backgroundColor'] != null
            ? Color(int.parse(json['backgroundColor'].toString()))
            : Colors.white,
    layout: layoutType,
  );
}