import 'dart:async'; import 'package:flutter/material.dart'; import 'package:speech_to_text_dialog/speech_to_text_dialog.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatefulWidget { const MyApp({super.key}); @override State createState() => _MyAppState(); } class _MyAppState extends State { final _speechToTextDialog = SpeechToTextDialog(); StreamSubscription? _textSubscription; String _recognizedText = 'Tap the button to start speaking...'; String _selectedLocale = 'en-US'; final List _locales = [ 'en-US', 'de-DE', 'es-ES', 'fr-FR', 'it-IT', 'pt-BR', 'ru-RU', 'zh-CN', 'ja-JP', 'ko-KR', ]; @override void initState() { super.initState(); // Listen for recognized text _textSubscription = _speechToTextDialog.textStream.listen((text) { setState(() { if (text.isEmpty) { _recognizedText = 'No speech detected or cancelled'; } else { _recognizedText = text; } }); }); } @override void dispose() { unawaited(_textSubscription?.cancel()); _speechToTextDialog.dispose(); super.dispose(); } Future _startListening() async { setState(() { _recognizedText = 'Listening...'; }); final success = await _speechToTextDialog.showDialog( locale: _selectedLocale, ); if (!success) { setState(() { _recognizedText = 'Speech recognition not available'; }); } } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: const Text('Speech to Text Dialog'), backgroundColor: Theme.of(context).colorScheme.inversePrimary, ), body: Padding( padding: const EdgeInsets.all(16.0), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Icon(Icons.mic, size: 80, color: Colors.blue), const SizedBox(height: 24), Text( _recognizedText, style: Theme.of(context).textTheme.headlineSmall, textAlign: TextAlign.center, ), const SizedBox(height: 32), DropdownButton( value: _selectedLocale, hint: const Text('Select Language'), items: _locales.map((String locale) { return DropdownMenuItem( value: locale, child: Text(locale), ); }).toList(), onChanged: (String? newValue) { if (newValue != null) { setState(() { _selectedLocale = newValue; }); } }, ), const SizedBox(height: 16), ElevatedButton.icon( onPressed: _startListening, icon: const Icon(Icons.mic), label: const Text('Start Speech Recognition'), style: ElevatedButton.styleFrom( padding: const EdgeInsets.symmetric( horizontal: 32, vertical: 16, ), ), ), ], ), ), ), ); } }