import 'package:flutter/material.dart'; import 'package:get/get.dart'; import '../controllers/azure_asr_test_controller.dart'; import '../../../data/services/azure_asr_service.dart'; class AzureAsrTestView extends GetView { const AzureAsrTestView({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Azure语音识别测试'), actions: [ _buildLanguageDropdown(), ], ), body: Padding( padding: const EdgeInsets.all(16.0), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ _buildStatusCard(), const SizedBox(height: 16), _buildRecognitionResultCard(), const SizedBox(height: 16), _buildControlButtons(context), ], ), ), ); } // 构建状态卡片 Widget _buildStatusCard() { return Card( child: Padding( padding: const EdgeInsets.all(16.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Text( '状态信息', style: TextStyle( fontSize: 18, fontWeight: FontWeight.bold, ), ), const SizedBox(height: 8), Obx(() => Text('识别状态: ${_getStateText(controller.asrState.value)}')), Obx(() => Text('是否正在监听: ${controller.isListening.value ? "是" : "否"}')), Obx(() => Text('当前语言: ${controller.selectedLanguage.value}')), ], ), ), ); } // 构建识别结果卡片 Widget _buildRecognitionResultCard() { return Expanded( child: Card( child: Padding( padding: const EdgeInsets.all(16.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Text( '识别结果', style: TextStyle( fontSize: 18, fontWeight: FontWeight.bold, ), ), const SizedBox(height: 8), const Text('部分识别结果:'), Obx(() => Text( controller.partialText.value, style: const TextStyle(color: Colors.blue), )), const SizedBox(height: 16), const Text('最终识别结果:'), Expanded( child: Obx(() => SingleChildScrollView( child: Text( controller.recognizedText.value, style: const TextStyle( fontWeight: FontWeight.bold, fontSize: 16, ), ), )), ), ], ), ), ), ); } // 构建控制按钮 Widget _buildControlButtons(BuildContext context) { return Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ Expanded( child: Obx(() => ElevatedButton( onPressed: controller.isListening.value ? null : controller.startSimpleRecognition, child: const Text('简单识别'), )), ), const SizedBox(width: 16), Expanded( child: Obx(() => ElevatedButton( onPressed: controller.toggleContinuousRecognition, style: ElevatedButton.styleFrom( backgroundColor: controller.isListening.value ? Colors.red : Theme.of(context).primaryColor, ), child: Text( controller.isListening.value ? '停止连续识别' : '开始连续识别', ), )), ), ], ); } // 构建语言下拉菜单 Widget _buildLanguageDropdown() { return Padding( padding: const EdgeInsets.only(right: 16.0), child: Obx(() => DropdownButton( value: controller.selectedLanguage.value, onChanged: controller.isListening.value ? null : (value) { if (value != null) { controller.changeLanguage(value); } }, items: controller.availableLanguages .map>((String value) { return DropdownMenuItem( value: value, child: Text(value), ); }).toList(), )), ); } // 获取状态文本 String _getStateText(AzureAsrState state) { switch (state) { case AzureAsrState.notInitialized: return '未初始化'; case AzureAsrState.initialized: return '已初始化'; case AzureAsrState.listening: return '正在监听'; case AzureAsrState.notListening: return '未监听'; case AzureAsrState.error: return '错误'; default: return '未知'; } } }