|
1 | | -import 'package:flutter/services.dart'; |
2 | | - |
3 | | -/// Formata o valor do campo com a máscara de NCM: `XXXX.XX.XX`. |
4 | | -class NCMInputFormatter extends TextInputFormatter { |
5 | | - @override |
6 | | - TextEditingValue formatEditUpdate( |
7 | | - TextEditingValue oldValue, TextEditingValue newValue) { |
8 | | - // verifica o tamanho máximo do campo |
9 | | - if (newValue.text.length > 8) return oldValue; |
10 | | - |
11 | | - var posicaoCursor = newValue.selection.end; |
12 | | - var substrIndex = 0; |
13 | | - final valorFinal = StringBuffer(); |
14 | | - |
15 | | - if (newValue.text.length >= 5) { |
16 | | - valorFinal.write('${newValue.text.substring(0, substrIndex = 4)}.'); |
17 | | - if (newValue.selection.end >= 4) posicaoCursor++; |
18 | | - } |
19 | | - if (newValue.text.length >= 7) { |
20 | | - valorFinal.write('${newValue.text.substring(4, substrIndex = 6)}.'); |
21 | | - if (newValue.selection.end >= 6) posicaoCursor++; |
22 | | - } |
23 | | - |
24 | | - if (newValue.text.length >= substrIndex) { |
25 | | - valorFinal.write(newValue.text.substring(substrIndex)); |
26 | | - } |
27 | | - |
28 | | - return TextEditingValue( |
29 | | - text: valorFinal.toString(), |
30 | | - selection: TextSelection.collapsed(offset: posicaoCursor), |
31 | | - ); |
32 | | - } |
33 | | -} |
| 1 | +import 'package:flutter/services.dart'; |
| 2 | + |
| 3 | +/// Formata o valor do campo com a máscara de NCM: `XXXX.XX.XX`. |
| 4 | +class NCMInputFormatter extends TextInputFormatter { |
| 5 | + @override |
| 6 | + TextEditingValue formatEditUpdate( |
| 7 | + TextEditingValue oldValue, TextEditingValue newValue) { |
| 8 | + // verifica o tamanho máximo do campo |
| 9 | + if (newValue.text.length > 8) return oldValue; |
| 10 | + |
| 11 | + var posicaoCursor = newValue.selection.end; |
| 12 | + var substrIndex = 0; |
| 13 | + final valorFinal = StringBuffer(); |
| 14 | + |
| 15 | + if (newValue.text.length >= 5) { |
| 16 | + valorFinal.write('${newValue.text.substring(0, substrIndex = 4)}.'); |
| 17 | + if (newValue.selection.end >= 4) posicaoCursor++; |
| 18 | + } |
| 19 | + if (newValue.text.length >= 7) { |
| 20 | + valorFinal.write('${newValue.text.substring(4, substrIndex = 6)}.'); |
| 21 | + if (newValue.selection.end >= 6) posicaoCursor++; |
| 22 | + } |
| 23 | + |
| 24 | + if (newValue.text.length >= substrIndex) { |
| 25 | + valorFinal.write(newValue.text.substring(substrIndex)); |
| 26 | + } |
| 27 | + |
| 28 | + return TextEditingValue( |
| 29 | + text: valorFinal.toString(), |
| 30 | + selection: TextSelection.collapsed(offset: posicaoCursor), |
| 31 | + ); |
| 32 | + } |
| 33 | +} |
0 commit comments