-
Notifications
You must be signed in to change notification settings - Fork 67
/
main.dart
228 lines (202 loc) · 6.5 KB
/
main.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//
// Copyright 2022-2023 Picovoice Inc.
//
// You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
// file accompanying this source.
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
//
import 'dart:async';
import 'dart:io';
import 'package:cheetah_demo/cheetah_manager.dart';
import 'package:cheetah_flutter/cheetah_error.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final String accessKey =
'{YOUR_ACCESS_KEY_HERE}'; // AccessKey obtained from Picovoice Console (https://console.picovoice.ai/)
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
bool isError = false;
String errorMessage = "";
bool isProcessing = false;
String transcriptText = "";
CheetahManager? _cheetahManager;
final ScrollController _controller = ScrollController();
@override
void initState() {
super.initState();
setState(() {
transcriptText = "";
});
initCheetah();
}
Future<void> initCheetah() async {
String platform = Platform.isAndroid
? "android"
: Platform.isIOS
? "ios"
: throw CheetahRuntimeException(
"This demo supports iOS and Android only.");
String modelPath = "assets/models/$platform/cheetah_params.pv";
try {
_cheetahManager = await CheetahManager.create(
accessKey, modelPath, transcriptCallback, errorCallback);
} on CheetahActivationException {
errorCallback(CheetahActivationException("AccessKey activation error."));
} on CheetahActivationLimitException {
errorCallback(CheetahActivationLimitException(
"AccessKey reached its device limit."));
} on CheetahActivationRefusedException {
errorCallback(CheetahActivationRefusedException("AccessKey refused."));
} on CheetahActivationThrottledException {
errorCallback(
CheetahActivationThrottledException("AccessKey has been throttled."));
} on CheetahException catch (ex) {
errorCallback(ex);
}
}
void transcriptCallback(String transcript) {
bool shouldScroll =
_controller.position.pixels == _controller.position.maxScrollExtent;
setState(() {
transcriptText = transcriptText + transcript;
});
WidgetsBinding.instance.addPostFrameCallback((_) {
if (shouldScroll && !_controller.position.atEdge) {
_controller.jumpTo(_controller.position.maxScrollExtent);
}
});
}
void errorCallback(CheetahException error) {
setState(() {
isError = true;
errorMessage = error.message!;
});
}
Future<void> _startProcessing() async {
if (isProcessing) {
return;
}
try {
await _cheetahManager!.startProcess();
setState(() {
transcriptText = "";
isProcessing = true;
});
} on CheetahException catch (ex) {
errorCallback(ex);
}
}
Future<void> _stopProcessing() async {
if (!isProcessing) {
return;
}
try {
await _cheetahManager!.stopProcess();
setState(() {
isProcessing = false;
});
} on CheetahException catch (ex) {
errorCallback(ex);
}
}
Color picoBlue = Color.fromRGBO(55, 125, 255, 1);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
key: _scaffoldKey,
appBar: AppBar(
title: const Text('Cheetah Demo'),
backgroundColor: picoBlue,
),
body: Column(
children: [
buildCheetahTextArea(context),
buildErrorMessage(context),
buildStartButton(context),
footer
],
),
),
);
}
buildStartButton(BuildContext context) {
final ButtonStyle buttonStyle = ElevatedButton.styleFrom(
backgroundColor: picoBlue,
shape: CircleBorder(),
textStyle: TextStyle(color: Colors.white));
return Expanded(
flex: 2,
child: Container(
child: SizedBox(
width: 130,
height: 130,
child: ElevatedButton(
style: buttonStyle,
onPressed: isError
? null
: isProcessing
? _stopProcessing
: _startProcessing,
child: Text(isProcessing ? "Stop" : "Start",
style: TextStyle(fontSize: 30)),
))),
);
}
buildCheetahTextArea(BuildContext context) {
return Expanded(
flex: 6,
child: Container(
alignment: Alignment.topCenter,
color: Color(0xff25187e),
margin: EdgeInsets.all(10),
child: SingleChildScrollView(
controller: _controller,
scrollDirection: Axis.vertical,
padding: EdgeInsets.all(10),
physics: RangeMaintainingScrollPhysics(),
child: Align(
alignment: Alignment.topLeft,
child: Text(
transcriptText,
textAlign: TextAlign.left,
style: TextStyle(color: Colors.white, fontSize: 20),
)))));
}
buildErrorMessage(BuildContext context) {
return Expanded(
flex: isError ? 2 : 0,
child: Container(
alignment: Alignment.center,
margin: EdgeInsets.only(left: 20, right: 20),
padding: EdgeInsets.all(5),
decoration: !isError
? null
: BoxDecoration(
color: Colors.red, borderRadius: BorderRadius.circular(5)),
child: !isError
? null
: Text(
errorMessage,
style: TextStyle(color: Colors.white, fontSize: 18),
)));
}
Widget footer = Expanded(
flex: 1,
child: Container(
alignment: Alignment.bottomCenter,
padding: EdgeInsets.only(bottom: 20),
child: const Text(
"Made in Vancouver, Canada by Picovoice",
style: TextStyle(color: Color(0xff666666)),
)));
}