简体   繁体   中英

Add a special character inside of textformfield input [Flutter]

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

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),);
  } 
}
                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"),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM