78 lines
1.9 KiB
Dart
78 lines
1.9 KiB
Dart
// ignore_for_file: avoid_print, library_private_types_in_public_api, depend_on_referenced_packages
|
|
|
|
// For jsonEncode
|
|
import 'package:flutter/material.dart';
|
|
import 'package:speech_to_text/speech_to_text.dart' as stt;
|
|
|
|
class SpeechInputPage extends StatelessWidget {
|
|
const SpeechInputPage({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('Speech to Text')),
|
|
body: const Center(
|
|
child: SpeechInputWidget(),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class SpeechInputWidget extends StatefulWidget {
|
|
const SpeechInputWidget({super.key});
|
|
|
|
@override
|
|
State<SpeechInputWidget> createState() => _SpeechInputWidgetState();
|
|
}
|
|
|
|
class _SpeechInputWidgetState extends State<SpeechInputWidget> {
|
|
late stt.SpeechToText _speech;
|
|
bool _isListening = false;
|
|
String _spokenText = '';
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_speech = stt.SpeechToText();
|
|
}
|
|
|
|
void _listen() async {
|
|
if (!_isListening) {
|
|
bool available = await _speech.initialize(
|
|
onStatus: (val) => print('Status: $val'),
|
|
onError: (val) => print('Error: $val'),
|
|
);
|
|
if (available) {
|
|
setState(() => _isListening = true);
|
|
_speech.listen(
|
|
onResult: (val) {
|
|
setState(() {
|
|
_spokenText = val.recognizedWords;
|
|
});
|
|
},
|
|
);
|
|
}
|
|
} else {
|
|
setState(() => _isListening = false);
|
|
_speech.stop();
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Text(
|
|
_spokenText.isEmpty ? 'Say something…' : _spokenText,
|
|
style: const TextStyle(fontSize: 20),
|
|
),
|
|
const SizedBox(height: 20),
|
|
FloatingActionButton(
|
|
onPressed: _listen,
|
|
child: Icon(_isListening ? Icons.mic : Icons.mic_none),
|
|
)
|
|
],
|
|
);
|
|
}
|
|
}
|