Improve text input behavior: append recognition results to text field

- Change from replacing current text to appending final results
- Distinguish between real-time results (display only) and final results (append to text)
- Add proper spacing between appended text segments
- Maintain history of individual recognition segments
- Clear current text when starting new recording session
This commit is contained in:
Max 2025-09-09 11:08:44 +08:00
parent 36fa796c10
commit ea32370fcc
1 changed files with 19 additions and 10 deletions

View File

@ -15,7 +15,7 @@ class MyApp extends StatelessWidget {
title: 'YX ASR Example',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
useMaterial3: true,
useMaterial3: false, // Material 3
),
home: const SpeechRecognitionPage(),
);
@ -95,8 +95,6 @@ class _SpeechRecognitionPageState extends State<SpeechRecognitionPage> {
if (result.recognizedWords.isNotEmpty) {
print('📱 [Example] 实时识别: ${result.recognizedWords}');
_currentText = result.recognizedWords;
//
_updateTextController();
}
});
});
@ -534,12 +532,22 @@ class _SpeechRecognitionPageState extends State<SpeechRecognitionPage> {
print(
'📱 [Example] RecordingButton 接收到识别结果: "${result.recognizedWords}"');
setState(() {
//
if (result.recognizedWords.isNotEmpty) {
if (result.finalResult) {
//
print('📱 [Example] 最终识别结果,追加到文本框: ${result.recognizedWords}');
String currentTextInBox = _textController.text;
if (currentTextInBox.isNotEmpty &&
!currentTextInBox.endsWith(' ')) {
currentTextInBox += ' '; //
}
_textController.text = currentTextInBox + result.recognizedWords;
_currentText = result.recognizedWords; //
} else {
//
print('📱 [Example] 实时识别: ${result.recognizedWords}');
_currentText = result.recognizedWords;
//
_updateTextController();
}
}
});
},
@ -555,7 +563,7 @@ class _SpeechRecognitionPageState extends State<SpeechRecognitionPage> {
});
if (!isListening) {
//
//
if (_currentText.isNotEmpty) {
setState(() {
_recognitionHistory.insert(0, _currentText);
@ -566,9 +574,10 @@ class _SpeechRecognitionPageState extends State<SpeechRecognitionPage> {
});
}
} else {
//
//
setState(() {
_realtimeResults.clear();
_currentText = ''; //
});
}
},