如何将检索到的文本传递给文本字段 Flutter
How to pass retrieved text to textfield Flutter
我正在使用语音转文本 (speech_recognition ) 插件并从语音中检索文本,但如何在检索文本后将文本放入搜索字段 (TextField)。所以我想从语音中检索数据并进行搜索。
这是我的代码:
// 这是在容器中检索到的文本
Container(
child: Center(
child: Text(
(transcription.isEmpty)
? "Speak to Record"
: "Your text is \n\n$transcription",
textAlign: TextAlign.center,
style: TextStyle(color: Colors.pink, fontSize: 20),
),
),
),
// 这是我的 SearchField (TextField)
Padding(
padding: const EdgeInsets.all(20),
child: TextField(
onChanged: (text) {
_con.refreshSearch(text);
},
onSubmitted: (text) {
_con.saveSearch(text);
},
controller: _searchController,
autofocus: true,
decoration: InputDecoration(
contentPadding: EdgeInsets.all(12),
hintText: S.of(context).search_for_product,
hintStyle: Theme.of(context)
.textTheme
.caption
.merge(TextStyle(fontSize: 14)),
prefixIcon:
Icon(Icons.search, color: Theme.of(context).accentColor),
border: OutlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context).focusColor.withOpacity(0.1))),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context).focusColor.withOpacity(0.3))),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context).focusColor.withOpacity(0.1))),
suffixIcon: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.end,
children: [
InkWell(
onTap: scanBarcode,
child: Image.asset(
"assets/img/barcode.png",
height: 20,
color: Colors.deepOrangeAccent,
),
),
_buildVoiceInput(
onPressed: _speechRecognitionAvailable && !_isListening
? () => start()
: () => stop(),
label: _isListening ? S.of(context).listening : '',
),
],
),
),
),
),
考虑到transcription
值,它负责有文本,你可以做的就是利用它,并使用Search TextEditingController
更改数据。
所以,这就是我要说的。我不确定你实际上在哪里获取数据,但是你可以更改值,只要你像这样从语音中获取数据,并将其分配给控制器以填充 Search Field
// Suppose you are fetching the data from this method,
void fetchTextFromVoice(){
setState(() {
// Here you might be assigning the value to the Transcript value
this.transcription = data_from_voice;
// also this does the magic, which will put the data inside the Search Field
// Your TextEditingController is the key to do that
_searchController.text = data_from_voice;
// or _searchController.text = transcription; Since transcript has some value
});
}
另外,阅读 TextEditingController Class 了解您可以使用它执行哪些操作。在这方面向您提供更多知识 Class 将在很大程度上帮助您 :)
希望对您有所帮助,如果没有,请告诉我您将值设置为 transcription
的函数在哪里。您可以将值分配给同一个地方,就像上面的 _searchController.text
一样,您就可以开始了。让我知道:)
我正在使用语音转文本 (speech_recognition ) 插件并从语音中检索文本,但如何在检索文本后将文本放入搜索字段 (TextField)。所以我想从语音中检索数据并进行搜索。 这是我的代码:
// 这是在容器中检索到的文本
Container(
child: Center(
child: Text(
(transcription.isEmpty)
? "Speak to Record"
: "Your text is \n\n$transcription",
textAlign: TextAlign.center,
style: TextStyle(color: Colors.pink, fontSize: 20),
),
),
),
// 这是我的 SearchField (TextField)
Padding(
padding: const EdgeInsets.all(20),
child: TextField(
onChanged: (text) {
_con.refreshSearch(text);
},
onSubmitted: (text) {
_con.saveSearch(text);
},
controller: _searchController,
autofocus: true,
decoration: InputDecoration(
contentPadding: EdgeInsets.all(12),
hintText: S.of(context).search_for_product,
hintStyle: Theme.of(context)
.textTheme
.caption
.merge(TextStyle(fontSize: 14)),
prefixIcon:
Icon(Icons.search, color: Theme.of(context).accentColor),
border: OutlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context).focusColor.withOpacity(0.1))),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context).focusColor.withOpacity(0.3))),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context).focusColor.withOpacity(0.1))),
suffixIcon: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.end,
children: [
InkWell(
onTap: scanBarcode,
child: Image.asset(
"assets/img/barcode.png",
height: 20,
color: Colors.deepOrangeAccent,
),
),
_buildVoiceInput(
onPressed: _speechRecognitionAvailable && !_isListening
? () => start()
: () => stop(),
label: _isListening ? S.of(context).listening : '',
),
],
),
),
),
),
考虑到transcription
值,它负责有文本,你可以做的就是利用它,并使用Search TextEditingController
更改数据。
所以,这就是我要说的。我不确定你实际上在哪里获取数据,但是你可以更改值,只要你像这样从语音中获取数据,并将其分配给控制器以填充 Search Field
// Suppose you are fetching the data from this method,
void fetchTextFromVoice(){
setState(() {
// Here you might be assigning the value to the Transcript value
this.transcription = data_from_voice;
// also this does the magic, which will put the data inside the Search Field
// Your TextEditingController is the key to do that
_searchController.text = data_from_voice;
// or _searchController.text = transcription; Since transcript has some value
});
}
另外,阅读 TextEditingController Class 了解您可以使用它执行哪些操作。在这方面向您提供更多知识 Class 将在很大程度上帮助您 :)
希望对您有所帮助,如果没有,请告诉我您将值设置为 transcription
的函数在哪里。您可以将值分配给同一个地方,就像上面的 _searchController.text
一样,您就可以开始了。让我知道:)