Closed rfschubert closed 5 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!
Sorry for reopening the issue, but I tried to use
updateMask()
insideonChanged()
, 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'); } }
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.
@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.
with easy_mask simple
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) ], ), ),
lock exemples
THANK YOU!!!
@clerdson thankss!!!
I want to use CPF/CNPJ mask.
So if user input
123.456.789-10
will mask asCPF
and if it continues to input, will change toCNPJ
mask... there is a way?