我想将 DropDownMenu 的文本字段与输入格式化程序一起使用,但我找不到任何选项。当用户使用键盘(网络或移动设备)输入信息时,有没有办法将 DropDownMenu 字段中所有单词的第一个字母大写?
在不需要 DropDownMenu 的地方,我使用了 TextFormField 的 inputFormatters 属性。但对于这种情况,需要可过滤/可搜索的 DropDownMenu。
当我想通过点击选择菜单项时,使用 Stack 构建我自己的可搜索 DropDownMenu 的其他方法不起作用。
谢谢。
您可以像我在之前的项目中那样使用它。
class Capitalization extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
if (newValue.text.isEmpty) {
return newValue.copyWith(text: '');
} else {
return TextEditingValue(
text: newValue.text.replaceFirst(newValue.text[0], newValue.text[0].toUpperCase()),
selection: newValue.selection,
composing: newValue.composing,
);
}
}
}
TextFormField(
inputFormatters: [Capitalization()],
onChanged: (value) {
},
),
DropdownMenu 小部件现在支持 inputFormatters 属性,如 flutter 通道 beta 中的 TextFormField。它应该很快就会流向稳定的通道。
示例:
DropdownMenu(
dropdownMenuEntries: /*....*/ ,
inputFormatters: []
)