-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathflutter_tts.dart
144 lines (133 loc) · 4.29 KB
/
flutter_tts.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import 'package:flutter/material.dart';
import 'package:flutter_tts/flutter_tts.dart';
class MyFlutterTTS extends StatefulWidget {
const MyFlutterTTS({Key? key}) : super(key: key);
@override
// ignore: library_private_types_in_public_api
_MyFlutterTTSState createState() => _MyFlutterTTSState();
}
class _MyFlutterTTSState extends State<MyFlutterTTS> {
String description =
"The birth centenary of Father of the Nation Bangabandhu Sheikh Mujibur Rahman is being celebrated across the country in 2020-21. Under the ICT Division of the Government of the People’s Republic of Bangladesh, different initiatives have been taken by the Bangladesh Computer Council’s iDEA project to organize the Mujib Year, most notably the “Bangabandhu Innovation Grant 2020 (BIG)” event.";
bool isPlaying = false;
final FlutterTts _flutterTts = FlutterTts();
@override
void initState() {
super.initState();
_flutterTts.setStartHandler(() {
setState(() {
isPlaying = true;
});
});
_flutterTts.setCompletionHandler(() {
setState(() {
isPlaying = false;
});
});
}
// Android settings
void speechSettingsAndroid() {
_flutterTts.setLanguage("en-US");
_flutterTts.setPitch(1.0);
_flutterTts.setSpeechRate(1.0);
_flutterTts.setVolume(1.0);
}
// iOS settings
void speechSettingsIOS() {
_flutterTts.setLanguage("en-US");
_flutterTts.setPitch(1.0);
_flutterTts.setSpeechRate(.5);
_flutterTts.setVolume(1.0);
}
Future _speak(String text) async {
if (text.isNotEmpty) {
var result = await _flutterTts.speak(text);
if (result == 1) {
setState(() {
isPlaying = true;
});
}
}
}
Future _stop() async {
var result = await _flutterTts.stop();
if (result == 1) {
setState(() {
isPlaying = false;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter TTS'),
),
body: Center(
child: Column(
children: [
const SizedBox(height: 20),
const Text(
'Text-To-Read',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
Stack(
alignment: Alignment.topCenter,
children: [
Container(
color: Colors.transparent,
height: 340,
),
Padding(
padding: const EdgeInsets.all(10.0),
child: Container(
decoration: BoxDecoration(
color: Colors.deepPurple[300],
borderRadius: BorderRadius.circular(10)),
child: Padding(
padding: const EdgeInsets.all(15.0),
child: Text(
description,
style:
const TextStyle(fontSize: 18, color: Colors.white),
textAlign: TextAlign.justify,
textDirection: TextDirection.ltr,
),
),
),
),
Positioned(
bottom: 0,
child: RawMaterialButton(
padding: const EdgeInsets.all(10.0),
elevation: 12.0,
onPressed: () {
setState(() {
speechSettingsIOS();
//speechSettingsAndroid();
isPlaying ? _stop() : _speak(description);
});
},
shape: const CircleBorder(),
fillColor: Colors.white,
child: isPlaying
? const Icon(
Icons.stop,
size: 60,
color: Colors.green,
)
: const Icon(
Icons.play_arrow,
size: 60,
color: Colors.red,
),
),
),
],
)
],
),
),
);
}
}