ProviderDemoApp.dart
5 KB
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
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:one_key/pages/home/RecognitionResultsWidget.dart';
import 'package:provider/provider.dart';
import 'package:speech_to_text/speech_to_text.dart';
import 'package:speech_to_text/speech_to_text_provider.dart';
class ProviderDemoApp extends StatefulWidget {
const ProviderDemoApp({super.key});
@override
State<ProviderDemoApp> createState() => _ProviderDemoAppState();
}
class _ProviderDemoAppState extends State<ProviderDemoApp> {
final SpeechToText speech = SpeechToText();
late SpeechToTextProvider speechProvider;
@override
void initState() {
super.initState();
speechProvider = SpeechToTextProvider(speech);
initSpeechState();
}
Future<void> initSpeechState() async {
bool ggggg = await speechProvider.initialize();
print(ggggg);
}
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<SpeechToTextProvider>.value(
value: speechProvider,
child: MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('标题'),
),
body: const SpeechProviderExampleWidget(),
),
),
);
}
}
class SpeechProviderExampleWidget extends StatefulWidget {
const SpeechProviderExampleWidget({super.key});
@override
SpeechProviderExampleWidgetState createState() =>
SpeechProviderExampleWidgetState();
}
class SpeechProviderExampleWidgetState
extends State<SpeechProviderExampleWidget> {
String _currentLocaleId = '';
void _setCurrentLocale(SpeechToTextProvider speechProvider) {
if (speechProvider.isAvailable && _currentLocaleId.isEmpty) {
_currentLocaleId = speechProvider.systemLocale?.localeId ?? '';
}
}
@override
Widget build(BuildContext context) {
var speechProvider = Provider.of<SpeechToTextProvider>(context);
if (speechProvider.isNotAvailable) {
return const Center(
child: Text(
'Speech recognition not available, no permission or not available on the device.'),
);
}
_setCurrentLocale(speechProvider);
return Column(children: [
const Center(
child: Text(
'Speech recognition available',
style: TextStyle(fontSize: 22.0),
),
),
Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
TextButton(
onPressed:
!speechProvider.isAvailable || speechProvider.isListening
? null
: () => speechProvider.listen(
partialResults: true, localeId: _currentLocaleId),
child: const Text('Start'),
),
TextButton(
onPressed: speechProvider.isListening
? () => speechProvider.stop()
: null,
child: const Text('Stop'),
),
TextButton(
onPressed: speechProvider.isListening
? () => speechProvider.cancel()
: null,
child: const Text('Cancel'),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
DropdownButton(
onChanged: (selectedVal) => _switchLang(selectedVal),
value: _currentLocaleId,
items: speechProvider.locales
.map(
(localeName) => DropdownMenuItem(
value: localeName.localeId,
child: Text(localeName.name),
),
)
.toList(),
),
],
)
],
),
const Expanded(
flex: 4,
child: RecognitionResultsWidget(),
),
Expanded(
flex: 1,
child: Column(
children: <Widget>[
const Center(
child: Text(
'Error Status',
style: TextStyle(fontSize: 22.0),
),
),
Center(
child: speechProvider.hasError
? Text(speechProvider.lastError!.errorMsg)
: Container(),
),
],
),
),
Container(
padding: const EdgeInsets.symmetric(vertical: 20),
color: Theme.of(context).colorScheme.surface,
child: Center(
child: speechProvider.isListening
? const Text(
"I'm listening...",
style: TextStyle(fontWeight: FontWeight.bold),
)
: const Text(
'Not listening',
style: TextStyle(fontWeight: FontWeight.bold),
),
),
),
]);
}
void _switchLang(selectedVal) {
setState(() {
_currentLocaleId = selectedVal;
});
debugPrint(selectedVal);
}
}