final textController = TextEditingController();
final StreamController<String> textStream = StreamController.broadcast();
@override
void initState() {
super.initState();
textController.addListener(textListener);
textStream.stream.debounce(const Duration(milliseconds: 500)).listen((event) {});
}
void textListener() {
if (textController.text.trim().isNotEmpty &&
//防止输入中文把正在输入的拼音也带进搜索
!textController.value.composing.isValid &&
//防止删除调用两次
textController.selection.baseOffset == textController.selection.extentOffset) {
textStream.sink.add(textController.text.trim());
}
}
Flutter 解决TextEditingController中文输入法监听带入拼音问题
于 2023-09-18 11:27:25 首次发布