Created
March 20, 2021 15:53
-
-
Save edward1986/330fba4579f758021dbaa21d4ed66fd2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_tts/flutter_tts.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatefulWidget { | |
@override | |
_MyAppState createState() => _MyAppState(); | |
} | |
enum TtsState { playing, stopped } | |
class _MyAppState extends State<MyApp> { | |
FlutterTts flutterTts; | |
dynamic languages; | |
String language; | |
double volume = 0.5; | |
double pitch = 1.0; | |
double rate = 0.5; | |
String _newVoiceText; | |
TtsState ttsState = TtsState.stopped; | |
get isPlaying => ttsState == TtsState.playing; | |
get isStopped => ttsState == TtsState.stopped; | |
@override | |
initState() { | |
super.initState(); | |
initTts(); | |
} | |
initTts() { | |
flutterTts = FlutterTts(); | |
_getLanguages(); | |
flutterTts.setStartHandler(() { | |
setState(() { | |
print("playing"); | |
ttsState = TtsState.playing; | |
}); | |
}); | |
flutterTts.setCompletionHandler(() { | |
setState(() { | |
print("Complete"); | |
ttsState = TtsState.stopped; | |
}); | |
}); | |
flutterTts.setErrorHandler((msg) { | |
setState(() { | |
print("error: $msg"); | |
ttsState = TtsState.stopped; | |
}); | |
}); | |
} | |
Future _getLanguages() async { | |
languages = await flutterTts.getLanguages; | |
print("pritty print ${languages}"); | |
if (languages != null) setState(() => languages); | |
} | |
Future _speak() async { | |
await flutterTts.setVolume(volume); | |
await flutterTts.setSpeechRate(rate); | |
await flutterTts.setPitch(pitch); | |
if (_newVoiceText != null) { | |
if (_newVoiceText.isNotEmpty) { | |
var result = await flutterTts.speak(_newVoiceText); | |
if (result == 1) setState(() => ttsState = TtsState.playing); | |
} | |
} | |
} | |
Future _stop() async { | |
var result = await flutterTts.stop(); | |
if (result == 1) setState(() => ttsState = TtsState.stopped); | |
} | |
@override | |
void dispose() { | |
super.dispose(); | |
flutterTts.stop(); | |
} | |
List<DropdownMenuItem<String>> getLanguageDropDownMenuItems() { | |
var items = List<DropdownMenuItem<String>>(); | |
for (String type in languages) { | |
items.add(DropdownMenuItem(value: type, child: Text(type))); | |
} | |
return items; | |
} | |
void changedLanguageDropDownItem(String selectedType) { | |
setState(() { | |
language = selectedType; | |
flutterTts.setLanguage(language); | |
}); | |
} | |
void _onChange(String text) { | |
setState(() { | |
_newVoiceText = text; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
bottomNavigationBar: bottomBar(), | |
appBar: AppBar( | |
title: Text('Text To Speech',), | |
centerTitle: true, | |
backgroundColor: Colors.green, | |
), | |
body: SingleChildScrollView( | |
scrollDirection: Axis.vertical, | |
child: Column(children: [ | |
_inputSection(), | |
languages != null ? _languageDropDownSection() : Text(""), | |
_buildSliders() | |
])))); | |
} | |
Widget _inputSection() => | |
Container( | |
alignment: Alignment.topCenter, | |
padding: EdgeInsets.only(top: 25.0, left: 25.0, right: 25.0), | |
child: TextField( | |
onChanged: (String value) { | |
_onChange(value); | |
}, | |
)); | |
Widget _languageDropDownSection() => | |
Container( | |
padding: EdgeInsets.only(top: 50.0), | |
child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [ | |
DropdownButton( | |
value: language, | |
items: getLanguageDropDownMenuItems(), | |
onChanged: changedLanguageDropDownItem, | |
) | |
])); | |
Column _buildButtonColumn(Color color, Color splashColor, IconData icon, | |
String label, Function func) { | |
return Column( | |
mainAxisSize: MainAxisSize.min, | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
IconButton( | |
icon: Icon(icon), | |
color: color, | |
splashColor: splashColor, | |
onPressed: () => func()), | |
Container( | |
margin: const EdgeInsets.only(top: 8.0), | |
child: Text(label, | |
style: TextStyle( | |
fontSize: 12.0, | |
fontWeight: FontWeight.w400, | |
color: color))) | |
]); | |
} | |
Widget _buildSliders() { | |
return Column( | |
children: [_volume(), _pitch(), _rate()], | |
); | |
} | |
Widget _volume() { | |
return Slider( | |
value: volume, | |
onChanged: (newVolume) { | |
setState(() => volume = newVolume); | |
}, | |
min: 0.0, | |
max: 1.0, | |
divisions: 10, | |
label: "Volume: $volume"); | |
} | |
Widget _pitch() { | |
return Slider( | |
value: pitch, | |
onChanged: (newPitch) { | |
setState(() => pitch = newPitch); | |
}, | |
min: 0.5, | |
max: 2.0, | |
divisions: 15, | |
label: "Pitch: $pitch", | |
activeColor: Colors.red, | |
); | |
} | |
Widget _rate() { | |
return Slider( | |
value: rate, | |
onChanged: (newRate) { | |
setState(() => rate = newRate); | |
}, | |
min: 0.0, | |
max: 1.0, | |
divisions: 10, | |
label: "Rate: $rate", | |
activeColor: Colors.green, | |
); | |
} | |
bottomBar() => | |
Container( | |
margin: EdgeInsets.all(10.0), | |
height: 50, | |
child: Row( | |
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | |
children: <Widget>[ | |
FloatingActionButton( | |
onPressed: _speak, | |
child: Icon(Icons.play_arrow), | |
backgroundColor: Colors.green, | |
), | |
FloatingActionButton( | |
onPressed: _stop, | |
backgroundColor: Colors.red, | |
child: Icon(Icons.stop), | |
), | |
], | |
), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment