在 textformfield 输入中添加一个特殊字符 [Flutter]

Add a special character inside of textformfield input [Flutter]

在经典的文本表单域小部件中,我想在用户输入时自动将“/”字符放在第四个字符之后。我该怎么做?

感谢帮助!

示例输入 2012/324234

                 OcasTextFormFieldContent(
                      maxLength: 12,
                      inputType: TextInputType.number,
                      validator: (String? fieldContent) {
                        if (fieldContent == "" || fieldContent == null) {
                          return 'not empty';
                        } else if (fieldContent.length < 12) {
                          return 'wrong type.';
                        } else {
                          return null;
                        }
                      },
                      controller: _dosyaNo,
                      name: "Doc No: ",
                      placeholder: "Please enter doc no"),

如果要自动添加/,可以使用TextField的onChange属性。

您可以在 onChange 属性

中添加以下代码
onChanged : (String value) {
  if(value.length >= 4 && !value.contains("\")) {
    value = '$value\';
    _dosyaNo.value = TextEditingValue(text: value,selection: TextSelection.collapsed(offset: value.length),);
  } 
}
                OcasTextFormFieldContent(
                     onChanged: (value) {
                       if(value.length >= 4 && !value.contains("\")) {
                       value = '$value\';
                      _dosyaNo.value = TextEditingValue(text: 
                      value,selection: TextSelection.collapsed(offset: 
                      value.length),);
                       } 
                        },
                          maxLength: 12,
                          inputType: TextInputType.number,
                          validator: (String? fieldContent) {
                            if (fieldContent == "" || fieldContent == null) {
                              return 'not empty';
                            } else if (fieldContent.length < 12) {
                              return 'wrong type.';
                            } else {
                              return null;
                            }
                          },
                          controller: _dosyaNo,
                          name: "Doc No: ",
                          placeholder: "Please enter doc no"),