computeValue function

String computeValue(
  1. FormFieldModel field,
  2. dynamic value, {
  3. bool? useNumericFormatter = false,
  4. bool? allowDecimal = true,
  5. int? maxDecimals = 2,
})

Implementation

String computeValue(
  FormFieldModel field,
  dynamic value, {
  bool? useNumericFormatter = false,
  bool? allowDecimal = true,
  int? maxDecimals = 2,
}) {
  if (value == null) return '-';

  String? output;
  try {
    switch (field.type) {
      case 'date':
        if (value is DateTime) {
          output = value.formatLocalized(
            pattern: field.props?['displayFormat'] as String?,
          );
        } else if (value is String) {
          final date = DateTime.tryParse(value);
          output = date?.formatLocalized(
            pattern: field.props?['displayFormat'] as String?,
          );
        }
      case 'date_hijri':
        if (value is DateTime) {
          output = value.formatHijriLocalized(
            pattern: field.props?['displayFormat'] as String?,
          );
        } else if (value is String) {
          final date = DateTime.tryParse(value);
          output = date?.formatHijriLocalized(
            pattern: field.props?['displayFormat'] as String?,
          );
        }
      case 'radio' || 'dropdown' || 'switch':
        final options = field.props!['options'] as List;
        final selectedOption = options.firstWhere(
          (option) => (option as Map)['value'] == value,
          orElse: () => {'label': value, 'value': null},
        );
        output = (selectedOption as Map)['label'].toString();
      case 'checkboxGroup':
        if (value == null || (value is List && value.isEmpty)) {
          output = '-';
        } else {
          final options = field.props!['options'] as List;
          final selectedLabels = <String>[];
          for (final val in value as List) {
            final selectedOption = options.firstWhere(
              (option) => (option as Map)['value'] == val,
              orElse: () => {'label': val, 'value': null},
            );
            final label = getString(
              (selectedOption as Map)['label'].toString(),
            );
            selectedLabels.add(label);
          }
          output = selectedLabels.join(', ');
        }
      default:
        if (value is DateTime) {
          output = value.formatLocalized();
        }
        if (value is int || value is double) {
          final multiplier = field.props?['multiplier'] as double? ?? 1.0;
          final numValue = value is num ? value * multiplier : value;
          output = useNumericFormatter ?? false
              ? formatNumber(
                  num.parse('$numValue'),
                  allowDecimal: allowDecimal,
                  maxDecimals: maxDecimals,
                )
              : numValue.toString();
        }
        if (value is String) {
          output = value;
          if (useNumericFormatter ?? false) {
            output = formatNumericString(
              value,
              allowDecimal: allowDecimal,
              maxDecimals: maxDecimals,
            );
          }
        }
    }
  } catch (e) {
    output = value.toString();
  }
  return output ?? '-';
}