getFormatter static method

String getFormatter(
  1. dynamic value,
  2. dynamic type
)

Implementation

static String getFormatter(value, type) {
  if (type == gDate) {
    String nums = value.toString().replaceAll(RegExp(r'[\D]'), '');
    String sSeg = '-';
    List listFormat = [];
    listFormat.add({
      'locFrom': 0,
      'locTo': 4,
      'type': 'i',
      'min': 1900,
      'max': 3000,
      'value:': ''
    });
    listFormat.add({
      'locFrom': 4,
      'locTo': 6,
      'type': 'm',
      'min': 1,
      'max': 12,
      'value:': ''
    });
    listFormat.add({
      'locFrom': 6,
      'locTo': 8,
      'type': 'd',
      'min': 1,
      'max': 31,
      'value:': ''
    });
    String result = '';

    if (nums.length < 5) {
      return nums;
    }
    if (nums.length == 5) {
      String lastChar = nums.characters.last;
      result = nums.substring(0, 4) + sSeg;
      if (int.parse(lastChar) > 1) {
        result = '${result}0';
      }
      result = result + lastChar;
      return result;
    }
    if (nums.length == 6) {
      result = nums.substring(0, 4) + sSeg;
      int iValue = int.parse(nums.substring(4, 6));
      if (iValue > 12 || iValue < 1) {
        return nums.substring(0, 4);
      }
      result = result + nums.substring(4, 6);
      return result;
    }
    if (nums.length == 7) {
      String lastChar = nums.characters.last;
      result = nums.substring(0, 4) + sSeg + nums.substring(4, 6) + sSeg;
      if (int.parse(lastChar) > 3) {
        result += '0';
      }
      result = result + lastChar;
      return result;
    }
    if (nums.length > 7) {
      result = nums.substring(0, 4) + sSeg + nums.substring(4, 6);
      int iValue = int.parse(nums.substring(6, 8));
      if (iValue > 31 || iValue < 1) {
        return result;
      }
      if (iValue > 28) {
        int month = int.parse(nums.substring(4, 6));
        if (month == 2) {
          int year = int.parse(nums.substring(0, 4));
          if (year % 4 > 0) {
            return result;
          }
        }
        if (iValue == 31 &&
            (month == 4 || month == 6 || month == 9 || month == 11)) {
          return result;
        }
      }
      result = result + sSeg + nums.substring(6, 8);
      return result;
    }
    return nums;
  } else if (type == gPhone) {
    String nums = value.toString().replaceAll(RegExp(r'[\D]'), '');
    String internationalPhoneFormatted = nums.isNotEmpty
        ? (nums.isNotEmpty ? '(' : '') +
            nums.substring(0, nums.length >= 3 ? 3 : null) +
            (nums.length > 3 ? ')' : '') +
            (nums.length > 3
                ? nums.substring(3, nums.length >= 6 ? 6 : null) +
                    (nums.length > 6
                        ? '-${nums.substring(6, nums.length >= 10 ? 10 : null)}'
                        : '')
                : '')
        : nums;
    return internationalPhoneFormatted;
  } else if (type == '$gEmail,$gPhone') {
    String valueString = value.toString();
    String nums = valueString.replaceAll(RegExp(r'[\D]'), '');
    int phonelength = nums.length;
    if (valueString.startsWith('(')) {
      phonelength++;
    }
    if (valueString.length > 4 && valueString.substring(4, 5) == ')') {
      phonelength++;
    }
    if (valueString.length > 8 && valueString.substring(8, 9) == '-') {
      phonelength++;
    }

    String internationalEmailPhoneFormatted = nums.isNotEmpty
        ? (nums.isNotEmpty ? '(' : '') +
            nums.substring(0, nums.length >= 3 ? 3 : null) +
            (nums.length > 3 ? ')' : '') +
            (nums.length > 3
                ? nums.substring(3, nums.length >= 6 ? 6 : null) +
                    (nums.length > 6
                        ? '-${nums.substring(6, nums.length >= 10 ? 10 : null)}'
                        : '')
                : '')
        : nums;
    if (valueString.length == phonelength) {
      return internationalEmailPhoneFormatted;
    }
  } else if (type == gMoney || type == gTip) {
    String nums = value.toString().replaceAll(RegExp(r'[\D]'), '');
    String internationalPhoneFormatted = toMoney(nums);
    return internationalPhoneFormatted;
  }
  return value;
}