bhrott / flutter-masked-text

A masked text for Flutter.
https://pub.dartlang.org/packages/flutter_masked_text
MIT License
276 stars 124 forks source link

Is possible to use 2 masks? #27

Closed rfschubert closed 5 years ago

rfschubert commented 5 years ago

I want to use CPF/CNPJ mask.

So if user input 123.456.789-10 will mask as CPF and if it continues to input, will change to CNPJ mask... there is a way?

bhrott commented 5 years ago

Hey man!

Docs: https://github.com/benhurott/flutter-masked-text#change-the-mask-in-runtime

waister commented 4 years ago

Sorry for reopening the issue, but I tried to use updateMask() inside onChanged(), but it didn't work. My current code:

onChanged: (value) { if (value.length > 11) { _controllerDocument.updateMask('00.000.000/0000-00'); } else { _controllerDocument.updateMask('000.000.000-00'); } }

Thanks!

deepak786 commented 4 years ago

Sorry for reopening the issue, but I tried to use updateMask() inside onChanged(), but it didn't work. My current code:

onChanged: (value) { if (value.length > 11) { _controllerDocument.updateMask('00.000.000/0000-00'); } else { _controllerDocument.updateMask('000.000.000-00'); } }

Thanks!

The value inside the onChanged contains the . - and / as well. So you need to check onChanged: (value) { if (value.length > 14) { _controllerDocument.updateMask('00.000.000/0000-00'); } else { _controllerDocument.updateMask('000.000.000-00'); } }

waister commented 4 years ago

No, the value inside onChanged comes only from the numbers, the problem is in the updateMask, which cancels the last number entered after changing the mask.

deepak786 commented 4 years ago

@waister try the below code

  // initialize your _controllerDocument with cpf fromat
  _controllerDocument = MaskedTextController(mask: '000.000.000-00');

  // add before change listener
  _controllerDocument.beforeChange = (String previous, String next) {
    if (next.length > 14) {
      // change to CNPJ format
      if(_controllerDocument.mask != '00.000.000/0000-00')
         _controllerDocument.updateMask('00.000.000/0000-00');
    } else {
      // change to CPF format
      if (_controllerDocument.mask != '000.000.000-00')
          _controllerDocument.updateMask('000.000.000-00');
    }
    return true;
  };

Let me know if it works or not. Working fine for me.

clerdson commented 3 years ago

with easy_mask simple

clerdson commented 3 years ago

Container( padding: EdgeInsets.symmetric(horizontal: 60), child: TextField( decoration: InputDecoration(hintText: 'Multi Mask CPF/CNPJ'), inputFormatters: [ TextInputMask( mask: ['999.999.999-99', '99.999.999/9999-99'], reverse: false) ], ), ),

clerdson commented 3 years ago

lock exemples

victorruandev commented 2 years ago

THANK YOU!!!

alefealvessilva commented 1 year ago

@clerdson thankss!!!