英文:
How to call function after displaying an alert without pressing button with flutter?
问题
我正在创建一个将文本转换为语音的函数。我想在显示警报后调用它,因为它应该读取警报的内容。
目前,该函数只在点击按钮时起作用。
这是该函数的代码:
speak(String text) async {
await flutterTts.setLanguage('en-US');
await flutterTts.setPitch(1);
await flutterTts.speak(text);
}
如何在不按任何按钮的情况下调用这个函数?
英文:
I'm creating a function that transfer the text to speech. I want to call it after displaying an alert because it should read the content of the alert.
For now, the function is working juste on clicking a button.
this is the function:
speak(String text) async {
await flutterTts.setLanguage('en-US');
await flutterTts.setPitch(1);
await flutterTts.speak(text);
}
child:
AlertDialog(
contentPadding: EdgeInsets.zero,
clipBehavior: Clip.antiAlias,
content: Row(
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: RichText(
text: TextSpan(
text:
'${view.description}\n'
How can I call this function without pressing any button ?
答案1
得分: 1
你可以在 showDialog
后像这样调用 speak
:
onTap: () {
showDialog(
context: context,
builder: (BuildContext context) {
return Dialog(
...
);
});
speak();
}
如果你将 Alert
作为小部件显示,你可以像这样使用 builder
小部件包装它:
Builder(
builder: (context) {
speak();
return AlertDialog(
...
);
},
),
英文:
you can call the speak
after showDialog
like this:
onTap: () {
showDialog(
context: context,
builder: (BuildContext context) {
return Dialog(
...
);
});
speak();
}
If you are showing Alert
as widget you can wrap it whit builder
widget like this:
Builder(
builder: (context) {
speak();
return AlertDialog(
...
);
},
),
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论