init
This commit is contained in:
800
client/lib/features/home/screens/home_screen.dart
Normal file
800
client/lib/features/home/screens/home_screen.dart
Normal file
@@ -0,0 +1,800 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../../../core/routes/app_routes.dart';
|
||||
import '../../vocabulary/providers/vocabulary_provider.dart';
|
||||
import '../../vocabulary/models/word_model.dart';
|
||||
import '../../auth/providers/auth_provider.dart';
|
||||
import '../../../shared/providers/notification_provider.dart';
|
||||
import '../../../core/services/tts_service.dart';
|
||||
|
||||
/// 首页界面
|
||||
class HomeScreen extends ConsumerStatefulWidget {
|
||||
const HomeScreen({super.key});
|
||||
|
||||
@override
|
||||
ConsumerState<HomeScreen> createState() => _HomeScreenState();
|
||||
}
|
||||
|
||||
class _HomeScreenState extends ConsumerState<HomeScreen> {
|
||||
final TTSService _ttsService = TTSService();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
// 初始化TTS服务
|
||||
_ttsService.initialize();
|
||||
|
||||
// 页面首帧后触发数据加载(今日单词与统计)
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
||||
print('=== HomeScreen: 开始加载数据 ===');
|
||||
final user = ref.read(currentUserProvider);
|
||||
print('当前用户: ${user?.username}, ID: ${user?.id}');
|
||||
|
||||
try {
|
||||
// 加载未读通知数量
|
||||
ref.read(notificationProvider.notifier).loadUnreadCount();
|
||||
|
||||
// 直接调用 Notifier 的方法
|
||||
final notifier = ref.read(vocabularyProvider.notifier);
|
||||
|
||||
print('开始加载今日单词...');
|
||||
await notifier.loadTodayStudyWords(userId: user?.id);
|
||||
print('今日单词加载完成,数量: ${ref.read(todayWordsProvider).length}');
|
||||
|
||||
print('开始加载统计数据...');
|
||||
await notifier.loadUserVocabularyOverallStats();
|
||||
await notifier.loadWeeklyStudyStats();
|
||||
print('统计数据加载完成');
|
||||
} catch (e, stack) {
|
||||
print('数据加载失败: $e');
|
||||
print('堆栈: $stack');
|
||||
}
|
||||
print('=== HomeScreen: 数据加载结束 ===');
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
// 释放TTS资源
|
||||
_ttsService.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: const Color(0xFFF5F5F5),
|
||||
body: SafeArea(
|
||||
child: SingleChildScrollView(
|
||||
physics: const BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics()),
|
||||
child: Column(
|
||||
children: [
|
||||
_buildHeader(),
|
||||
const SizedBox(height: 20),
|
||||
_buildQuickActions(context),
|
||||
const SizedBox(height: 20),
|
||||
_buildLearningStats(context),
|
||||
const SizedBox(height: 20),
|
||||
_buildTodayWords(),
|
||||
const SizedBox(height: 20),
|
||||
_buildAIRecommendation(context),
|
||||
const SizedBox(height: 100), // 底部导航栏空间
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildHeader() {
|
||||
// 直接使用StateNotifierProvider获取认证状态
|
||||
final authState = ref.watch(authProvider);
|
||||
final user = authState.user;
|
||||
final isAuthenticated = authState.isAuthenticated;
|
||||
final unreadCount = ref.watch(unreadCountProvider);
|
||||
final avatarUrl = user?.profile?.avatar ?? user?.avatar;
|
||||
final displayName = user?.profile?.realName ?? user?.username ?? '未登录用户';
|
||||
final motto = user?.profile?.bio ?? '欢迎来到AI英语学习';
|
||||
|
||||
return GestureDetector(
|
||||
onTap: () {
|
||||
// 点击头部跳转:未登录->登录页,已登录->个人中心
|
||||
if (isAuthenticated) {
|
||||
Navigator.pushNamed(context, Routes.profile);
|
||||
} else {
|
||||
Navigator.pushNamed(context, Routes.login);
|
||||
}
|
||||
},
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: const BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topLeft,
|
||||
end: Alignment.bottomRight,
|
||||
colors: [Color(0xFF2196F3), Color(0xFF1976D2)],
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
CircleAvatar(
|
||||
radius: 25,
|
||||
backgroundColor: Colors.white,
|
||||
backgroundImage: avatarUrl != null && avatarUrl.isNotEmpty
|
||||
? NetworkImage(avatarUrl)
|
||||
: null,
|
||||
child: (avatarUrl == null || avatarUrl.isEmpty)
|
||||
? Icon(
|
||||
Icons.person,
|
||||
color: isAuthenticated ? const Color(0xFF2196F3) : Colors.grey,
|
||||
size: 24,
|
||||
)
|
||||
: null,
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
displayName,
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
motto,
|
||||
style: const TextStyle(
|
||||
color: Colors.white70,
|
||||
fontSize: 14,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Stack(
|
||||
children: [
|
||||
IconButton(
|
||||
onPressed: () {
|
||||
// TODO: 跳转到通知页面
|
||||
Navigator.pushNamed(context, '/notifications');
|
||||
},
|
||||
icon: const Icon(
|
||||
Icons.notifications_outlined,
|
||||
color: Colors.white,
|
||||
size: 28,
|
||||
),
|
||||
),
|
||||
if (unreadCount > 0)
|
||||
Positioned(
|
||||
right: 8,
|
||||
top: 8,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(4),
|
||||
decoration: const BoxDecoration(
|
||||
color: Colors.red,
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
constraints: const BoxConstraints(
|
||||
minWidth: 18,
|
||||
minHeight: 18,
|
||||
),
|
||||
child: Text(
|
||||
unreadCount > 99 ? '99+' : unreadCount.toString(),
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 10,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Widget _buildQuickActions(BuildContext context) {
|
||||
return Container(
|
||||
margin: const EdgeInsets.symmetric(horizontal: 16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Padding(
|
||||
padding: EdgeInsets.only(left: 4, bottom: 12),
|
||||
child: Text(
|
||||
'快速功能',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Color(0xFF333333),
|
||||
),
|
||||
),
|
||||
),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: _buildQuickActionCard(
|
||||
context,
|
||||
'智能复习',
|
||||
'基于记忆曲线',
|
||||
Icons.psychology,
|
||||
const Color(0xFF4CAF50),
|
||||
() => Navigator.pushNamed(context, '/vocabulary/smart-review'),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: _buildQuickActionCard(
|
||||
context,
|
||||
'词汇测试',
|
||||
'检测学习效果',
|
||||
Icons.quiz,
|
||||
const Color(0xFFFF9800),
|
||||
() => Navigator.pushNamed(context, '/vocabulary/test'),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: _buildQuickActionCard(
|
||||
context,
|
||||
'生词本',
|
||||
'收藏重要单词',
|
||||
Icons.bookmark,
|
||||
const Color(0xFF9C27B0),
|
||||
() => Navigator.pushNamed(context, '/vocabulary/word-book'),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: _buildQuickActionCard(
|
||||
context,
|
||||
'学习计划',
|
||||
'制定学习目标',
|
||||
Icons.schedule,
|
||||
const Color(0xFF2196F3),
|
||||
() => Navigator.pushNamed(context, '/vocabulary/study-plan'),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildQuickActionCard(
|
||||
BuildContext context,
|
||||
String title,
|
||||
String subtitle,
|
||||
IconData icon,
|
||||
Color color,
|
||||
VoidCallback onTap,
|
||||
) {
|
||||
return GestureDetector(
|
||||
onTap: onTap,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withOpacity(0.05),
|
||||
blurRadius: 10,
|
||||
offset: const Offset(0, 2),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
Container(
|
||||
width: 48,
|
||||
height: 48,
|
||||
decoration: BoxDecoration(
|
||||
color: color.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(24),
|
||||
),
|
||||
child: Icon(
|
||||
icon,
|
||||
color: color,
|
||||
size: 24,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Text(
|
||||
title,
|
||||
style: const TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Color(0xFF333333),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
subtitle,
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.grey,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildLearningStats(BuildContext context) {
|
||||
final statistics = ref.watch(todayStatisticsProvider);
|
||||
final dailyStats = ref.watch(dailyVocabularyStatsProvider);
|
||||
final weeklyStudied = ref.watch(weeklyWordsStudiedProvider);
|
||||
final overallStats = ref.watch(overallVocabularyStatsProvider) ?? {};
|
||||
final user = ref.watch(currentUserProvider);
|
||||
final todayStudied = dailyStats?.wordsLearned ?? statistics?.wordsStudied ?? 0;
|
||||
final totalStudied = (overallStats['total_studied'] as int?) ?? 0;
|
||||
final weeklyGoal = ((user?.settings?.dailyWordGoal ?? 20) * 7);
|
||||
final progress = weeklyGoal > 0 ? (weeklyStudied / weeklyGoal).clamp(0.0, 1.0) : 0.0;
|
||||
final progressPercentText = '${(progress * 100).toInt()}%';
|
||||
return Container(
|
||||
margin: const EdgeInsets.symmetric(horizontal: 16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(left: 4, bottom: 12),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
const Text(
|
||||
'学习统计',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Color(0xFF333333),
|
||||
),
|
||||
),
|
||||
GestureDetector(
|
||||
onTap: () {
|
||||
Navigator.of(context).pushNamed(Routes.learningStatsDetail);
|
||||
},
|
||||
child: const Text(
|
||||
'查看详情',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: Color(0xFF2196F3),
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Container(
|
||||
padding: const EdgeInsets.all(20),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withOpacity(0.05),
|
||||
blurRadius: 10,
|
||||
offset: const Offset(0, 2),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: _buildStatCard(todayStudied.toString(), '今日学习', Icons.today, const Color(0xFF4CAF50)),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: _buildStatCard(weeklyStudied.toString(), '本周学习', Icons.date_range, const Color(0xFF2196F3)),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: _buildStatCard(totalStudied.toString(), '总词汇量', Icons.library_books, const Color(0xFFFF9800)),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFF2196F3).withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
const Icon(
|
||||
Icons.trending_up,
|
||||
color: Color(0xFF2196F3),
|
||||
size: 24,
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'本周学习进度',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: Color(0xFF333333),
|
||||
),
|
||||
),
|
||||
Text(
|
||||
progressPercentText,
|
||||
style: const TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Color(0xFF2196F3),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Container(
|
||||
width: 60,
|
||||
height: 6,
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(3),
|
||||
),
|
||||
child: FractionallySizedBox(
|
||||
alignment: Alignment.centerLeft,
|
||||
widthFactor: progress,
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFF2196F3),
|
||||
borderRadius: BorderRadius.circular(3),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStatCard(String value, String label, IconData icon, Color color) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: color.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
Icon(
|
||||
icon,
|
||||
color: color,
|
||||
size: 20,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
value,
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: color,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
label,
|
||||
style: const TextStyle(
|
||||
fontSize: 11,
|
||||
color: Colors.grey,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTodayWords() {
|
||||
final words = ref.watch(todayWordsProvider);
|
||||
|
||||
return Container(
|
||||
margin: const EdgeInsets.symmetric(horizontal: 16),
|
||||
padding: const EdgeInsets.all(20),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withOpacity(0.05),
|
||||
blurRadius: 10,
|
||||
offset: const Offset(0, 2),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
const Text(
|
||||
'今日单词',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
Navigator.pushNamed(context, Routes.dailyWords);
|
||||
},
|
||||
child: const Text('查看更多'),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
words.isEmpty
|
||||
? Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 20.0),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.book_outlined,
|
||||
size: 48,
|
||||
color: Colors.grey[400],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Text(
|
||||
'暂无今日单词',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
color: Colors.grey[600],
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
// 触发加载今日单词
|
||||
final notifier = ref.read(vocabularyProvider.notifier);
|
||||
final user = ref.read(currentUserProvider);
|
||||
await notifier.loadTodayStudyWords(userId: user?.id);
|
||||
},
|
||||
child: const Text('加载今日单词'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
)
|
||||
: Column(
|
||||
children: [
|
||||
_buildWordItem(words[0]),
|
||||
if (words.length > 1) ...[
|
||||
const SizedBox(height: 12),
|
||||
_buildWordItem(words[1]),
|
||||
],
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTodayWordsOld() {
|
||||
final vocabAsync = ref.watch(vocabularyProvider);
|
||||
|
||||
return Container(
|
||||
margin: const EdgeInsets.symmetric(horizontal: 16),
|
||||
padding: const EdgeInsets.all(20),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withOpacity(0.05),
|
||||
blurRadius: 10,
|
||||
offset: const Offset(0, 2),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
const Text(
|
||||
'今日单词',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
Navigator.pushNamed(context, Routes.dailyWords);
|
||||
},
|
||||
child: const Text('查看更多'),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Builder(
|
||||
builder: (context) {
|
||||
final state = ref.watch(vocabularyProvider);
|
||||
final words = state.todayWords;
|
||||
if (words.isEmpty) {
|
||||
return Column(
|
||||
children: [
|
||||
const Icon(
|
||||
Icons.book_outlined,
|
||||
size: 48,
|
||||
color: Colors.grey,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
const Text(
|
||||
'暂无今日单词',
|
||||
style: TextStyle(fontSize: 14, color: Colors.grey),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
final user = ref.read(currentUserProvider);
|
||||
final notifier = ref.read(vocabularyProvider.notifier);
|
||||
await notifier.loadTodayStudyWords(userId: user?.id);
|
||||
},
|
||||
child: const Text('刷新'),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
return Column(
|
||||
children: [
|
||||
_buildWordItem(words[0]),
|
||||
if (words.length > 1) ...[
|
||||
const SizedBox(height: 12),
|
||||
_buildWordItem(words[1]),
|
||||
],
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildWordItem(Word word) {
|
||||
final meaning = word.definitions.isNotEmpty ? word.definitions.first.definition : '';
|
||||
return Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
word.word,
|
||||
style: const TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
meaning,
|
||||
style: const TextStyle(
|
||||
fontSize: 14,
|
||||
color: Colors.grey,
|
||||
),
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
onPressed: () async {
|
||||
// 播放单词发音
|
||||
await _ttsService.speak(word.word);
|
||||
},
|
||||
icon: const Icon(
|
||||
Icons.volume_up,
|
||||
color: Color(0xFF2196F3),
|
||||
size: 24,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildAIRecommendation(BuildContext context) {
|
||||
return GestureDetector(
|
||||
onTap: () {
|
||||
Navigator.pushNamed(context, '/ai');
|
||||
},
|
||||
child: Container(
|
||||
margin: const EdgeInsets.symmetric(horizontal: 16),
|
||||
padding: const EdgeInsets.all(20),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withOpacity(0.05),
|
||||
blurRadius: 10,
|
||||
offset: const Offset(0, 2),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Container(
|
||||
width: 48,
|
||||
height: 48,
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFF2196F3).withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(24),
|
||||
),
|
||||
child: const Icon(
|
||||
Icons.smart_toy,
|
||||
color: Color(0xFF2196F3),
|
||||
size: 24,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
const Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'AI 智能助手',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
SizedBox(height: 4),
|
||||
Text(
|
||||
'写作批改 • 口语评估 • 智能推荐\n点击体验AI学习助手',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: Colors.grey,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const Icon(
|
||||
Icons.arrow_forward_ios,
|
||||
color: Colors.grey,
|
||||
size: 16,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,722 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import '../../../core/theme/app_colors.dart';
|
||||
import '../../../shared/widgets/custom_app_bar.dart';
|
||||
import '../../../shared/widgets/custom_card.dart';
|
||||
import '../../vocabulary/models/learning_stats_model.dart';
|
||||
import '../../vocabulary/services/learning_stats_service.dart';
|
||||
import '../../../core/services/storage_service.dart';
|
||||
import '../../../core/network/api_client.dart';
|
||||
|
||||
/// 学习统计详情页面
|
||||
class LearningStatsDetailScreen extends ConsumerStatefulWidget {
|
||||
const LearningStatsDetailScreen({super.key});
|
||||
|
||||
@override
|
||||
ConsumerState<LearningStatsDetailScreen> createState() => _LearningStatsDetailScreenState();
|
||||
}
|
||||
|
||||
class _LearningStatsDetailScreenState extends ConsumerState<LearningStatsDetailScreen>
|
||||
with SingleTickerProviderStateMixin {
|
||||
late TabController _tabController;
|
||||
LearningStats? _stats;
|
||||
bool _isLoading = true;
|
||||
String? _error;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_tabController = TabController(length: 3, vsync: this);
|
||||
_loadStats();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_tabController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _loadStats() async {
|
||||
try {
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
_error = null;
|
||||
});
|
||||
|
||||
final storageService = await StorageService.getInstance();
|
||||
final apiClient = ApiClient.instance;
|
||||
|
||||
final statsService = LearningStatsService(
|
||||
apiClient: apiClient,
|
||||
storageService: storageService,
|
||||
);
|
||||
|
||||
// 从本地存储加载统计数据
|
||||
final stats = await statsService.getUserStats();
|
||||
|
||||
setState(() {
|
||||
_stats = stats;
|
||||
_isLoading = false;
|
||||
});
|
||||
} catch (e) {
|
||||
print('❌ 加载学习统计失败: $e');
|
||||
setState(() {
|
||||
_error = e.toString();
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: AppColors.background,
|
||||
appBar: CustomAppBar(
|
||||
title: '学习统计详情',
|
||||
automaticallyImplyLeading: true,
|
||||
),
|
||||
body: _isLoading
|
||||
? const Center(child: CircularProgressIndicator())
|
||||
: _error != null
|
||||
? _buildErrorWidget()
|
||||
: _buildContent(),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildErrorWidget() {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.error_outline,
|
||||
size: 64,
|
||||
color: AppColors.error,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
'加载失败',
|
||||
style: Theme.of(context).textTheme.headlineSmall,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
_error ?? '未知错误',
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
color: AppColors.onSurfaceVariant,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
ElevatedButton(
|
||||
onPressed: _loadStats,
|
||||
child: const Text('重试'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildContent() {
|
||||
if (_stats == null) return const SizedBox.shrink();
|
||||
|
||||
return Column(
|
||||
children: [
|
||||
_buildOverviewCard(),
|
||||
const SizedBox(height: 16),
|
||||
_buildTabBar(),
|
||||
Expanded(
|
||||
child: TabBarView(
|
||||
controller: _tabController,
|
||||
children: [
|
||||
_buildDailyTab(),
|
||||
_buildWeeklyTab(),
|
||||
_buildMonthlyTab(),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildOverviewCard() {
|
||||
return Container(
|
||||
margin: const EdgeInsets.all(16),
|
||||
child: CustomCard(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'总体统计',
|
||||
style: Theme.of(context).textTheme.titleLarge?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: _buildOverviewItem(
|
||||
'学习天数',
|
||||
'${_stats!.totalStudyDays}',
|
||||
'天',
|
||||
Icons.calendar_today,
|
||||
AppColors.primary,
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: _buildOverviewItem(
|
||||
'连续学习',
|
||||
'${_stats!.currentStreak}',
|
||||
'天',
|
||||
Icons.local_fire_department,
|
||||
AppColors.warning,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: _buildOverviewItem(
|
||||
'学习单词',
|
||||
'${_stats!.totalWordsLearned}',
|
||||
'个',
|
||||
Icons.school,
|
||||
AppColors.success,
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: _buildOverviewItem(
|
||||
'学习时长',
|
||||
'${_stats!.totalStudyHours.toStringAsFixed(1)}',
|
||||
'小时',
|
||||
Icons.access_time,
|
||||
AppColors.info,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
_buildProgressBar(),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildOverviewItem(String title, String value, String unit, IconData icon, Color color) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: color.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Icon(icon, color: color, size: 20),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
title,
|
||||
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
||||
color: AppColors.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
RichText(
|
||||
text: TextSpan(
|
||||
children: [
|
||||
TextSpan(
|
||||
text: value,
|
||||
style: Theme.of(context).textTheme.headlineSmall?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: color,
|
||||
),
|
||||
),
|
||||
TextSpan(
|
||||
text: ' $unit',
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
color: AppColors.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildProgressBar() {
|
||||
final progress = _stats!.progressPercentage;
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(
|
||||
'本周学习进度',
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
'${(progress * 100).toInt()}%',
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: AppColors.primary,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
LinearProgressIndicator(
|
||||
value: progress,
|
||||
backgroundColor: AppColors.surfaceVariant,
|
||||
valueColor: AlwaysStoppedAnimation<Color>(AppColors.primary),
|
||||
minHeight: 8,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTabBar() {
|
||||
return Container(
|
||||
margin: const EdgeInsets.symmetric(horizontal: 16),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.surfaceVariant,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: TabBar(
|
||||
controller: _tabController,
|
||||
indicator: BoxDecoration(
|
||||
color: AppColors.primary,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
labelColor: Colors.white,
|
||||
unselectedLabelColor: AppColors.onSurfaceVariant,
|
||||
tabs: const [
|
||||
Tab(text: '每日'),
|
||||
Tab(text: '每周'),
|
||||
Tab(text: '每月'),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildDailyTab() {
|
||||
return SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
children: [
|
||||
_buildDailyChart(),
|
||||
const SizedBox(height: 16),
|
||||
_buildDailyStats(),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildWeeklyTab() {
|
||||
return SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
children: [
|
||||
_buildWeeklyChart(),
|
||||
const SizedBox(height: 16),
|
||||
_buildWeeklyStats(),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildMonthlyTab() {
|
||||
return SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
children: [
|
||||
_buildMonthlyChart(),
|
||||
const SizedBox(height: 16),
|
||||
_buildMonthlyStats(),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildDailyChart() {
|
||||
return CustomCard(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'每日学习趋势',
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
SizedBox(
|
||||
height: 200,
|
||||
child: LineChart(
|
||||
LineChartData(
|
||||
gridData: FlGridData(show: false),
|
||||
titlesData: FlTitlesData(
|
||||
leftTitles: AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
rightTitles: AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
topTitles: AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
bottomTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
interval: 1,
|
||||
getTitlesWidget: (value, meta) {
|
||||
const days = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'];
|
||||
final index = value.toInt();
|
||||
if (index >= 0 && index < days.length) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Text(
|
||||
days[index],
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
);
|
||||
}
|
||||
return const Text('');
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
borderData: FlBorderData(show: false),
|
||||
lineBarsData: [
|
||||
LineChartBarData(
|
||||
spots: _generateDailySpots(),
|
||||
isCurved: true,
|
||||
color: AppColors.primary,
|
||||
barWidth: 3,
|
||||
dotData: FlDotData(show: true),
|
||||
belowBarData: BarAreaData(
|
||||
show: true,
|
||||
color: AppColors.primary.withOpacity(0.1),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildWeeklyChart() {
|
||||
// 计算最大值,用于设置柱状图的y轴范围
|
||||
double maxY = 100;
|
||||
if (_stats != null && _stats!.monthlyStats.weeklyRecords.isNotEmpty) {
|
||||
final maxWords = _stats!.monthlyStats.weeklyRecords
|
||||
.map((r) => r.wordsLearned)
|
||||
.reduce((a, b) => a > b ? a : b);
|
||||
maxY = (maxWords * 1.2).ceilToDouble(); // 留有20%的上边距
|
||||
if (maxY < 10) maxY = 10; // 最小值10
|
||||
}
|
||||
|
||||
return CustomCard(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'每周学习统计',
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
SizedBox(
|
||||
height: 200,
|
||||
child: BarChart(
|
||||
BarChartData(
|
||||
alignment: BarChartAlignment.spaceAround,
|
||||
maxY: maxY,
|
||||
barTouchData: BarTouchData(enabled: false),
|
||||
titlesData: FlTitlesData(
|
||||
show: true,
|
||||
bottomTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
getTitlesWidget: (value, meta) {
|
||||
const weeks = ['第1周', '第2周', '第3周', '第4周'];
|
||||
if (value.toInt() >= 0 && value.toInt() < weeks.length) {
|
||||
return Text(
|
||||
weeks[value.toInt()],
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
);
|
||||
}
|
||||
return const Text('');
|
||||
},
|
||||
),
|
||||
),
|
||||
leftTitles: AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
topTitles: AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
rightTitles: AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
),
|
||||
borderData: FlBorderData(show: false),
|
||||
barGroups: _generateWeeklyBars(),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildMonthlyChart() {
|
||||
return CustomCard(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'月度学习分析',
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
SizedBox(
|
||||
height: 200,
|
||||
child: PieChart(
|
||||
PieChartData(
|
||||
sections: _generatePieChartSections(),
|
||||
centerSpaceRadius: 40,
|
||||
sectionsSpace: 2,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildDailyStats() {
|
||||
return CustomCard(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'今日详情',
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
_buildStatRow('学习单词', '${_stats!.weeklyStats.wordsLearned ~/ 7}', '个'),
|
||||
_buildStatRow('复习单词', '${_stats!.weeklyStats.wordsReviewed ~/ 7}', '个'),
|
||||
_buildStatRow('学习时长', '${(_stats!.weeklyStats.studyHours / 7).toStringAsFixed(1)}', '小时'),
|
||||
_buildStatRow('准确率', '${(_stats!.weeklyStats.accuracyRate * 100).toInt()}', '%'),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildWeeklyStats() {
|
||||
return CustomCard(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'本周详情',
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
_buildStatRow('学习天数', '${_stats!.weeklyStats.studyDays}', '天'),
|
||||
_buildStatRow('学习单词', '${_stats!.weeklyStats.wordsLearned}', '个'),
|
||||
_buildStatRow('复习单词', '${_stats!.weeklyStats.wordsReviewed}', '个'),
|
||||
_buildStatRow('学习时长', '${_stats!.weeklyStats.studyHours.toStringAsFixed(1)}', '小时'),
|
||||
_buildStatRow('准确率', '${(_stats!.weeklyStats.accuracyRate * 100).toInt()}', '%'),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildMonthlyStats() {
|
||||
return CustomCard(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'本月详情',
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
_buildStatRow('学习天数', '${_stats!.monthlyStats.studyDays}', '天'),
|
||||
_buildStatRow('学习单词', '${_stats!.monthlyStats.wordsLearned}', '个'),
|
||||
_buildStatRow('复习单词', '${_stats!.monthlyStats.wordsReviewed}', '个'),
|
||||
_buildStatRow('学习时长', '${(_stats!.monthlyStats.studyTimeMinutes / 60).toStringAsFixed(1)}', '小时'),
|
||||
_buildStatRow('准确率', '${(_stats!.monthlyStats.accuracyRate * 100).toInt()}', '%'),
|
||||
_buildStatRow('完成词汇书', '${_stats!.monthlyStats.completedBooks}', '本'),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStatRow(String label, String value, String unit) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(
|
||||
label,
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
color: AppColors.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
RichText(
|
||||
text: TextSpan(
|
||||
children: [
|
||||
TextSpan(
|
||||
text: value,
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: AppColors.onSurface,
|
||||
),
|
||||
),
|
||||
TextSpan(
|
||||
text: ' $unit',
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
color: AppColors.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
List<FlSpot> _generateDailySpots() {
|
||||
if (_stats == null || _stats!.weeklyStats.dailyRecords.isEmpty) {
|
||||
// 如果没有数据,返回空数组
|
||||
return [];
|
||||
}
|
||||
|
||||
// 使用最近7天的真实数据
|
||||
final dailyRecords = _stats!.weeklyStats.dailyRecords;
|
||||
final spots = <FlSpot>[];
|
||||
|
||||
// 获取最近7天的记录
|
||||
final now = DateTime.now();
|
||||
for (int i = 6; i >= 0; i--) {
|
||||
final targetDate = now.subtract(Duration(days: i));
|
||||
final dateStr = '${targetDate.year}-${targetDate.month.toString().padLeft(2, '0')}-${targetDate.day.toString().padLeft(2, '0')}';
|
||||
|
||||
// 查找该日期的记录
|
||||
final record = dailyRecords.where((r) {
|
||||
final recordDateStr = '${r.date.year}-${r.date.month.toString().padLeft(2, '0')}-${r.date.day.toString().padLeft(2, '0')}';
|
||||
return recordDateStr == dateStr;
|
||||
}).firstOrNull;
|
||||
|
||||
// 添加数据点(x轴:0-6表示周一到周日,y轴:学习单词数)
|
||||
final wordsCount = record?.wordsLearned.toDouble() ?? 0.0;
|
||||
spots.add(FlSpot((6 - i).toDouble(), wordsCount));
|
||||
}
|
||||
|
||||
return spots;
|
||||
}
|
||||
|
||||
List<BarChartGroupData> _generateWeeklyBars() {
|
||||
if (_stats == null || _stats!.monthlyStats.weeklyRecords.isEmpty) {
|
||||
// 如果没有数据,返回空数组
|
||||
return [];
|
||||
}
|
||||
|
||||
// 使用月度统计中的周记录
|
||||
final weeklyRecords = _stats!.monthlyStats.weeklyRecords;
|
||||
final bars = <BarChartGroupData>[];
|
||||
|
||||
// 最多显示4周的数据
|
||||
for (int i = 0; i < weeklyRecords.length && i < 4; i++) {
|
||||
final record = weeklyRecords[i];
|
||||
bars.add(
|
||||
BarChartGroupData(
|
||||
x: i,
|
||||
barRods: [
|
||||
BarChartRodData(
|
||||
toY: record.wordsLearned.toDouble(),
|
||||
color: AppColors.primary,
|
||||
width: 20,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return bars;
|
||||
}
|
||||
|
||||
List<PieChartSectionData> _generatePieChartSections() {
|
||||
if (_stats == null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// 使用月度统计数据计算比例
|
||||
final monthlyStats = _stats!.monthlyStats;
|
||||
final totalWords = monthlyStats.wordsLearned + monthlyStats.wordsReviewed;
|
||||
|
||||
if (totalWords == 0) {
|
||||
// 如果没有数据,显示默认状态
|
||||
return [
|
||||
PieChartSectionData(
|
||||
color: AppColors.surfaceVariant,
|
||||
value: 100,
|
||||
title: '暂无数据',
|
||||
radius: 60,
|
||||
titleStyle: const TextStyle(fontSize: 12, fontWeight: FontWeight.bold, color: Colors.white),
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
// 计算新学单词和复习单词的比例
|
||||
final newWordsPercent = (monthlyStats.wordsLearned / totalWords * 100).toInt();
|
||||
final reviewWordsPercent = (monthlyStats.wordsReviewed / totalWords * 100).toInt();
|
||||
|
||||
return [
|
||||
PieChartSectionData(
|
||||
color: AppColors.primary,
|
||||
value: monthlyStats.wordsLearned.toDouble(),
|
||||
title: '新学\n$newWordsPercent%',
|
||||
radius: 60,
|
||||
titleStyle: const TextStyle(fontSize: 12, fontWeight: FontWeight.bold, color: Colors.white),
|
||||
),
|
||||
PieChartSectionData(
|
||||
color: AppColors.success,
|
||||
value: monthlyStats.wordsReviewed.toDouble(),
|
||||
title: '复习\n$reviewWordsPercent%',
|
||||
radius: 60,
|
||||
titleStyle: const TextStyle(fontSize: 12, fontWeight: FontWeight.bold, color: Colors.white),
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
54
client/lib/features/home/screens/simple_home_screen.dart
Normal file
54
client/lib/features/home/screens/simple_home_screen.dart
Normal file
@@ -0,0 +1,54 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// 简化版主页面 - 用于测试应用启动
|
||||
class SimpleHomeScreen extends StatelessWidget {
|
||||
const SimpleHomeScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('AI英语学习平台'),
|
||||
backgroundColor: Colors.blue,
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
body: const Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.school,
|
||||
size: 100,
|
||||
color: Colors.blue,
|
||||
),
|
||||
SizedBox(height: 24),
|
||||
Text(
|
||||
'欢迎使用AI英语学习平台',
|
||||
style: TextStyle(
|
||||
fontSize: 24,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
SizedBox(height: 16),
|
||||
Text(
|
||||
'智能化英语学习体验',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
color: Colors.grey,
|
||||
),
|
||||
),
|
||||
SizedBox(height: 32),
|
||||
Text(
|
||||
'应用启动成功!',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: Colors.green,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user