From d1ab67e60e157c760334644ccf8d19ba2e82d2fb Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 9 Sep 2025 11:22:37 +0800 Subject: [PATCH] Fix real-time speech recognition display 1. Update UI to show actual _currentText instead of fixed 'realtime recognizing...' text - Use Flexible widget to handle text overflow - Show actual recognition text when available - Fallback to 'realtime recognizing...' when text is empty 2. Fix final result processing logic - Don't update _currentText when processing final results - Final results only append to text field, don't interfere with real-time display 3. Improve listening status management - Clear _currentText when recording stops - Ensure clean state transitions between recording sessions This fixes the issue where users could only see final results without real-time feedback. --- example/lib/main.dart | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index fbf30da..ac7e671 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -359,12 +359,16 @@ class _SpeechRecognitionPageState extends State { children: [ Icon(Icons.mic, size: 14, color: Colors.blue[600]), const SizedBox(width: 4), - Text( - '实时识别中...', - style: TextStyle( - fontSize: 12, - color: Colors.blue[600], - fontWeight: FontWeight.w500, + Flexible( + child: Text( + _currentText.isEmpty ? '实时识别中...' : _currentText, + style: TextStyle( + fontSize: 12, + color: Colors.blue[600], + fontWeight: FontWeight.w500, + ), + overflow: TextOverflow.ellipsis, + maxLines: 1, ), ), ], @@ -476,7 +480,7 @@ class _SpeechRecognitionPageState extends State { currentTextInBox += ' '; // 添加空格分隔 } _textController.text = currentTextInBox + result.recognizedWords; - _currentText = result.recognizedWords; // 保存当前识别结果 + // 最终结果不需要更新_currentText,因为录音已结束 } else { // 实时结果:仅用于显示,不追加到文本框 print('📱 [Example] 实时识别: ${result.recognizedWords}'); @@ -505,6 +509,8 @@ class _SpeechRecognitionPageState extends State { if (_recognitionHistory.length > 10) { _recognitionHistory.removeLast(); } + // 清空当前文本,因为录音已结束 + _currentText = ''; }); } } else {