formatCpfCnpj static method

String formatCpfCnpj(
  1. int cpfCnpj
)

Recebe um inteiro que representa ou um CPF ou um CNPJ. Itentifica do que se trata com base na quantidade de dígitos.

Realiza tratamento preenchendo com zeros à esquerda quando faltar caracteres. CPF deve ter 11; Já CNPJ, 14.

Retorna o texto formatado.

Implementation

static String formatCpfCnpj(int cpfCnpj) {
  String strCpfCnpj = cpfCnpj.toString();

  if (strCpfCnpj.length > 11) {
    strCpfCnpj = strCpfCnpj.padLeft(14, '0');
    var cnpjController =
        MaskedTextController(text: strCpfCnpj, mask: '00.000.000/0000-00');
    return cnpjController.text;
  } else {
    strCpfCnpj = strCpfCnpj.padLeft(11, '0');
    var cpfController =
        MaskedTextController(text: strCpfCnpj, mask: '000.000.000-00');
    return cpfController.text;
  }
}