<p>In this tutorial, I'm going to show you how to add a text to speech feature to your app. We are not going to use any API and this will be pretty simple using the <strong>flutter_text_to_speech</strong> package.</p> <p>Before moving on, let's see an illustration of what we will be able to achieve with this tutorial.</p> <p><strong>STEPS TO REPRODUCE:</strong></p> <p>** </p> <p> **</p> <ul> <li><p>Add the package dependency to your pubspec.yaml file</p> </li> <li><p>For this particular package to work, you will have to change your minSdkVersion value to 21. Navigate to the following file and use the below code to make the necessary change - /android/app/build.gradle</p> </li> </ul> <p></p> <ul> <li>Finally, use the following code in your main.dart file to get a working example as shown in the video.</li> </ul> <p>runApp(MyApp());</p> <p>class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, title: 'Text To Speech', theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(title: 'Text To Speech'), ); } }</p> <p>class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key);</p> <p>final String title;</p> <p>@override _MyHomePageState createState() => _MyHomePageState(); }</p> <p>class _MyHomePageState extends State { VoiceController _voiceController;</p> <p>String text = 'This is an example tutorial of using text to speech in a flutter application! The example is provided on fluttercentral website.';</p> <p>@override void initState() { _voiceController = FlutterTextToSpeech.instance.voiceController();</p> <pre>super.initState(); </pre> <p>}</p> <p>@override void dispose() { super.dispose(); _voiceController.stop(); }</p> <p>_playVoice() { <em>voiceController.init().then((</em>) { _voiceController.speak( text, VoiceControllerOptions(), ); }); }</p> <p>_stopVoice() { _voiceController.stop(); }</p> <p>@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Center( child: Column( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center, children: [ Text( text, textAlign: TextAlign.center, ), SizedBox( height: 10, ), RaisedButton( onPressed: _playVoice, color: Colors.blue, child: Text('Play Voice'), ), RaisedButton( onPressed: _stopVoice, color: Colors.blue, child: Text('Stop Voice'), ), ], ), ), ); } }</p> <p>I hope you liked this tutorial.</p> <p>If you have any doubts, drop them in comments :)</p>
Flutter text to speech example
This Article is posted by abbas.devcode at 4/26/2020 10:13:09 PM
Check out our other latest articles
Safearea widget - How to avoid visual overlap with Notch on flutter mobile app?How to convert row of widgets into column of widgets in flutter based on screen size?
How to run Flutter programs from GitHub?
How to get screen orientation in flutter?
NavigationRail example in flutter
Tags: text to speech
Check out our other latest articles
Safearea widget - How to avoid visual overlap with Notch on flutter mobile app?How to convert row of widgets into column of widgets in flutter based on screen size?
How to run Flutter programs from GitHub?
How to get screen orientation in flutter?
NavigationRail example in flutter