Implement real-time text appending to input field

1. Add _baseText variable to track text before recording starts
   - Preserves existing text when starting new recording session
   - Provides foundation for real-time appending

2. Update real-time result processing logic
   - Real-time results now immediately update the text field
   - Combine base text + current recognition text
   - Move cursor to end for better UX
   - Add intelligent spacing between segments

3. Improve final result handling
   - Final results update base text for next recording
   - Clean state management between recording sessions

4. Update recording status change logic
   - Capture base text when recording starts
   - Maintain proper state transitions

Now users can see their speech being converted to text in real-time directly in the input field, providing immediate visual feedback during speech recognition.
This commit is contained in:
Max 2025-09-09 11:24:53 +08:00
parent d1ab67e60e
commit ed51fa89bd
1 changed files with 26 additions and 14 deletions

View File

@ -41,6 +41,9 @@ class _SpeechRecognitionPageState extends State<SpeechRecognitionPage> {
String _currentText = ''; String _currentText = '';
String _errorMessage = ''; String _errorMessage = '';
List<String> _recognitionHistory = []; List<String> _recognitionHistory = [];
///
String _baseText = '';
// //
final List<String> _realtimeResults = []; // final List<String> _realtimeResults = []; //
@ -155,6 +158,7 @@ class _SpeechRecognitionPageState extends State<SpeechRecognitionPage> {
_recognitionHistory.clear(); _recognitionHistory.clear();
_realtimeResults.clear(); _realtimeResults.clear();
_currentText = ''; _currentText = '';
_baseText = ''; //
_textController.clear(); _textController.clear();
_errorMessage = ''; _errorMessage = '';
}); });
@ -472,19 +476,27 @@ class _SpeechRecognitionPageState extends State<SpeechRecognitionPage> {
setState(() { setState(() {
if (result.recognizedWords.isNotEmpty) { if (result.recognizedWords.isNotEmpty) {
if (result.finalResult) { if (result.finalResult) {
// // base text为下次录音做准备
print('📱 [Example] 最终识别结果,追加到文本框: ${result.recognizedWords}'); print('📱 [Example] 最终识别结果,确认文本: ${result.recognizedWords}');
String currentTextInBox = _textController.text; _baseText = _textController.text; //
if (currentTextInBox.isNotEmpty && _currentText = ''; //
!currentTextInBox.endsWith(' ')) {
currentTextInBox += ' '; //
}
_textController.text = currentTextInBox + result.recognizedWords;
// _currentText
} else { } else {
// //
print('📱 [Example] 实时识别: ${result.recognizedWords}'); print('📱 [Example] 实时识别,更新输入框: ${result.recognizedWords}');
_currentText = result.recognizedWords; _currentText = result.recognizedWords;
// = +
String newText = _baseText;
if (newText.isNotEmpty && !newText.endsWith(' ') && _currentText.isNotEmpty) {
newText += ' '; //
}
newText += _currentText;
_textController.text = newText;
//
_textController.selection = TextSelection.fromPosition(
TextPosition(offset: newText.length),
);
} }
} }
}); });
@ -509,13 +521,13 @@ class _SpeechRecognitionPageState extends State<SpeechRecognitionPage> {
if (_recognitionHistory.length > 10) { if (_recognitionHistory.length > 10) {
_recognitionHistory.removeLast(); _recognitionHistory.removeLast();
} }
//
_currentText = '';
}); });
} }
// _currentText
} else { } else {
// //
setState(() { setState(() {
_baseText = _textController.text; //
_realtimeResults.clear(); _realtimeResults.clear();
_currentText = ''; // _currentText = ''; //
}); });