我正在尝试将Textformfield的第一个字母大写,为此我使用了
textCapitalization: TextCapitalization.words,
它对于Textformfield
不起作用,但对于textfield
起作用。
请帮忙解决如何做到这一点。
我正在尝试将Textformfield的第一个字母大写,为此我使用了
textCapitalization: TextCapitalization.words,
它对于Textformfield
不起作用,但对于textfield
起作用。
请帮忙解决如何做到这一点。
尝试下面的代码,希望对您有所帮助。请参考TextCapitalization
这里
TextCapitalization.words
TextCapitalization.characters
TextCapitalization.sentences
TextCapitalization.none
您的小部件:
TextField(
textCapitalization: TextCapitalization.words,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
prefixIcon: Icon(
Icons.search,
),
hintText: 'Search',
),
),
使用 TextCapitalization.words
的结果屏幕:
使用 TextCapitalization.characters
的结果屏幕:
TextFormField(
controller: _textEditingController,
inputFormatters: <TextInputFormatter>[
UpperCaseTextFormatter()
],
)
大写文本格式化程序
class UpperCaseTextFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
return TextEditingValue(
text: capitalize(newValue.text),
selection: newValue.selection,
);
}
}
String capitalize(String value) {
if(value.trim().isEmpty) return "";
return "${value[0].toUpperCase()}${value.substring(1).toLowerCase()}";
}
输出:
TextFormField(
textCapitalization: TextCapitalization.words,
),
这个功能可以将我们在TextFormField中输入的每个单词的首字母大写。
设置textCapitalization后,请在模拟器或设备实例上重新构建并再次检查。如果有效,请告诉我,
textCapitalization: TextCapitalization.sentences // Capital first letter
keyboardType: TextInputType.text,
keyboardType: TextInputType.name
时,它不起作用。textCapitalization: TextCapitalization.sentences
。这将使首字母大写。可以与其他属性一起使用。keyboardType: TextInputType.text,
我只是在完善Safeela Nasarin的答案,这个答案很有效。
只需确保使用嵌入式模拟器键盘或设备嵌入式键盘,具体取决于您的情况。它不会与外部或计算机键盘一起使用。
textCapitalization
属性无法胜任此工作,因为它只是通过自动大写键盘向用户提供建议,而我的要求是输入必须是单词大写的。
所以我需要自己实现它,使用Jahidul提供的基本TextEditingValue
。这会改变输入,因此用户无法违反我们对该字段的要求。
希望能帮助其他人。
class UpperCaseTextFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
return TextEditingValue(
text: capitalizeAllWordsInFullSentence(newValue.text),
// text: capitalizeAllWordsInFullSentence(newValue.text),
selection: newValue.selection,
);
}
}
String capitalizeAllWordsInFullSentence(String str) {
int i;
String constructedString = "";
for (i = 0; i < str.length; i++) {
if (i == 0) {
constructedString += str[0].toUpperCase();
} else if (str[i - 1] == ' ') {
// mandatory to have index>1 !
constructedString += str[i].toUpperCase();
} else {
constructedString += str[i];
}
}
// print('constructed: $constructedString');
return constructedString;
}
String capitalize1Word(String value) {
if (value.trim().isEmpty) return "";
return "${value[0].toUpperCase()}${value.substring(1).toLowerCase()}";
}
我遇到了同样的问题,TextCapitalization.sentences
或 TextCapitalization.words
对我不起作用,因为有 keyboardType: TextInputType.name
。当我将其更改为 keyboardType: TextInputType.text
时,textCapitalization
在句子和单词中都开始工作。
TextField(
controller: mytext,
onChanged: (value) {
mytext.value = TextEditingValue(
text: value.toUpperCase(),
selection: mytext.selection
);
}
)
String _capitalizeWords(String text) {
if (text == null || text.isEmpty) {
return text;
}
List<String> words = text.split(' ');
for (int i = 0; i < words.length; i++) {
if (words[i].isNotEmpty) {
words[i] = words[i][0].toUpperCase() + words[i].substring(1);
}
}
return words.join(' ');
}
TextFormField(
other properties ...
onChanged: (text) {
.textController.value = textController.value.copyWith(
text: _capitalizeWords(text),
selection: TextSelection.collapsed(offset: text.length),
);
},
...
),