从外部源向 TextField 添加文本
Add text to TextField from an outside source
我在文本字段中添加了语音识别,它可以工作,但我无法设法将文本添加到文本字段中,有没有办法做到这一点。
文本字段如下所示:
Widget _buildDescriptionTextField(productBloc) {
return StreamBuilder<Object>(
stream: productBloc.messageStream,
builder: (context, snapshot) {
return TextField(
maxLines: 3,
controller: _controllerMessage,
onChanged: productBloc.messageSink,
decoration: InputDecoration(
labelText: allTranslations.text(StringConstant.description),
errorText: snapshot.error,
suffixIcon: IconButton(icon: Icon(Icons.mic), onPressed: () {
if (_isAvailable && !_isListening)
_speechRecognition
.listen(locale: "en_US")
.then((result) => print('$result'));
},
),
),
);
}
);
}
我有一个 steam-builder 来手动管理添加的文本,还有一个控制器,如果这个页面用于编辑,然后作为 suffixsIcon 的 iconButton 来启动语音识别。当我在文本小部件外添加结果文本时,它可以工作,但我需要在 texField 内。
只是这样做应该行不?
setState(() => _controllerMessage.text = result)
您需要使用 TextEditingController
属性。我假设您将一个声明为 _controllerMessage
.
要为您的 TextField
设置新值并将光标保持在最后 - 使用类似于文档中示例的内容。
例如
_speechRecognition
.listen(locale: "en_US")
.then(_onResult);
// ...
void _onResult(String result) {
setState(() {
_controllerMessage.value = _controllerMessage.value.copyWith(
text: result,
selection: TextSelection(baseOffset: result.length, extentOffset: result.length),
composing: TextRange.empty,
);
});
}
如果有帮助请告诉我。
所以我所做的只是使用文档中的 _speechRecognition.setRecognitionResultHandler
来为 textField 的控制器设置一个新值,如下所示:
_speechRecognition.setRecognitionResultHandler(
(String speech) => setState(() {
_controllerMessage = new TextEditingController(text: resultText = speech);
})
);
textField 保持原样,请参阅问题。
我在文本字段中添加了语音识别,它可以工作,但我无法设法将文本添加到文本字段中,有没有办法做到这一点。
文本字段如下所示:
Widget _buildDescriptionTextField(productBloc) {
return StreamBuilder<Object>(
stream: productBloc.messageStream,
builder: (context, snapshot) {
return TextField(
maxLines: 3,
controller: _controllerMessage,
onChanged: productBloc.messageSink,
decoration: InputDecoration(
labelText: allTranslations.text(StringConstant.description),
errorText: snapshot.error,
suffixIcon: IconButton(icon: Icon(Icons.mic), onPressed: () {
if (_isAvailable && !_isListening)
_speechRecognition
.listen(locale: "en_US")
.then((result) => print('$result'));
},
),
),
);
}
);
}
我有一个 steam-builder 来手动管理添加的文本,还有一个控制器,如果这个页面用于编辑,然后作为 suffixsIcon 的 iconButton 来启动语音识别。当我在文本小部件外添加结果文本时,它可以工作,但我需要在 texField 内。
只是这样做应该行不?
setState(() => _controllerMessage.text = result)
您需要使用 TextEditingController
属性。我假设您将一个声明为 _controllerMessage
.
要为您的 TextField
设置新值并将光标保持在最后 - 使用类似于文档中示例的内容。
例如
_speechRecognition
.listen(locale: "en_US")
.then(_onResult);
// ...
void _onResult(String result) {
setState(() {
_controllerMessage.value = _controllerMessage.value.copyWith(
text: result,
selection: TextSelection(baseOffset: result.length, extentOffset: result.length),
composing: TextRange.empty,
);
});
}
如果有帮助请告诉我。
所以我所做的只是使用文档中的 _speechRecognition.setRecognitionResultHandler
来为 textField 的控制器设置一个新值,如下所示:
_speechRecognition.setRecognitionResultHandler(
(String speech) => setState(() {
_controllerMessage = new TextEditingController(text: resultText = speech);
})
);
textField 保持原样,请参阅问题。