init
This commit is contained in:
405
client/lib/features/ai/pages/ai_main_page.dart
Normal file
405
client/lib/features/ai/pages/ai_main_page.dart
Normal file
@@ -0,0 +1,405 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'ai_writing_page.dart';
|
||||
import 'ai_speaking_page.dart';
|
||||
import '../../../core/network/ai_api_service.dart';
|
||||
|
||||
/// AI功能主页面
|
||||
class AIMainPage extends StatefulWidget {
|
||||
const AIMainPage({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<AIMainPage> createState() => _AIMainPageState();
|
||||
}
|
||||
|
||||
class _AIMainPageState extends State<AIMainPage> {
|
||||
final AIApiService _aiApiService = AIApiService();
|
||||
Map<String, dynamic>? _usageStats;
|
||||
bool _isLoadingStats = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadUsageStats();
|
||||
}
|
||||
|
||||
Future<void> _loadUsageStats() async {
|
||||
setState(() {
|
||||
_isLoadingStats = true;
|
||||
});
|
||||
|
||||
try {
|
||||
final stats = await _aiApiService.getAIUsageStats();
|
||||
setState(() {
|
||||
_usageStats = stats;
|
||||
_isLoadingStats = false;
|
||||
});
|
||||
} catch (e) {
|
||||
setState(() {
|
||||
_isLoadingStats = false;
|
||||
});
|
||||
// 静默处理错误,不影响用户体验
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('AI智能助手'),
|
||||
backgroundColor: Theme.of(context).primaryColor,
|
||||
foregroundColor: Colors.white,
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.refresh),
|
||||
onPressed: _loadUsageStats,
|
||||
tooltip: '刷新统计',
|
||||
),
|
||||
],
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 欢迎卡片
|
||||
Card(
|
||||
elevation: 4,
|
||||
child: Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.all(20.0),
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
gradient: LinearGradient(
|
||||
colors: [
|
||||
Theme.of(context).primaryColor,
|
||||
Theme.of(context).primaryColor.withOpacity(0.8),
|
||||
],
|
||||
begin: Alignment.topLeft,
|
||||
end: Alignment.bottomRight,
|
||||
),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.psychology,
|
||||
color: Colors.white,
|
||||
size: 32,
|
||||
),
|
||||
SizedBox(width: 12),
|
||||
Text(
|
||||
'AI智能助手',
|
||||
style: TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 24,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
const Text(
|
||||
'让AI帮助您提升英语学习效果',
|
||||
style: TextStyle(
|
||||
color: Colors.white70,
|
||||
fontSize: 16,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Row(
|
||||
children: [
|
||||
_buildFeatureChip('智能批改', Icons.edit),
|
||||
const SizedBox(width: 8),
|
||||
_buildFeatureChip('口语评估', Icons.mic),
|
||||
const SizedBox(width: 8),
|
||||
_buildFeatureChip('个性推荐', Icons.recommend),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
|
||||
// 功能模块
|
||||
const Text(
|
||||
'AI功能',
|
||||
style: TextStyle(
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 功能卡片网格
|
||||
GridView.count(
|
||||
shrinkWrap: true,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
crossAxisCount: 2,
|
||||
crossAxisSpacing: 16,
|
||||
mainAxisSpacing: 16,
|
||||
childAspectRatio: 1.2,
|
||||
children: [
|
||||
_buildFeatureCard(
|
||||
title: '写作批改',
|
||||
subtitle: 'AI智能批改英文写作',
|
||||
icon: Icons.edit_note,
|
||||
color: Colors.blue,
|
||||
onTap: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => const AIWritingPage(),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
_buildFeatureCard(
|
||||
title: '口语评估',
|
||||
subtitle: 'AI评估发音和流利度',
|
||||
icon: Icons.record_voice_over,
|
||||
color: Colors.green,
|
||||
onTap: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => const AISpeakingPage(),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
_buildFeatureCard(
|
||||
title: '智能推荐',
|
||||
subtitle: '个性化学习内容推荐',
|
||||
icon: Icons.lightbulb,
|
||||
color: Colors.orange,
|
||||
onTap: () {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('智能推荐功能即将上线')),
|
||||
);
|
||||
},
|
||||
),
|
||||
_buildFeatureCard(
|
||||
title: '练习生成',
|
||||
subtitle: 'AI生成个性化练习题',
|
||||
icon: Icons.quiz,
|
||||
color: Colors.purple,
|
||||
onTap: () {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('练习生成功能即将上线')),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
|
||||
// 使用统计
|
||||
if (_usageStats != null)
|
||||
Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Row(
|
||||
children: [
|
||||
Icon(Icons.analytics, color: Colors.blue),
|
||||
SizedBox(width: 8),
|
||||
Text(
|
||||
'使用统计',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: _buildStatItem(
|
||||
'写作批改',
|
||||
'${_usageStats!['writing_corrections'] ?? 0}',
|
||||
Icons.edit,
|
||||
Colors.blue,
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: _buildStatItem(
|
||||
'口语评估',
|
||||
'${_usageStats!['speaking_evaluations'] ?? 0}',
|
||||
Icons.mic,
|
||||
Colors.green,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: _buildStatItem(
|
||||
'总使用次数',
|
||||
'${_usageStats!['total_usage'] ?? 0}',
|
||||
Icons.trending_up,
|
||||
Colors.orange,
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: _buildStatItem(
|
||||
'本月使用',
|
||||
'${_usageStats!['monthly_usage'] ?? 0}',
|
||||
Icons.calendar_month,
|
||||
Colors.purple,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
// 加载统计时的占位符
|
||||
if (_isLoadingStats)
|
||||
const Card(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(16.0),
|
||||
child: Center(
|
||||
child: Column(
|
||||
children: [
|
||||
CircularProgressIndicator(),
|
||||
SizedBox(height: 8),
|
||||
Text('加载使用统计中...'),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildFeatureChip(String label, IconData icon) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white.withOpacity(0.2),
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(icon, color: Colors.white, size: 16),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
label,
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildFeatureCard({
|
||||
required String title,
|
||||
required String subtitle,
|
||||
required IconData icon,
|
||||
required Color color,
|
||||
required VoidCallback onTap,
|
||||
}) {
|
||||
return Card(
|
||||
elevation: 2,
|
||||
child: InkWell(
|
||||
onTap: onTap,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: color.withOpacity(0.1),
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
child: Icon(
|
||||
icon,
|
||||
color: color,
|
||||
size: 32,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Text(
|
||||
title,
|
||||
style: const TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
subtitle,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.grey.shade600,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStatItem(
|
||||
String label,
|
||||
String value,
|
||||
IconData icon,
|
||||
Color color,
|
||||
) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
margin: const EdgeInsets.symmetric(horizontal: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: color.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
Icon(icon, color: color, size: 24),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
value,
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: color,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
label,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.grey.shade600,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
475
client/lib/features/ai/pages/ai_speaking_page.dart
Normal file
475
client/lib/features/ai/pages/ai_speaking_page.dart
Normal file
@@ -0,0 +1,475 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'dart:io';
|
||||
import '../../../core/network/ai_api_service.dart';
|
||||
|
||||
/// AI口语评估页面
|
||||
class AISpeakingPage extends StatefulWidget {
|
||||
const AISpeakingPage({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<AISpeakingPage> createState() => _AISpeakingPageState();
|
||||
}
|
||||
|
||||
class _AISpeakingPageState extends State<AISpeakingPage> {
|
||||
final AIApiService _aiApiService = AIApiService();
|
||||
|
||||
bool _isRecording = false;
|
||||
bool _isLoading = false;
|
||||
File? _audioFile;
|
||||
Map<String, dynamic>? _evaluationResult;
|
||||
String? _error;
|
||||
String _selectedTask = 'pronunciation';
|
||||
|
||||
final List<Map<String, String>> _taskTypes = [
|
||||
{'value': 'pronunciation', 'label': '发音评估'},
|
||||
{'value': 'fluency', 'label': '流利度评估'},
|
||||
{'value': 'conversation', 'label': '对话评估'},
|
||||
{'value': 'presentation', 'label': '演讲评估'},
|
||||
];
|
||||
|
||||
Future<void> _startRecording() async {
|
||||
// 这里应该集成录音功能
|
||||
// 暂时模拟录音状态
|
||||
setState(() {
|
||||
_isRecording = true;
|
||||
_error = null;
|
||||
});
|
||||
|
||||
// 模拟录音过程
|
||||
await Future.delayed(const Duration(seconds: 2));
|
||||
|
||||
setState(() {
|
||||
_isRecording = false;
|
||||
// 模拟生成音频文件
|
||||
_audioFile = File('/tmp/mock_audio.wav');
|
||||
});
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('录音完成,可以开始评估')),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _stopRecording() async {
|
||||
setState(() {
|
||||
_isRecording = false;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _submitForEvaluation() async {
|
||||
if (_audioFile == null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('请先录制音频')),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
_error = null;
|
||||
_evaluationResult = null;
|
||||
});
|
||||
|
||||
try {
|
||||
final result = await _aiApiService.evaluateSpeaking(
|
||||
audioText: '模拟音频转文本结果', // 实际应该是音频转文本的结果
|
||||
prompt: '请评估这段英语口语的$_selectedTask',
|
||||
);
|
||||
|
||||
setState(() {
|
||||
_evaluationResult = result;
|
||||
_isLoading = false;
|
||||
});
|
||||
} catch (e) {
|
||||
setState(() {
|
||||
_error = e.toString();
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void _clearRecording() {
|
||||
setState(() {
|
||||
_audioFile = null;
|
||||
_evaluationResult = null;
|
||||
_error = null;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('AI口语评估'),
|
||||
backgroundColor: Theme.of(context).primaryColor,
|
||||
foregroundColor: Colors.white,
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.clear),
|
||||
onPressed: _clearRecording,
|
||||
tooltip: '清空录音',
|
||||
),
|
||||
],
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 任务类型选择
|
||||
Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'评估类型',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
children: _taskTypes.map((type) {
|
||||
final isSelected = _selectedTask == type['value'];
|
||||
return ChoiceChip(
|
||||
label: Text(type['label']!),
|
||||
selected: isSelected,
|
||||
onSelected: (selected) {
|
||||
if (selected) {
|
||||
setState(() {
|
||||
_selectedTask = type['value']!;
|
||||
});
|
||||
}
|
||||
},
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 录音控制区域
|
||||
Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'录音控制',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 录音按钮
|
||||
Center(
|
||||
child: Column(
|
||||
children: [
|
||||
GestureDetector(
|
||||
onTap: _isRecording ? _stopRecording : _startRecording,
|
||||
child: Container(
|
||||
width: 120,
|
||||
height: 120,
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
color: _isRecording
|
||||
? Colors.red.shade400
|
||||
: Theme.of(context).primaryColor,
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: (_isRecording
|
||||
? Colors.red.shade400
|
||||
: Theme.of(context).primaryColor)
|
||||
.withOpacity(0.3),
|
||||
spreadRadius: _isRecording ? 10 : 5,
|
||||
blurRadius: 20,
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Icon(
|
||||
_isRecording ? Icons.stop : Icons.mic,
|
||||
size: 50,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
_isRecording ? '点击停止录音' : '点击开始录音',
|
||||
style: const TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
if (_audioFile != null)
|
||||
Container(
|
||||
margin: const EdgeInsets.only(top: 12),
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 16,
|
||||
vertical: 8,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.green.shade50,
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
border: Border.all(
|
||||
color: Colors.green.shade200,
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.check_circle,
|
||||
color: Colors.green.shade600,
|
||||
size: 16,
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
const Text(
|
||||
'录音完成',
|
||||
style: TextStyle(fontSize: 12),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 评估按钮
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: ElevatedButton(
|
||||
onPressed: (_audioFile != null && !_isLoading)
|
||||
? _submitForEvaluation
|
||||
: null,
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Theme.of(context).primaryColor,
|
||||
foregroundColor: Colors.white,
|
||||
padding: const EdgeInsets.symmetric(vertical: 16),
|
||||
),
|
||||
child: _isLoading
|
||||
? const Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
|
||||
),
|
||||
),
|
||||
SizedBox(width: 8),
|
||||
Text('AI评估中...'),
|
||||
],
|
||||
)
|
||||
: const Text(
|
||||
'开始AI评估',
|
||||
style: TextStyle(fontSize: 16),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 错误显示
|
||||
if (_error != null)
|
||||
Card(
|
||||
color: Colors.red.shade50,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(Icons.error, color: Colors.red.shade700),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
_error!,
|
||||
style: TextStyle(color: Colors.red.shade700),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
// 评估结果显示
|
||||
if (_evaluationResult != null)
|
||||
Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.assessment,
|
||||
color: Colors.blue.shade600,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
const Text(
|
||||
'AI评估结果',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 总体评分
|
||||
if (_evaluationResult!['overall_score'] != null)
|
||||
Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.blue.shade50,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
const Icon(Icons.star, color: Colors.amber),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
'总体评分: ${_evaluationResult!['overall_score']}/100',
|
||||
style: const TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
|
||||
// 各项评分
|
||||
if (_evaluationResult!['scores'] != null)
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'详细评分:',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
...(_evaluationResult!['scores'] as Map<String, dynamic>)
|
||||
.entries
|
||||
.map(
|
||||
(entry) => Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 4),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(
|
||||
_getScoreLabel(entry.key),
|
||||
style: const TextStyle(fontSize: 14),
|
||||
),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 8,
|
||||
vertical: 2,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: _getScoreColor(entry.value),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Text(
|
||||
'${entry.value}/100',
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
// 改进建议
|
||||
if (_evaluationResult!['suggestions'] != null)
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const SizedBox(height: 16),
|
||||
const Text(
|
||||
'改进建议:',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.green.shade50,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(
|
||||
color: Colors.green.shade200,
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
_evaluationResult!['suggestions'].toString(),
|
||||
style: const TextStyle(fontSize: 14),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _getScoreLabel(String key) {
|
||||
switch (key) {
|
||||
case 'pronunciation':
|
||||
return '发音准确度';
|
||||
case 'fluency':
|
||||
return '流利度';
|
||||
case 'grammar':
|
||||
return '语法正确性';
|
||||
case 'vocabulary':
|
||||
return '词汇丰富度';
|
||||
case 'coherence':
|
||||
return '连贯性';
|
||||
default:
|
||||
return key;
|
||||
}
|
||||
}
|
||||
|
||||
Color _getScoreColor(dynamic score) {
|
||||
final scoreValue = score is int ? score : int.tryParse(score.toString()) ?? 0;
|
||||
if (scoreValue >= 80) {
|
||||
return Colors.green;
|
||||
} else if (scoreValue >= 60) {
|
||||
return Colors.orange;
|
||||
} else {
|
||||
return Colors.red;
|
||||
}
|
||||
}
|
||||
}
|
||||
351
client/lib/features/ai/pages/ai_writing_page.dart
Normal file
351
client/lib/features/ai/pages/ai_writing_page.dart
Normal file
@@ -0,0 +1,351 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../../core/network/ai_api_service.dart';
|
||||
|
||||
/// AI写作批改页面
|
||||
class AIWritingPage extends StatefulWidget {
|
||||
const AIWritingPage({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<AIWritingPage> createState() => _AIWritingPageState();
|
||||
}
|
||||
|
||||
class _AIWritingPageState extends State<AIWritingPage> {
|
||||
final TextEditingController _contentController = TextEditingController();
|
||||
final AIApiService _aiApiService = AIApiService();
|
||||
|
||||
String _selectedType = 'essay';
|
||||
bool _isLoading = false;
|
||||
Map<String, dynamic>? _correctionResult;
|
||||
String? _error;
|
||||
|
||||
final List<Map<String, String>> _writingTypes = [
|
||||
{'value': 'essay', 'label': '议论文'},
|
||||
{'value': 'email', 'label': '邮件'},
|
||||
{'value': 'report', 'label': '报告'},
|
||||
{'value': 'letter', 'label': '信件'},
|
||||
{'value': 'story', 'label': '故事'},
|
||||
];
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_contentController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _submitForCorrection() async {
|
||||
if (_contentController.text.trim().isEmpty) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('请输入要批改的内容')),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
_error = null;
|
||||
_correctionResult = null;
|
||||
});
|
||||
|
||||
try {
|
||||
final result = await _aiApiService.correctWriting(
|
||||
content: _contentController.text.trim(),
|
||||
taskType: _selectedType,
|
||||
);
|
||||
|
||||
setState(() {
|
||||
_correctionResult = result;
|
||||
_isLoading = false;
|
||||
});
|
||||
} catch (e) {
|
||||
setState(() {
|
||||
_error = e.toString();
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void _clearContent() {
|
||||
setState(() {
|
||||
_contentController.clear();
|
||||
_correctionResult = null;
|
||||
_error = null;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('AI写作批改'),
|
||||
backgroundColor: Theme.of(context).primaryColor,
|
||||
foregroundColor: Colors.white,
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.clear),
|
||||
onPressed: _clearContent,
|
||||
tooltip: '清空内容',
|
||||
),
|
||||
],
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 写作类型选择
|
||||
Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'写作类型',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
children: _writingTypes.map((type) {
|
||||
final isSelected = _selectedType == type['value'];
|
||||
return ChoiceChip(
|
||||
label: Text(type['label']!),
|
||||
selected: isSelected,
|
||||
onSelected: (selected) {
|
||||
if (selected) {
|
||||
setState(() {
|
||||
_selectedType = type['value']!;
|
||||
});
|
||||
}
|
||||
},
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 内容输入区域
|
||||
Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'写作内容',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextField(
|
||||
controller: _contentController,
|
||||
maxLines: 10,
|
||||
decoration: const InputDecoration(
|
||||
hintText: '请输入您的英文写作内容...',
|
||||
border: OutlineInputBorder(),
|
||||
contentPadding: EdgeInsets.all(12),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: ElevatedButton(
|
||||
onPressed: _isLoading ? null : _submitForCorrection,
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Theme.of(context).primaryColor,
|
||||
foregroundColor: Colors.white,
|
||||
padding: const EdgeInsets.symmetric(vertical: 16),
|
||||
),
|
||||
child: _isLoading
|
||||
? const Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
|
||||
),
|
||||
),
|
||||
SizedBox(width: 8),
|
||||
Text('AI批改中...'),
|
||||
],
|
||||
)
|
||||
: const Text(
|
||||
'开始AI批改',
|
||||
style: TextStyle(fontSize: 16),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 错误显示
|
||||
if (_error != null)
|
||||
Card(
|
||||
color: Colors.red.shade50,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(Icons.error, color: Colors.red.shade700),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
_error!,
|
||||
style: TextStyle(color: Colors.red.shade700),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
// 批改结果显示
|
||||
if (_correctionResult != null)
|
||||
Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.check_circle,
|
||||
color: Colors.green.shade600,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
const Text(
|
||||
'AI批改结果',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 总体评分
|
||||
if (_correctionResult!['score'] != null)
|
||||
Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.blue.shade50,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
const Icon(Icons.star, color: Colors.amber),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
'总体评分: ${_correctionResult!['score']}/100',
|
||||
style: const TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
|
||||
// 批改建议
|
||||
if (_correctionResult!['suggestions'] != null)
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'批改建议:',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
_correctionResult!['suggestions'].toString(),
|
||||
style: const TextStyle(fontSize: 14),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
// 错误列表
|
||||
if (_correctionResult!['errors'] != null)
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const SizedBox(height: 16),
|
||||
const Text(
|
||||
'发现的错误:',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
...(_correctionResult!['errors'] as List).map(
|
||||
(error) => Container(
|
||||
margin: const EdgeInsets.only(bottom: 8),
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.orange.shade50,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(
|
||||
color: Colors.orange.shade200,
|
||||
),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'错误类型: ${error['type'] ?? '未知'}',
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.orange.shade700,
|
||||
),
|
||||
),
|
||||
if (error['original'] != null)
|
||||
Text('原文: ${error['original']}'),
|
||||
if (error['corrected'] != null)
|
||||
Text(
|
||||
'建议: ${error['corrected']}',
|
||||
style: TextStyle(
|
||||
color: Colors.green.shade700,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
if (error['explanation'] != null)
|
||||
Text(
|
||||
'说明: ${error['explanation']}',
|
||||
style: const TextStyle(fontSize: 12),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user