Home > Software design >  How to call function after displaying an alert without pressing button with flutter?
How to call function after displaying an alert without pressing button with flutter?

Time:01-09

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 ?

CodePudding user response:

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

 
  • Related