obterTelefone static method

String obterTelefone(
  1. String telefone, {
  2. bool ddd = true,
  3. bool mascara = true,
})

Retorna o telefone informado, utilizando a máscara: (00) 11111-2222

Ajusta-se automaticamente para celular e fixo.

Para retornar apenas os números, informe mascara=false.

Implementation

static String obterTelefone(String telefone,
    {bool ddd = true, bool mascara = true}) {
  assert((telefone.length <= 15),
      'Telefone com tamanho inválido. Deve conter 10 ou 11 caracteres');
  if (!mascara) return UtilBrasilFields.removeCaracteres(telefone);

  if (ddd) {
    assert((telefone.length == 10 || telefone.length == 11),
        'Telefone com tamanho inválido. Deve conter 10 ou 11 caracteres');

    return telefone.length == 10
        ? '(${telefone.substring(0, 2)}) ${telefone.substring(2, 6)}-${telefone.substring(6, 10)}'
        : '(${telefone.substring(0, 2)}) ${telefone.substring(2, 7)}-${telefone.substring(7, 11)}';
  } else {
    assert((telefone.length == 8 || telefone.length == 9),
        'Telefone com tamanho inválido. Deve conter 8 ou 9 caracteres');

    return (telefone.length == 8)
        ? '${telefone.substring(0, 4)}-${telefone.substring(4, 8)}'
        : '${telefone.substring(0, 5)}-${telefone.substring(5, 9)}';
  }
}