buildLocalizedTextSpan static method
TextSpan
buildLocalizedTextSpan(
- dynamic value,
- TextStyle baseStyle, {
- OfferingPackages? offeringPackage,
- OfferingPackagesProduct? product,
- String? currentLocale,
- bool isRTL = false,
Converts a localization value (String or List) to a TextSpan with support for attributes like bold, italic, underline, strike.
Implementation
static TextSpan buildLocalizedTextSpan(
dynamic value,
TextStyle baseStyle, {
OfferingPackages? offeringPackage,
OfferingPackagesProduct? product,
String? currentLocale,
bool isRTL = false,
}) {
if (value is List) {
List<TextSpan> children = [];
for (var part in value) {
if (part == null) continue;
if (part is String) {
children.add(TextSpan(text: part, style: baseStyle));
} else if (part is Map<String, dynamic>) {
// Handle text with attributes
if (part.containsKey('text')) {
String text = part['text'] ?? '';
Map<String, dynamic> attrs = part['attributes'] ?? {};
TextStyle style = baseStyle;
if (attrs['bold'] == true) {
style = style.copyWith(fontWeight: FontWeight.bold);
}
if (attrs['italic'] == true) {
style = style.copyWith(fontStyle: FontStyle.italic);
}
// Combine underline and strike if both are true
if (attrs['underline'] == true && attrs['strike'] == true) {
style = style.copyWith(
decoration: TextDecoration.combine(
[TextDecoration.underline, TextDecoration.lineThrough]));
} else if (attrs['underline'] == true) {
style = style.copyWith(decoration: TextDecoration.underline);
} else if (attrs['strike'] == true) {
style = style.copyWith(decoration: TextDecoration.lineThrough);
}
children.add(TextSpan(text: text, style: style));
} else if (part.containsKey('tag')) {
// Handle dynamic tags (e.g., PROD_TITLE, PROD_PRICE, etc.)
final String? tag = part['tag'];
String tagText = '';
switch (tag) {
case "PROD_TITLE":
tagText = product?.name ?? 'N/A';
break;
case "PROD_PRICE":
final priceObject = product?.prices
?.firstWhereOrNull((p) => p.currency == 'IRR') ??
product?.prices
?.firstWhereOrNull((p) => p.currency == 'USD') ??
product?.prices?.firstOrNull;
final price = priceObject?.price?.toString() ?? 'N/A';
tagText = isRTL ? _formatPriceStatic(price, true) : price;
break;
case "PROD_CURRENCY":
final priceObject = product?.prices
?.firstWhereOrNull((p) => p.currency == 'IRR') ??
product?.prices
?.firstWhereOrNull((p) => p.currency == 'USD') ??
product?.prices?.firstOrNull;
final String currencyText = priceObject?.currency ?? 'N/A';
tagText =
isRTL && currencyText == 'IRR' ? 'تومان' : currencyText;
break;
case "PACK_DESCRIPTION":
tagText = offeringPackage?.description ?? 'N/A';
break;
default:
tagText = '[$tag]';
break;
}
children.add(TextSpan(text: tagText, style: baseStyle));
}
}
}
return TextSpan(children: children, style: baseStyle);
} else if (value is String) {
return TextSpan(text: value, style: baseStyle);
}
return TextSpan(text: '', style: baseStyle);
}