init
This commit is contained in:
@@ -0,0 +1,726 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../models/test_models.dart';
|
||||
import '../providers/test_riverpod_provider.dart';
|
||||
import '../screens/test_execution_screen.dart';
|
||||
import '../../auth/providers/auth_provider.dart' as auth;
|
||||
|
||||
/// 综合测试页面
|
||||
class ComprehensiveTestScreen extends ConsumerStatefulWidget {
|
||||
const ComprehensiveTestScreen({super.key});
|
||||
|
||||
@override
|
||||
ConsumerState<ComprehensiveTestScreen> createState() => _ComprehensiveTestScreenState();
|
||||
}
|
||||
|
||||
class _ComprehensiveTestScreenState extends ConsumerState<ComprehensiveTestScreen> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
// 加载测试模板和最近结果
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
ref.read(testProvider.notifier).loadTestTemplates();
|
||||
_loadRecentResults();
|
||||
});
|
||||
}
|
||||
|
||||
void _loadRecentResults() async {
|
||||
// 尝试获取认证状态,但不强制要求登录
|
||||
try {
|
||||
final authState = ref.read(auth.authProvider);
|
||||
final userId = authState.user?.id;
|
||||
if (userId != null) {
|
||||
ref.read(testProvider.notifier).loadRecentResults(userId: userId);
|
||||
} else {
|
||||
// 用户未登录时,显示模拟数据或空状态
|
||||
print('用户未登录,显示静态内容');
|
||||
}
|
||||
} catch (e) {
|
||||
// 认证服务出错时,继续显示静态内容
|
||||
print('认证服务错误: $e');
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final testState = ref.watch(testProvider);
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.grey[50],
|
||||
appBar: AppBar(
|
||||
title: const Text(
|
||||
'综合测试',
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
backgroundColor: const Color(0xFF2196F3),
|
||||
elevation: 0,
|
||||
centerTitle: true,
|
||||
),
|
||||
body: _buildBody(testState),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildBody(TestState testState) {
|
||||
// 如果正在加载,显示加载指示器
|
||||
if (testState.isLoading) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
// 如果有错误,显示错误信息
|
||||
if (testState.errorMessage != null) {
|
||||
return _buildErrorView(testState.errorMessage!);
|
||||
}
|
||||
|
||||
// 显示正常内容(静态展示,不强制登录)
|
||||
return SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildWelcomeSection(),
|
||||
const SizedBox(height: 24),
|
||||
_buildTestTypes(testState),
|
||||
const SizedBox(height: 32),
|
||||
_buildRecentResults(testState),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildErrorView(String errorMessage) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.error_outline,
|
||||
size: 64,
|
||||
color: Colors.red[300],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
errorMessage,
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
color: Colors.red[600],
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
ref.read(testProvider.notifier).loadTestTemplates();
|
||||
_loadRecentResults();
|
||||
},
|
||||
child: const Text('重试'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildWelcomeSection() {
|
||||
return Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.all(20),
|
||||
decoration: BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topLeft,
|
||||
end: Alignment.bottomRight,
|
||||
colors: [
|
||||
Colors.teal.withOpacity(0.1),
|
||||
Colors.teal.withOpacity(0.05),
|
||||
],
|
||||
),
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
border: Border.all(color: Colors.teal.withOpacity(0.2)),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.teal.withOpacity(0.2),
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
child: const Icon(
|
||||
Icons.quiz,
|
||||
color: Colors.teal,
|
||||
size: 24,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
const Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'全面能力评估',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.black87,
|
||||
),
|
||||
),
|
||||
SizedBox(height: 4),
|
||||
Text(
|
||||
'测试您的英语综合能力水平',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: Colors.grey,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTestTypes(TestState testState) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'测试类型',
|
||||
style: TextStyle(
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.black87,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
if (testState.templates.isEmpty)
|
||||
const Center(
|
||||
child: Text(
|
||||
'暂无可用的测试模板',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
color: Colors.grey,
|
||||
),
|
||||
),
|
||||
)
|
||||
else
|
||||
GridView.builder(
|
||||
shrinkWrap: true,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 2,
|
||||
crossAxisSpacing: 16,
|
||||
mainAxisSpacing: 16,
|
||||
childAspectRatio: 1.2,
|
||||
),
|
||||
itemCount: testState.templates.length,
|
||||
itemBuilder: (context, index) {
|
||||
final template = testState.templates[index];
|
||||
return _buildTestCard(
|
||||
template: template,
|
||||
onTap: () => _startTest(template),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTestCard({
|
||||
required TestTemplate template,
|
||||
required VoidCallback onTap,
|
||||
}) {
|
||||
final color = _getColorForTestType(template.type);
|
||||
final icon = _getIconForTestType(template.type);
|
||||
|
||||
return Card(
|
||||
elevation: 4,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
),
|
||||
child: InkWell(
|
||||
onTap: onTap,
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topLeft,
|
||||
end: Alignment.bottomRight,
|
||||
colors: [
|
||||
color.withOpacity(0.1),
|
||||
color.withOpacity(0.05),
|
||||
],
|
||||
),
|
||||
),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: color.withOpacity(0.2),
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
child: Icon(
|
||||
icon,
|
||||
size: 24,
|
||||
color: color,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Text(
|
||||
template.name,
|
||||
style: const TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.black87,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
'${template.duration}分钟',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.grey[600],
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
if (template.totalQuestions > 0)
|
||||
Text(
|
||||
'${template.totalQuestions}题',
|
||||
style: TextStyle(
|
||||
fontSize: 10,
|
||||
color: Colors.grey[500],
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildRecentResults(TestState testState) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'最近测试结果',
|
||||
style: TextStyle(
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.black87,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
if (testState.testResults.isEmpty)
|
||||
Card(
|
||||
elevation: 2,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: const Padding(
|
||||
padding: EdgeInsets.all(32),
|
||||
child: Center(
|
||||
child: Column(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.quiz_outlined,
|
||||
size: 48,
|
||||
color: Colors.grey,
|
||||
),
|
||||
SizedBox(height: 16),
|
||||
Text(
|
||||
'暂无测试记录',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
color: Colors.grey,
|
||||
),
|
||||
),
|
||||
SizedBox(height: 8),
|
||||
Text(
|
||||
'完成第一次测试后,结果将显示在这里',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: Colors.grey,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
else
|
||||
Card(
|
||||
elevation: 2,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
children: testState.testResults
|
||||
.asMap()
|
||||
.entries
|
||||
.map((entry) {
|
||||
final index = entry.key;
|
||||
final result = entry.value;
|
||||
return Column(
|
||||
children: [
|
||||
if (index > 0) const Divider(),
|
||||
_buildResultItem(result: result),
|
||||
],
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildResultItem({
|
||||
required TestResult result,
|
||||
}) {
|
||||
final template = ref.read(testProvider)
|
||||
.templates
|
||||
.firstWhere(
|
||||
(t) => t.id == result.testId,
|
||||
orElse: () => TestTemplate(
|
||||
id: result.testId,
|
||||
name: '未知测试',
|
||||
description: '',
|
||||
type: TestType.standard,
|
||||
duration: 0,
|
||||
totalQuestions: 0,
|
||||
skillDistribution: {},
|
||||
difficultyDistribution: {},
|
||||
questionIds: [],
|
||||
isActive: true,
|
||||
createdAt: DateTime.now(),
|
||||
updatedAt: DateTime.now(),
|
||||
),
|
||||
);
|
||||
|
||||
return InkWell(
|
||||
onTap: () => _showResultDetails(result),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||
child: Row(
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.teal.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: const Icon(
|
||||
Icons.quiz,
|
||||
color: Colors.teal,
|
||||
size: 20,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
template.name,
|
||||
style: const TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: Colors.black87,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
_formatDate(result.endTime),
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.grey[600],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
Text(
|
||||
'${result.totalScore.toStringAsFixed(0)}分',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: _getScoreColor(result.totalScore.toDouble()),
|
||||
),
|
||||
),
|
||||
Text(
|
||||
_getScoreLevel(result.totalScore.toDouble()),
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.grey[600],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Icon(
|
||||
Icons.chevron_right,
|
||||
color: Colors.grey[400],
|
||||
size: 20,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _startTest(TestTemplate template) {
|
||||
// 显示测试开始确认对话框
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: const Text('开始测试'),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text('确定要开始「${template.name}」吗?'),
|
||||
const SizedBox(height: 16),
|
||||
if (template.description.isNotEmpty) ...[
|
||||
Text(
|
||||
template.description,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: Colors.grey[600],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
],
|
||||
Row(
|
||||
children: [
|
||||
Icon(Icons.timer, size: 16, color: Colors.grey[600]),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
'${template.duration}分钟',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: Colors.grey[600],
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Icon(Icons.quiz, size: 16, color: Colors.grey[600]),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
'${template.totalQuestions}题',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: Colors.grey[600],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
child: const Text('取消'),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop();
|
||||
_navigateToTest(template);
|
||||
},
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.teal,
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
child: const Text('开始'),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void _navigateToTest(TestTemplate template) {
|
||||
// 导航到测试执行页面
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (context) => TestExecutionScreen(template: template),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _showResultDetails(TestResult result) {
|
||||
// 显示测试结果详情
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: const Text('测试结果详情'),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text('总分:${result.totalScore.toStringAsFixed(1)}'),
|
||||
const SizedBox(height: 8),
|
||||
Text('完成时间:${_formatDate(result.endTime)}'),
|
||||
const SizedBox(height: 8),
|
||||
Text('用时:${result.duration}秒'),
|
||||
if (result.skillScores.isNotEmpty) ...[
|
||||
const SizedBox(height: 16),
|
||||
const Text(
|
||||
'各项技能得分:',
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
...result.skillScores.map(
|
||||
(skillScore) => Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 2),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(_getSkillTypeName(skillScore.skillType)),
|
||||
Text('${skillScore.score}/${skillScore.maxScore}分'),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
child: const Text('关闭'),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Color _getColorForTestType(TestType type) {
|
||||
switch (type) {
|
||||
case TestType.quick:
|
||||
return Colors.blue;
|
||||
case TestType.standard:
|
||||
return Colors.green;
|
||||
case TestType.full:
|
||||
return Colors.orange;
|
||||
case TestType.mock:
|
||||
return Colors.purple;
|
||||
case TestType.vocabulary:
|
||||
return Colors.teal;
|
||||
case TestType.grammar:
|
||||
return Colors.indigo;
|
||||
case TestType.reading:
|
||||
return Colors.brown;
|
||||
case TestType.listening:
|
||||
return Colors.cyan;
|
||||
case TestType.speaking:
|
||||
return Colors.pink;
|
||||
case TestType.writing:
|
||||
return Colors.amber;
|
||||
}
|
||||
}
|
||||
|
||||
IconData _getIconForTestType(TestType type) {
|
||||
switch (type) {
|
||||
case TestType.quick:
|
||||
return Icons.flash_on;
|
||||
case TestType.standard:
|
||||
return Icons.school;
|
||||
case TestType.full:
|
||||
return Icons.quiz;
|
||||
case TestType.mock:
|
||||
return Icons.assignment;
|
||||
case TestType.vocabulary:
|
||||
return Icons.book;
|
||||
case TestType.grammar:
|
||||
return Icons.text_fields;
|
||||
case TestType.reading:
|
||||
return Icons.menu_book;
|
||||
case TestType.listening:
|
||||
return Icons.headphones;
|
||||
case TestType.speaking:
|
||||
return Icons.mic;
|
||||
case TestType.writing:
|
||||
return Icons.edit;
|
||||
}
|
||||
}
|
||||
|
||||
String _formatDate(DateTime date) {
|
||||
return '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
|
||||
}
|
||||
|
||||
Color _getScoreColor(double score) {
|
||||
if (score >= 90) return Colors.green;
|
||||
if (score >= 80) return Colors.blue;
|
||||
if (score >= 70) return Colors.orange;
|
||||
return Colors.red;
|
||||
}
|
||||
|
||||
String _getScoreLevel(double score) {
|
||||
if (score >= 90) return '优秀';
|
||||
if (score >= 80) return '良好';
|
||||
if (score >= 70) return '中等';
|
||||
if (score >= 60) return '及格';
|
||||
return '不及格';
|
||||
}
|
||||
|
||||
String _getSkillName(LanguageSkill skill) {
|
||||
switch (skill) {
|
||||
case LanguageSkill.listening:
|
||||
return '听力';
|
||||
case LanguageSkill.reading:
|
||||
return '阅读';
|
||||
case LanguageSkill.speaking:
|
||||
return '口语';
|
||||
case LanguageSkill.writing:
|
||||
return '写作';
|
||||
case LanguageSkill.vocabulary:
|
||||
return '词汇';
|
||||
case LanguageSkill.grammar:
|
||||
return '语法';
|
||||
case LanguageSkill.pronunciation:
|
||||
return '发音';
|
||||
case LanguageSkill.comprehension:
|
||||
return '理解';
|
||||
}
|
||||
}
|
||||
|
||||
String _getSkillTypeName(SkillType skillType) {
|
||||
switch (skillType) {
|
||||
case SkillType.vocabulary:
|
||||
return '词汇';
|
||||
case SkillType.grammar:
|
||||
return '语法';
|
||||
case SkillType.listening:
|
||||
return '听力';
|
||||
case SkillType.reading:
|
||||
return '阅读';
|
||||
case SkillType.speaking:
|
||||
return '口语';
|
||||
case SkillType.writing:
|
||||
return '写作';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,698 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'dart:async';
|
||||
import '../models/test_models.dart';
|
||||
import '../providers/test_riverpod_provider.dart';
|
||||
import '../data/test_static_data.dart';
|
||||
import '../widgets/question_widgets.dart';
|
||||
import 'test_result_screen.dart';
|
||||
|
||||
/// 测试执行页面
|
||||
class TestExecutionScreen extends ConsumerStatefulWidget {
|
||||
final TestTemplate template;
|
||||
|
||||
const TestExecutionScreen({
|
||||
super.key,
|
||||
required this.template,
|
||||
});
|
||||
|
||||
@override
|
||||
ConsumerState<TestExecutionScreen> createState() => _TestExecutionScreenState();
|
||||
}
|
||||
|
||||
class _TestExecutionScreenState extends ConsumerState<TestExecutionScreen> {
|
||||
Timer? _timer;
|
||||
int _timeRemaining = 0;
|
||||
bool _isTestStarted = false;
|
||||
bool _isTestCompleted = false;
|
||||
Map<String, UserAnswer> _userAnswers = {};
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_timeRemaining = widget.template.duration * 60; // 转换为秒
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
_startTest();
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_timer?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _startTest() async {
|
||||
setState(() {
|
||||
_isTestStarted = true;
|
||||
});
|
||||
|
||||
// 创建测试会话(使用静态数据)
|
||||
final questions = TestStaticData.getAllQuestions().take(10).toList(); // 取前10题作为测试
|
||||
final session = TestSession(
|
||||
id: 'test_session_${DateTime.now().millisecondsSinceEpoch}',
|
||||
templateId: widget.template.id,
|
||||
userId: 'current_user',
|
||||
status: TestStatus.inProgress,
|
||||
questions: questions,
|
||||
answers: [],
|
||||
currentQuestionIndex: 0,
|
||||
startTime: DateTime.now(),
|
||||
timeRemaining: _timeRemaining,
|
||||
metadata: {},
|
||||
);
|
||||
|
||||
// 更新provider状态
|
||||
ref.read(testProvider.notifier).setCurrentSession(session);
|
||||
|
||||
// 启动计时器
|
||||
_startTimer();
|
||||
}
|
||||
|
||||
void _startTimer() {
|
||||
_timer = Timer.periodic(const Duration(seconds: 1), (timer) {
|
||||
setState(() {
|
||||
if (_timeRemaining > 0) {
|
||||
_timeRemaining--;
|
||||
} else {
|
||||
_submitTest();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
void _submitTest() async {
|
||||
if (_isTestCompleted) return;
|
||||
|
||||
_timer?.cancel();
|
||||
setState(() {
|
||||
_isTestCompleted = true;
|
||||
});
|
||||
|
||||
// 获取当前会话和答案
|
||||
final currentSession = ref.read(testProvider).currentSession;
|
||||
if (currentSession == null) return;
|
||||
|
||||
// 创建测试结果
|
||||
final testResult = _createTestResult(currentSession);
|
||||
|
||||
// 更新provider状态
|
||||
ref.read(testProvider.notifier).setCurrentResult(testResult);
|
||||
|
||||
// 导航到结果页面
|
||||
if (mounted) {
|
||||
Navigator.of(context).pushReplacement(
|
||||
MaterialPageRoute(
|
||||
builder: (context) => TestResultScreen(
|
||||
testResult: testResult,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
TestResult _createTestResult(TestSession session) {
|
||||
final answers = session.answers;
|
||||
int totalScore = 0;
|
||||
int maxScore = 0;
|
||||
Map<SkillType, List<int>> skillScores = {};
|
||||
|
||||
// 计算得分
|
||||
for (final question in session.questions) {
|
||||
maxScore += question.points;
|
||||
final userAnswer = answers.where((a) => a.questionId == question.id).firstOrNull;
|
||||
|
||||
if (userAnswer != null) {
|
||||
// 简单的评分逻辑
|
||||
bool isCorrect = false;
|
||||
if (question.type == QuestionType.multipleChoice) {
|
||||
isCorrect = userAnswer.selectedAnswers.isNotEmpty &&
|
||||
question.correctAnswers.contains(userAnswer.selectedAnswers.first);
|
||||
} else if (question.type == QuestionType.fillInBlank) {
|
||||
isCorrect = userAnswer.textAnswer?.toLowerCase().trim() != null &&
|
||||
question.correctAnswers.any((correct) =>
|
||||
correct.toLowerCase().trim() == userAnswer.textAnswer!.toLowerCase().trim());
|
||||
}
|
||||
|
||||
if (isCorrect) {
|
||||
totalScore += question.points;
|
||||
}
|
||||
|
||||
// 记录技能得分
|
||||
final skill = question.skillType;
|
||||
if (!skillScores.containsKey(skill)) {
|
||||
skillScores[skill] = [];
|
||||
}
|
||||
skillScores[skill]!.add(isCorrect ? question.points : 0);
|
||||
}
|
||||
}
|
||||
|
||||
// 计算技能得分
|
||||
final skillScoresList = skillScores.entries.map((entry) {
|
||||
final skill = entry.key;
|
||||
final scores = entry.value;
|
||||
final skillTotal = scores.reduce((a, b) => a + b);
|
||||
final skillMax = scores.length * 10; // 假设每题10分
|
||||
final percentage = skillMax > 0 ? (skillTotal / skillMax * 100) : 0.0;
|
||||
|
||||
return SkillScore(
|
||||
skillType: skill,
|
||||
score: skillTotal,
|
||||
maxScore: skillMax,
|
||||
percentage: percentage,
|
||||
level: _getSkillLevel(percentage),
|
||||
feedback: _getSkillFeedback(skill, percentage),
|
||||
);
|
||||
}).toList();
|
||||
|
||||
final overallPercentage = maxScore > 0 ? (totalScore / maxScore * 100) : 0.0;
|
||||
|
||||
return TestResult(
|
||||
id: 'result_${DateTime.now().millisecondsSinceEpoch}',
|
||||
testId: session.id,
|
||||
userId: session.userId,
|
||||
testType: TestType.standard,
|
||||
totalScore: totalScore,
|
||||
maxScore: maxScore,
|
||||
percentage: overallPercentage,
|
||||
overallLevel: _getSkillLevel(overallPercentage),
|
||||
skillScores: skillScoresList,
|
||||
answers: answers,
|
||||
startTime: session.startTime!,
|
||||
endTime: DateTime.now(),
|
||||
duration: DateTime.now().difference(session.startTime!).inSeconds,
|
||||
feedback: _getOverallFeedback(totalScore, maxScore),
|
||||
recommendations: _getRecommendations(skillScoresList),
|
||||
);
|
||||
}
|
||||
|
||||
LanguageSkill _convertSkillType(SkillType skillType) {
|
||||
switch (skillType) {
|
||||
case SkillType.vocabulary:
|
||||
return LanguageSkill.vocabulary;
|
||||
case SkillType.grammar:
|
||||
return LanguageSkill.grammar;
|
||||
case SkillType.reading:
|
||||
return LanguageSkill.reading;
|
||||
case SkillType.listening:
|
||||
return LanguageSkill.listening;
|
||||
case SkillType.speaking:
|
||||
return LanguageSkill.speaking;
|
||||
case SkillType.writing:
|
||||
return LanguageSkill.writing;
|
||||
}
|
||||
}
|
||||
|
||||
DifficultyLevel _getSkillLevel(double percentage) {
|
||||
if (percentage >= 90) return DifficultyLevel.expert;
|
||||
if (percentage >= 80) return DifficultyLevel.advanced;
|
||||
if (percentage >= 70) return DifficultyLevel.upperIntermediate;
|
||||
if (percentage >= 60) return DifficultyLevel.intermediate;
|
||||
if (percentage >= 50) return DifficultyLevel.elementary;
|
||||
return DifficultyLevel.beginner;
|
||||
}
|
||||
|
||||
String _getSkillTypeName(SkillType skillType) {
|
||||
switch (skillType) {
|
||||
case SkillType.vocabulary:
|
||||
return '词汇';
|
||||
case SkillType.grammar:
|
||||
return '语法';
|
||||
case SkillType.reading:
|
||||
return '阅读';
|
||||
case SkillType.listening:
|
||||
return '听力';
|
||||
case SkillType.speaking:
|
||||
return '口语';
|
||||
case SkillType.writing:
|
||||
return '写作';
|
||||
}
|
||||
}
|
||||
|
||||
String _getSkillFeedback(SkillType skillType, double percentage) {
|
||||
final skillName = _getSkillTypeName(skillType);
|
||||
if (percentage >= 80) {
|
||||
return '$skillName 掌握得很好,继续保持!';
|
||||
} else if (percentage >= 60) {
|
||||
return '$skillName 基础不错,还有提升空间。';
|
||||
} else {
|
||||
return '$skillName 需要加强练习。';
|
||||
}
|
||||
}
|
||||
|
||||
String _getSkillName(SkillType skillType) {
|
||||
switch (skillType) {
|
||||
case SkillType.vocabulary:
|
||||
return '词汇';
|
||||
case SkillType.grammar:
|
||||
return '语法';
|
||||
case SkillType.reading:
|
||||
return '阅读';
|
||||
case SkillType.listening:
|
||||
return '听力';
|
||||
case SkillType.speaking:
|
||||
return '口语';
|
||||
case SkillType.writing:
|
||||
return '写作';
|
||||
}
|
||||
}
|
||||
|
||||
String _getOverallFeedback(int totalScore, int maxScore) {
|
||||
final percentage = (totalScore / maxScore * 100).round();
|
||||
if (percentage >= 90) {
|
||||
return '恭喜!您的表现非常出色,各项技能都达到了很高的水平。';
|
||||
} else if (percentage >= 80) {
|
||||
return '您的表现很好!大部分技能都掌握得不错,但还有一些提升空间。';
|
||||
} else if (percentage >= 70) {
|
||||
return '您的基础还不错,但需要在某些技能上加强练习。';
|
||||
} else if (percentage >= 60) {
|
||||
return '您已经掌握了一些基础知识,但还需要更多的练习和学习。';
|
||||
} else {
|
||||
return '建议您从基础开始系统性地学习,多做练习。';
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, dynamic> _getRecommendations(List<SkillScore> skillScores) {
|
||||
final weakSkills = skillScores
|
||||
.where((skill) => skill.percentage < 70)
|
||||
.map((skill) => _getSkillTypeName(skill.skillType))
|
||||
.toList();
|
||||
|
||||
List<String> recommendations = [];
|
||||
|
||||
if (weakSkills.isNotEmpty) {
|
||||
recommendations.add('重点加强${weakSkills.join('、')}方面的练习');
|
||||
}
|
||||
|
||||
recommendations.addAll([
|
||||
'每天坚持30分钟的英语学习',
|
||||
'多做相关类型的练习题',
|
||||
'建议参加更多的模拟测试',
|
||||
]);
|
||||
|
||||
return {
|
||||
'general': recommendations,
|
||||
'weakSkills': weakSkills,
|
||||
};
|
||||
}
|
||||
|
||||
void _onAnswerChanged(String questionId, UserAnswer answer) {
|
||||
setState(() {
|
||||
_userAnswers[questionId] = answer;
|
||||
});
|
||||
}
|
||||
|
||||
void _nextQuestion() {
|
||||
ref.read(testProvider.notifier).nextQuestion();
|
||||
}
|
||||
|
||||
void _previousQuestion() {
|
||||
ref.read(testProvider.notifier).previousQuestion();
|
||||
}
|
||||
|
||||
String _formatTime(int seconds) {
|
||||
final minutes = seconds ~/ 60;
|
||||
final remainingSeconds = seconds % 60;
|
||||
return '${minutes.toString().padLeft(2, '0')}:${remainingSeconds.toString().padLeft(2, '0')}';
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final testState = ref.watch(testProvider);
|
||||
final currentQuestion = testState.currentQuestion;
|
||||
final currentSession = testState.currentSession;
|
||||
|
||||
if (!_isTestStarted || currentSession == null) {
|
||||
return _buildLoadingScreen();
|
||||
}
|
||||
|
||||
if (currentQuestion == null) {
|
||||
return _buildCompletionScreen();
|
||||
}
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.grey[50],
|
||||
appBar: AppBar(
|
||||
title: Text(widget.template.name),
|
||||
backgroundColor: const Color(0xFF2196F3),
|
||||
foregroundColor: Colors.white,
|
||||
elevation: 0,
|
||||
actions: [
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
margin: const EdgeInsets.only(right: 16),
|
||||
decoration: BoxDecoration(
|
||||
color: _timeRemaining < 300 ? Colors.red : Colors.white.withOpacity(0.2),
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.timer,
|
||||
size: 16,
|
||||
color: _timeRemaining < 300 ? Colors.white : Colors.white,
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
_formatTime(_timeRemaining),
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: _timeRemaining < 300 ? Colors.white : Colors.white,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Column(
|
||||
children: [
|
||||
_buildProgressBar(currentSession),
|
||||
Expanded(
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildQuestionHeader(currentQuestion, currentSession),
|
||||
const SizedBox(height: 24),
|
||||
_buildQuestionContent(),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
_buildNavigationBar(currentSession),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildLoadingScreen() {
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.grey[50],
|
||||
body: const Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
CircularProgressIndicator(),
|
||||
SizedBox(height: 16),
|
||||
Text(
|
||||
'正在准备测试...',
|
||||
style: TextStyle(fontSize: 16),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildCompletionScreen() {
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.grey[50],
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Icon(
|
||||
Icons.check_circle,
|
||||
size: 64,
|
||||
color: Colors.green,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
const Text(
|
||||
'测试已完成!',
|
||||
style: TextStyle(
|
||||
fontSize: 24,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
const Text(
|
||||
'正在计算结果...',
|
||||
style: TextStyle(fontSize: 16),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
ElevatedButton(
|
||||
onPressed: _submitTest,
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.green,
|
||||
foregroundColor: Colors.white,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 32, vertical: 12),
|
||||
),
|
||||
child: const Text('查看结果'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildProgressBar(TestSession session) {
|
||||
final progress = (session.currentQuestionIndex + 1) / session.questions.length;
|
||||
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.grey.withOpacity(0.1),
|
||||
blurRadius: 4,
|
||||
offset: const Offset(0, 2),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(
|
||||
'题目 ${session.currentQuestionIndex + 1} / ${session.questions.length}',
|
||||
style: const TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
'${(progress * 100).toInt()}% 完成',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: Colors.grey[600],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
LinearProgressIndicator(
|
||||
value: progress,
|
||||
backgroundColor: Colors.grey[200],
|
||||
valueColor: const AlwaysStoppedAnimation<Color>(Color(0xFF2196F3)),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildQuestionHeader(TestQuestion question, TestSession session) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.grey.withOpacity(0.1),
|
||||
blurRadius: 4,
|
||||
offset: const Offset(0, 2),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: _getSkillColor(question.skillType).withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Text(
|
||||
_getSkillName(question.skillType),
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: _getSkillColor(question.skillType),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: _getDifficultyColor(question.difficulty).withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Text(
|
||||
_getDifficultyName(question.difficulty),
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: _getDifficultyColor(question.difficulty),
|
||||
),
|
||||
),
|
||||
),
|
||||
const Spacer(),
|
||||
Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.star,
|
||||
size: 16,
|
||||
color: Colors.amber[600],
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
'${question.points} 分',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: Colors.grey[600],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildQuestionContent() {
|
||||
final testState = ref.watch(testProvider);
|
||||
final currentQuestion = testState.currentQuestion;
|
||||
|
||||
if (currentQuestion == null) {
|
||||
return const Center(
|
||||
child: Text(
|
||||
'没有找到题目',
|
||||
style: TextStyle(fontSize: 16, color: Colors.grey),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final userAnswer = testState.currentSession?.answers
|
||||
.where((answer) => answer.questionId == currentQuestion.id)
|
||||
.firstOrNull;
|
||||
|
||||
return QuestionWidget(
|
||||
question: currentQuestion,
|
||||
userAnswer: userAnswer,
|
||||
onAnswerChanged: (answer) {
|
||||
ref.read(testProvider.notifier).recordAnswer(answer);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildNavigationBar(TestSession session) {
|
||||
final canGoPrevious = session.currentQuestionIndex > 0;
|
||||
final canGoNext = session.currentQuestionIndex < session.questions.length - 1;
|
||||
final isLastQuestion = session.currentQuestionIndex == session.questions.length - 1;
|
||||
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.grey.withOpacity(0.1),
|
||||
blurRadius: 4,
|
||||
offset: const Offset(0, -2),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: ElevatedButton(
|
||||
onPressed: canGoPrevious ? _previousQuestion : null,
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.grey[300],
|
||||
foregroundColor: Colors.grey[700],
|
||||
padding: const EdgeInsets.symmetric(vertical: 12),
|
||||
),
|
||||
child: const Text('上一题'),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
flex: 2,
|
||||
child: ElevatedButton(
|
||||
onPressed: isLastQuestion ? _submitTest : (canGoNext ? _nextQuestion : null),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: isLastQuestion ? Colors.green : const Color(0xFF2196F3),
|
||||
foregroundColor: Colors.white,
|
||||
padding: const EdgeInsets.symmetric(vertical: 12),
|
||||
),
|
||||
child: Text(isLastQuestion ? '提交测试' : '下一题'),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Color _getSkillColor(SkillType skillType) {
|
||||
switch (skillType) {
|
||||
case SkillType.vocabulary:
|
||||
return Colors.blue;
|
||||
case SkillType.grammar:
|
||||
return Colors.green;
|
||||
case SkillType.reading:
|
||||
return Colors.orange;
|
||||
case SkillType.listening:
|
||||
return Colors.purple;
|
||||
case SkillType.speaking:
|
||||
return Colors.red;
|
||||
case SkillType.writing:
|
||||
return Colors.teal;
|
||||
}
|
||||
}
|
||||
|
||||
Color _getDifficultyColor(DifficultyLevel difficulty) {
|
||||
switch (difficulty) {
|
||||
case DifficultyLevel.beginner:
|
||||
return Colors.green;
|
||||
case DifficultyLevel.elementary:
|
||||
return Colors.lightGreen;
|
||||
case DifficultyLevel.intermediate:
|
||||
return Colors.orange;
|
||||
case DifficultyLevel.upperIntermediate:
|
||||
return Colors.deepOrange;
|
||||
case DifficultyLevel.advanced:
|
||||
return Colors.red;
|
||||
case DifficultyLevel.expert:
|
||||
return Colors.purple;
|
||||
}
|
||||
}
|
||||
|
||||
String _getDifficultyName(DifficultyLevel difficulty) {
|
||||
switch (difficulty) {
|
||||
case DifficultyLevel.beginner:
|
||||
return '初级';
|
||||
case DifficultyLevel.elementary:
|
||||
return '基础';
|
||||
case DifficultyLevel.intermediate:
|
||||
return '中级';
|
||||
case DifficultyLevel.upperIntermediate:
|
||||
return '中高级';
|
||||
case DifficultyLevel.advanced:
|
||||
return '高级';
|
||||
case DifficultyLevel.expert:
|
||||
return '专家级';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,560 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../models/test_models.dart';
|
||||
import '../providers/test_riverpod_provider.dart';
|
||||
|
||||
/// 测试结果页面
|
||||
class TestResultScreen extends ConsumerWidget {
|
||||
final TestResult testResult;
|
||||
|
||||
const TestResultScreen({
|
||||
super.key,
|
||||
required this.testResult,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.grey[50],
|
||||
appBar: AppBar(
|
||||
title: const Text('测试结果'),
|
||||
backgroundColor: Colors.white,
|
||||
foregroundColor: Colors.black87,
|
||||
elevation: 0,
|
||||
actions: [
|
||||
IconButton(
|
||||
onPressed: () => _shareResult(context),
|
||||
icon: const Icon(Icons.share),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildOverallScore(),
|
||||
const SizedBox(height: 24),
|
||||
_buildSkillBreakdown(),
|
||||
const SizedBox(height: 24),
|
||||
_buildPerformanceAnalysis(),
|
||||
const SizedBox(height: 24),
|
||||
_buildRecommendations(),
|
||||
const SizedBox(height: 24),
|
||||
_buildActionButtons(context),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildOverallScore() {
|
||||
final percentage = (testResult.totalScore / testResult.maxScore * 100).round();
|
||||
final level = _getScoreLevel(percentage);
|
||||
final color = _getScoreColor(percentage);
|
||||
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(24),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.grey.withOpacity(0.1),
|
||||
blurRadius: 8,
|
||||
offset: const Offset(0, 4),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.emoji_events,
|
||||
size: 32,
|
||||
color: color,
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
const Expanded(
|
||||
child: Text(
|
||||
'总体成绩',
|
||||
style: TextStyle(
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.baseline,
|
||||
textBaseline: TextBaseline.alphabetic,
|
||||
children: [
|
||||
Text(
|
||||
'$percentage',
|
||||
style: TextStyle(
|
||||
fontSize: 48,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: color,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
'%',
|
||||
style: TextStyle(
|
||||
fontSize: 24,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: color,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
level,
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: color,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
||||
children: [
|
||||
_buildScoreDetail('得分', '${testResult.totalScore}'),
|
||||
_buildScoreDetail('满分', '${testResult.maxScore}'),
|
||||
_buildScoreDetail('用时', _formatDuration(testResult.duration)),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildScoreDetail(String label, String value) {
|
||||
return Column(
|
||||
children: [
|
||||
Text(
|
||||
value,
|
||||
style: const TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
label,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.grey[600],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSkillBreakdown() {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(20),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.grey.withOpacity(0.1),
|
||||
blurRadius: 8,
|
||||
offset: const Offset(0, 4),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.analytics,
|
||||
size: 24,
|
||||
color: Colors.blue,
|
||||
),
|
||||
SizedBox(width: 8),
|
||||
Text(
|
||||
'技能分析',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
...testResult.skillScores.map((skillScore) => _buildSkillItem(skillScore)),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSkillItem(SkillScore skillScore) {
|
||||
final color = _getSkillColor(skillScore.skillType);
|
||||
final percentage = skillScore.percentage;
|
||||
|
||||
return Container(
|
||||
margin: const EdgeInsets.only(bottom: 16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(
|
||||
_getSkillName(skillScore.skillType),
|
||||
style: const TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
'${percentage.round()}%',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: color,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
LinearProgressIndicator(
|
||||
value: percentage / 100,
|
||||
backgroundColor: Colors.grey[200],
|
||||
valueColor: AlwaysStoppedAnimation<Color>(color),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
_getDifficultyName(skillScore.level),
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.grey[600],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildPerformanceAnalysis() {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(20),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.grey.withOpacity(0.1),
|
||||
blurRadius: 8,
|
||||
offset: const Offset(0, 4),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.insights,
|
||||
size: 24,
|
||||
color: Colors.green,
|
||||
),
|
||||
SizedBox(width: 8),
|
||||
Text(
|
||||
'表现分析',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
if (testResult.feedback != null) ...[
|
||||
Text(
|
||||
testResult.feedback!,
|
||||
style: const TextStyle(
|
||||
fontSize: 14,
|
||||
height: 1.5,
|
||||
),
|
||||
),
|
||||
] else ...[
|
||||
_buildDefaultAnalysis(),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildDefaultAnalysis() {
|
||||
final percentage = (testResult.totalScore / testResult.maxScore * 100).round();
|
||||
String analysis;
|
||||
|
||||
if (percentage >= 90) {
|
||||
analysis = '恭喜!您的表现非常出色,各项技能都达到了很高的水平。继续保持这种学习状态,可以尝试更高难度的挑战。';
|
||||
} else if (percentage >= 80) {
|
||||
analysis = '您的表现很好!大部分技能都掌握得不错,但还有一些提升空间。建议重点关注得分较低的技能领域。';
|
||||
} else if (percentage >= 70) {
|
||||
analysis = '您的基础还不错,但需要在某些技能上加强练习。建议制定针对性的学习计划,逐步提升薄弱环节。';
|
||||
} else if (percentage >= 60) {
|
||||
analysis = '您已经掌握了一些基础知识,但还需要更多的练习和学习。建议从基础开始,循序渐进地提升各项技能。';
|
||||
} else {
|
||||
analysis = '建议您从基础开始系统性地学习,多做练习,不要着急,学习是一个循序渐进的过程。';
|
||||
}
|
||||
|
||||
return Text(
|
||||
analysis,
|
||||
style: const TextStyle(
|
||||
fontSize: 14,
|
||||
height: 1.5,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildRecommendations() {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(20),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.grey.withOpacity(0.1),
|
||||
blurRadius: 8,
|
||||
offset: const Offset(0, 4),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.lightbulb,
|
||||
size: 24,
|
||||
color: Colors.orange,
|
||||
),
|
||||
SizedBox(width: 8),
|
||||
Text(
|
||||
'学习建议',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
if (testResult.recommendations != null && testResult.recommendations!.isNotEmpty) ...[
|
||||
...testResult.recommendations!.values.where((v) => v is String).map((recommendation) => _buildRecommendationItem(recommendation as String)),
|
||||
] else ...[
|
||||
..._getDefaultRecommendations().map((recommendation) => _buildRecommendationItem(recommendation)),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildRecommendationItem(String recommendation) {
|
||||
return Container(
|
||||
margin: const EdgeInsets.only(bottom: 12),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Container(
|
||||
width: 6,
|
||||
height: 6,
|
||||
margin: const EdgeInsets.only(top: 6),
|
||||
decoration: const BoxDecoration(
|
||||
color: Colors.orange,
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Text(
|
||||
recommendation,
|
||||
style: const TextStyle(
|
||||
fontSize: 14,
|
||||
height: 1.4,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
List<String> _getDefaultRecommendations() {
|
||||
final weakSkills = testResult.skillScores
|
||||
.where((skill) => skill.percentage < 70)
|
||||
.map((skill) => _getSkillName(skill.skillType))
|
||||
.toList();
|
||||
|
||||
List<String> recommendations = [];
|
||||
|
||||
if (weakSkills.isNotEmpty) {
|
||||
recommendations.add('重点加强${weakSkills.join('、')}方面的练习');
|
||||
}
|
||||
|
||||
recommendations.addAll([
|
||||
'每天坚持30分钟的英语学习',
|
||||
'多做相关类型的练习题',
|
||||
'建议参加更多的模拟测试',
|
||||
'可以寻求老师或同学的帮助',
|
||||
]);
|
||||
|
||||
return recommendations;
|
||||
}
|
||||
|
||||
Widget _buildActionButtons(BuildContext context) {
|
||||
return Column(
|
||||
children: [
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: ElevatedButton(
|
||||
onPressed: () => _retakeTest(context),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: const Color(0xFF2196F3),
|
||||
foregroundColor: Colors.white,
|
||||
padding: const EdgeInsets.symmetric(vertical: 16),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
),
|
||||
child: const Text(
|
||||
'重新测试',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: OutlinedButton(
|
||||
onPressed: () => _backToHome(context),
|
||||
style: OutlinedButton.styleFrom(
|
||||
foregroundColor: const Color(0xFF2196F3),
|
||||
padding: const EdgeInsets.symmetric(vertical: 16),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
),
|
||||
child: const Text(
|
||||
'返回首页',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
void _shareResult(BuildContext context) {
|
||||
// 这里应该实现分享功能
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('分享功能正在开发中...'),
|
||||
backgroundColor: Colors.blue,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _retakeTest(BuildContext context) {
|
||||
Navigator.of(context).popUntil((route) => route.isFirst);
|
||||
}
|
||||
|
||||
void _backToHome(BuildContext context) {
|
||||
Navigator.of(context).popUntil((route) => route.isFirst);
|
||||
}
|
||||
|
||||
String _formatDuration(int seconds) {
|
||||
final minutes = seconds ~/ 60;
|
||||
final remainingSeconds = seconds % 60;
|
||||
return '${minutes}分${remainingSeconds}秒';
|
||||
}
|
||||
|
||||
String _getScoreLevel(int percentage) {
|
||||
if (percentage >= 90) return '优秀';
|
||||
if (percentage >= 80) return '良好';
|
||||
if (percentage >= 70) return '中等';
|
||||
if (percentage >= 60) return '及格';
|
||||
return '需要努力';
|
||||
}
|
||||
|
||||
Color _getScoreColor(int percentage) {
|
||||
if (percentage >= 90) return Colors.green;
|
||||
if (percentage >= 80) return Colors.blue;
|
||||
if (percentage >= 70) return Colors.orange;
|
||||
if (percentage >= 60) return Colors.amber;
|
||||
return Colors.red;
|
||||
}
|
||||
|
||||
Color _getSkillColor(SkillType skill) {
|
||||
switch (skill) {
|
||||
case SkillType.vocabulary:
|
||||
return Colors.blue;
|
||||
case SkillType.grammar:
|
||||
return Colors.green;
|
||||
case SkillType.reading:
|
||||
return Colors.orange;
|
||||
case SkillType.listening:
|
||||
return Colors.purple;
|
||||
case SkillType.speaking:
|
||||
return Colors.red;
|
||||
case SkillType.writing:
|
||||
return Colors.teal;
|
||||
}
|
||||
}
|
||||
|
||||
String _getSkillName(SkillType skill) {
|
||||
switch (skill) {
|
||||
case SkillType.vocabulary:
|
||||
return '词汇';
|
||||
case SkillType.grammar:
|
||||
return '语法';
|
||||
case SkillType.reading:
|
||||
return '阅读';
|
||||
case SkillType.listening:
|
||||
return '听力';
|
||||
case SkillType.speaking:
|
||||
return '口语';
|
||||
case SkillType.writing:
|
||||
return '写作';
|
||||
}
|
||||
}
|
||||
|
||||
String _getDifficultyName(DifficultyLevel difficulty) {
|
||||
switch (difficulty) {
|
||||
case DifficultyLevel.beginner:
|
||||
return '初级';
|
||||
case DifficultyLevel.elementary:
|
||||
return '基础';
|
||||
case DifficultyLevel.intermediate:
|
||||
return '中级';
|
||||
case DifficultyLevel.upperIntermediate:
|
||||
return '中高级';
|
||||
case DifficultyLevel.advanced:
|
||||
return '高级';
|
||||
case DifficultyLevel.expert:
|
||||
return '专家级';
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user