使用 Text to speech 搜索仅适用于 speech 或 textField
search with Text to speech works only either with speech or textField
所以我有一个普通的 TextField,用于过滤列表
children: products.map((doc) => _buildSingleProduct(doc)).toList(),
,它正常工作,然后我给它添加了一个 text-to-speech 插件 speech_recognition:
并将它与过滤功能结合起来,它工作得很好。
问题是当我完成语音过滤时,例如我想通过写入不再过滤的 TextField 来添加或更正它。
文本域
child: TextField(
controller: controller,
decoration: InputDecoration(
labelText: allTranslations.text(StringConstant.search),
prefixIcon: Icon(Icons.search),
suffixIcon: IconButton(
icon: Icon(Icons.mic),
onPressed: () {
if (_isAvailable && !_isListening)
_speechRecognition
.listen(locale: "en_US")
.then((result) => print('$result'));
},
),
),
),
如您所见,我使用 controller
进行过滤,然后使用麦克风图标将语音结果传递给控制器,如下所示:
_speechRecognition
.setRecognitionResultHandler((String result) => setState(() {
controller = TextEditingController(text: resultText = result);
}));
这里我从语音中获取结果并将其添加到过滤器和控制器的 resultText 中,因此它出现在文本字段中。
如果我这样做:
_speechRecognition
.setRecognitionResultHandler((String speech) => setState(() => resultText = speech));
一切正常,但文本显然没有出现在 text-field 中。
对于 textField 过滤,我初始化状态以将其添加到 resultText:
initState() {
initSpeechRecognizer();
controller.addListener(() {
setState(() {
resultText = controller.text;
});
});
super.initState();
}
这就是我 return 来自数据库的结果:
return resultText == null || resultText == ""
? buildProducts(id, title, favorite, message, price, doc)
: doc.data['title'].toLowerCase().contains(resultText.toLowerCase())
? buildProducts(id, title, favorite, message, price, doc)
: Container();
正如你可能看到的那样,我搜索了标题。
问题又来了,
1.search 有演讲
它出现在文本字段上并过滤列表
当我按文本字段更改查询时,它不再过滤。
但反之亦然
用文本过滤列表
过滤列表
我激活了 speech-to-text,它更改了查询并使用新查询过滤列表。
所以对于需要解决方案的人来说
_speechRecognition
.setRecognitionResultHandler((String result) => setState(() {
resultText = result;
controller.text = resultText;
}));
您从语音中获得结果,将其添加到处理变量中,但您还将该结果添加到控制器中,以便在文本字段中获得结果。
所以我有一个普通的 TextField,用于过滤列表
children: products.map((doc) => _buildSingleProduct(doc)).toList(),
,它正常工作,然后我给它添加了一个 text-to-speech 插件 speech_recognition:
并将它与过滤功能结合起来,它工作得很好。
问题是当我完成语音过滤时,例如我想通过写入不再过滤的 TextField 来添加或更正它。
文本域
child: TextField(
controller: controller,
decoration: InputDecoration(
labelText: allTranslations.text(StringConstant.search),
prefixIcon: Icon(Icons.search),
suffixIcon: IconButton(
icon: Icon(Icons.mic),
onPressed: () {
if (_isAvailable && !_isListening)
_speechRecognition
.listen(locale: "en_US")
.then((result) => print('$result'));
},
),
),
),
如您所见,我使用 controller
进行过滤,然后使用麦克风图标将语音结果传递给控制器,如下所示:
_speechRecognition
.setRecognitionResultHandler((String result) => setState(() {
controller = TextEditingController(text: resultText = result);
}));
这里我从语音中获取结果并将其添加到过滤器和控制器的 resultText 中,因此它出现在文本字段中。
如果我这样做:
_speechRecognition
.setRecognitionResultHandler((String speech) => setState(() => resultText = speech));
一切正常,但文本显然没有出现在 text-field 中。
对于 textField 过滤,我初始化状态以将其添加到 resultText:
initState() {
initSpeechRecognizer();
controller.addListener(() {
setState(() {
resultText = controller.text;
});
});
super.initState();
}
这就是我 return 来自数据库的结果:
return resultText == null || resultText == ""
? buildProducts(id, title, favorite, message, price, doc)
: doc.data['title'].toLowerCase().contains(resultText.toLowerCase())
? buildProducts(id, title, favorite, message, price, doc)
: Container();
正如你可能看到的那样,我搜索了标题。
问题又来了,
1.search 有演讲
它出现在文本字段上并过滤列表
当我按文本字段更改查询时,它不再过滤。
但反之亦然
用文本过滤列表
过滤列表
我激活了 speech-to-text,它更改了查询并使用新查询过滤列表。
所以对于需要解决方案的人来说
_speechRecognition
.setRecognitionResultHandler((String result) => setState(() {
resultText = result;
controller.text = resultText;
}));
您从语音中获得结果,将其添加到处理变量中,但您还将该结果添加到控制器中,以便在文本字段中获得结果。