我只是實作了一個簡單的 TextField,但是當我輸入多個空格時,它會在此之前自動添加一個點。
我的自定義顫振文本欄位
這是我的自定義 TextField 小部件
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(5),
child: Column(children: [
Align(
alignment: Alignment.topLeft,
child: Padding(
padding: const EdgeInsets.only(bottom: 5),
child: Text(
title,
),
)),
TextField(
controller: _controller,
autocorrect: false,
decoration: InputDecoration(
isDense: true,
contentPadding: const EdgeInsets.all(10),
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(
color: Colors.black,
width: 2,
),
borderRadius: BorderRadius.circular(5),
),
focusedBorder: OutlineInputBorder(
borderSide: const BorderSide(
color: Colors.orange,
width: 2,
),
borderRadius: BorderRadius.circular(5),
),
))
]));
}
uj5u.com熱心網友回復:
這是 iOS 鍵盤和大多數 Android 鍵盤的標準功能。我認為你無法從 Flutter 中控制它。
uj5u.com熱心網友回復:
我認為這與應用程式本身無關,而是與電話有關。您需要從手機的設定中禁用它。
雖然,如果你真的需要能夠輸入雙空格,這里是我如何實作它。
final TextEditingController controller = TextEditingController();
TextSelection? cursor;
int length = 0;
String lastChar = '';
String currentChar = '';
String replaceCharAt(String oldString, int index, String newChar) {
// function from https://stackoverflow.com/questions/52083836/how-to-replace-only-one-character-in-a-string-in-dart
return oldString.substring(0, index)
newChar
oldString.substring(index 1);
}
void removeDotOnSpace(String input) {
//save the position of the cursor
cursor = controller.selection;
lastChar = currentChar;
// if the input isn't empty and if you're not removing text
if (input.isNotEmpty && input.length > length) {
currentChar = input[input.length - 1];
// if it has at least two characters, the second to last being a dot and the "lastChar" variable not being a dot
if (input.length >= 2 &&
input[input.length - 2] == '.' &&
lastChar != '.') {
// replace the dot and set state. Because setstate resests cursor position we need to save it and give it back.
setState(() {
controller.text = replaceCharAt(input, input.length - 2, ' ');
controller.selection = cursor!;
});
}
} else {
currentChar = '';
lastChar = '';
}
length = input.length;
}
把它放在一個有狀態的小部件中并在 onchanged 函式中使用它
TextFormField(
controller: controller,
onChanged: (value) {
removeDotOnSpace(value);
},
),
PS:除非您的文本欄位必須能夠有雙空格,否則您應該讓它成為。
uj5u.com熱心網友回復:
嘗試 textCapitalization: TextCapitalization.words,
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/393799.html
