CardContentItem.fromJson constructor
CardContentItem.fromJson(
- Map<String, dynamic> json
)
Implementation
factory CardContentItem.fromJson(Map<String, dynamic> json) {
var contentType = ContentType.text;
if (json['type'] != null) {
switch (json['type']) {
case 'richText':
contentType = ContentType.richText;
case 'image':
contentType = ContentType.image;
case 'button':
contentType = ContentType.button;
case 'icon':
contentType = ContentType.icon;
case 'chart':
contentType = ContentType.chart;
case 'divider':
contentType = ContentType.divider;
case 'list':
contentType = ContentType.list;
case 'custom':
contentType = ContentType.custom;
default:
contentType = ContentType.text;
}
}
IconData? iconData;
if (json['icon'] != null) {
// You'd need to implement a method to convert string to IconData
iconData = Icons.info; // Default icon
}
List<CardContentItem>? listItems;
if (json['listItems'] != null) {
listItems =
(json['listItems'] as List)
.map(
(item) =>
CardContentItem.fromJson(item as Map<String, dynamic>),
)
.toList();
}
return CardContentItem(
type: contentType,
text: json['text'] as String?,
imageUrl: json['imageUrl'] as String?,
imageHeight:
json['imageHeight'] != null
? double.tryParse(json['imageHeight'].toString())
: null,
imageWidth:
json['imageWidth'] != null
? double.tryParse(json['imageWidth'].toString())
: null,
imageRadius:
json['imageRadius'] != null
? double.tryParse(json['imageRadius'].toString())
: null,
actionId: json['actionId'] as String?,
actionData: json['actionData'],
icon: iconData,
iconColor:
json['iconColor'] != null
? Color(int.parse(json['iconColor'].toString()))
: null,
iconSize:
json['iconSize'] != null
? double.tryParse(json['iconSize'].toString())
: null,
dividerColor:
json['dividerColor'] != null
? Color(int.parse(json['dividerColor'].toString()))
: null,
dividerThickness:
json['dividerThickness'] != null
? double.tryParse(json['dividerThickness'].toString())
: null,
dividerHeight:
json['dividerHeight'] != null
? double.tryParse(json['dividerHeight'].toString())
: null,
listItems: listItems,
padding:
json['padding'] != null
? EdgeInsets.all(double.tryParse(json['padding'].toString()) ?? 0)
: null,
);
}