在文本表单字段输入中添加特殊字符 [Flutter]

发布于 2025-01-20 11:43:16 字数 813 浏览 0 评论 0原文

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

感谢您的帮助!

输入示例 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"),

In a classic textformfield widget, I want to put the "/" character automatically after the fourth character when the user enters input. How can I do this?

Thanks for help!

Example input
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"),

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

欢你一世 2025-01-27 11:43:16
                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"),
                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"),
杀お生予夺 2025-01-27 11:43:16

如果要自动添加/,则可以使用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),);
  } 
}

If you want to add / automatically, you can use onChange property of TextField.

You can add following code in onChange property

onChanged : (String value) {
  if(value.length >= 4 && !value.contains("\\")) {
    value = '$value\\';
    _dosyaNo.value = TextEditingValue(text: value,selection: TextSelection.collapsed(offset: value.length),);
  } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文