如何在Flutter的TextField中添加遮罩?

Vic*_*que 5 masking textfield dart flutter

我尝试将日期掩码添加到textField中,因为我不喜欢日期选择器,因为例如对于出生日期,它并不那么灵活。之后,从字符串转换为日期时间,我相信我可以继续进行该项目,谢谢。

static final TextEditingController _birthDate = new TextEditingController();
    new TextFormField( 
            controller: _birthDate, 
            maxLength: 10,
            keyboardType: TextInputType.datetime, 
            validator: _validateDate
        ), String _validateDate(String value) { 
    if(value.isEmpty)
        return null;
    if(value.length != 10)
        return 'Enter date in DD / MM / YYYY format';
    return null; 
}
Run Code Online (Sandbox Code Playgroud)

Vic*_*que 8

我修改了一些东西并设法获得了预期的结果。

我创建了这个类来定义变量

static final _UsNumberTextInputFormatter _birthDate = new _UsNumberTextInputFormatter();

class _UsNumberTextInputFormatter extends TextInputFormatter {
  @override
  TextEditingValue formatEditUpdate(
TextEditingValue oldValue,
TextEditingValue newValue  ) {
final int newTextLength = newValue.text.length;
int selectionIndex = newValue.selection.end;
int usedSubstringIndex = 0;
final StringBuffer newText = new StringBuffer();
if (newTextLength >= 3) {
  newText.write(newValue.text.substring(0, usedSubstringIndex = 2) + '/');
  if (newValue.selection.end >= 2)
    selectionIndex ++;
}
if (newTextLength >= 5) {
  newText.write(newValue.text.substring(2, usedSubstringIndex = 4) + '/');
  if (newValue.selection.end >= 4)
    selectionIndex++;
}
if (newTextLength >= 9) {
  newText.write(newValue.text.substring(4, usedSubstringIndex = 8));
  if (newValue.selection.end >= 8)
    selectionIndex++;
}
// Dump the rest.
if (newTextLength >= usedSubstringIndex)
  newText.write(newValue.text.substring(usedSubstringIndex));
return new TextEditingValue(
  text: newText.toString(),
  selection: new TextSelection.collapsed(offset: selectionIndex),
); 
} 
}
Run Code Online (Sandbox Code Playgroud)

最后我在文本字段中添加了一个输入格式

new TextFormField( 
          maxLength: 10,
          keyboardType: TextInputType.datetime, 
          validator: _validateDate,
          decoration: const InputDecoration(
            hintText: 'Digite sua data de nascimento',
            labelText: 'Data de Nascimento',
          ),
          inputFormatters: <TextInputFormatter> [
                WhitelistingTextInputFormatter.digitsOnly,
                // Fit the validating format.
                _birthDate,
              ]
        ),
Run Code Online (Sandbox Code Playgroud)

现在好了,谢谢


小智 7

https://pub.dartlang.org/packages/masked_text

masked_text

一个屏蔽文本的包,所以如果你想要一个电话面具、邮政编码或任何类型的面具,就用它:D

入门

它非常简单,它是一个和其他所有小部件一样的小部件。

new MaskedTextField
(
    maskedTextFieldController: _textCPFController,
    mask: "xx/xx/xxxx",
    maxLength: 10,
    keyboardType: TextInputType.number,
    inputDecoration: new InputDecoration(
    hintText: "Digite a data do seu nascimento", labelText: "Data"),
);
Run Code Online (Sandbox Code Playgroud)

'x' 是您的文本将具有的正常字符。

这个样本再现到底是这样的:11/02/1995

  • 不要使用这个。这是使用本机 TextField 小部件的更好方法:https://pub.dev/packages/mask_text_input_formatter (4认同)