在显示警告后,如何在不按按钮的情况下调用函数,使用Flutter?

huangapple go评论89阅读模式
英文:

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(
           ...
          );
  },
),

huangapple
  • 本文由 发表于 2023年1月9日 17:42:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055428.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定