feat(app): 设置页 UI + Mood/成就/贴纸 BLoC 接入 API + B7 测试扩展
前端改动: - 新建设置页面 (主题切换/关于/隐私政策/用户协议/儿童隐私保护) - SettingsBloc 注册到 MultiRepositoryProvider 全局可访问 - MoodBloc 修复编译错误 + 接入 /diary/stats/mood API - MoodPage 添加错误状态展示和重试按钮 - AchievementBloc + 页面改造接入 /diary/achievements API - StickerBloc + 页面改造接入 /diary/sticker-packs API - TemplateBloc + 页面改造接入 /diary/templates API - ProfilePage 设置入口改为跳转 /settings - 添加 /settings 路由 后端改动: - 扩展 mood_stats_service 测试 (连续天数算法/心情计数/边界场景) - 新增 class_service 测试 (班级码生成/唯一性/错误映射) - 新增 achievement_service 测试 (DTO 结构/序列化/map 构建) - 新增 sticker_service 测试 (DTO 序列化/错误处理) - 扩展 dto.rs 测试 (achievement/mood_stats/sticker/template/notification) - 清理 2 个 unused import warning 验证: - cargo check 0 error 0 warning - flutter analyze 0 error
This commit is contained in:
108
app/lib/features/achievement/bloc/achievement_bloc.dart
Normal file
108
app/lib/features/achievement/bloc/achievement_bloc.dart
Normal file
@@ -0,0 +1,108 @@
|
||||
// 成就 BLoC — 通过 API 加载成就列表
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:nuanji_app/data/remote/api_client.dart';
|
||||
|
||||
// ===== 模型 =====
|
||||
|
||||
/// 成就数据
|
||||
class Achievement {
|
||||
final String id;
|
||||
final String code;
|
||||
final String name;
|
||||
final String? description;
|
||||
final String? icon;
|
||||
final String category;
|
||||
final bool isUnlocked;
|
||||
final DateTime? unlockedAt;
|
||||
|
||||
const Achievement({
|
||||
required this.id,
|
||||
required this.code,
|
||||
required this.name,
|
||||
this.description,
|
||||
this.icon,
|
||||
required this.category,
|
||||
this.isUnlocked = false,
|
||||
this.unlockedAt,
|
||||
});
|
||||
}
|
||||
|
||||
// ===== State =====
|
||||
|
||||
/// 成就页面状态
|
||||
class AchievementState {
|
||||
final List<Achievement> achievements;
|
||||
final bool isLoading;
|
||||
final String? errorMessage;
|
||||
|
||||
const AchievementState({
|
||||
this.achievements = const [],
|
||||
this.isLoading = false,
|
||||
this.errorMessage,
|
||||
});
|
||||
|
||||
int get unlockedCount =>
|
||||
achievements.where((a) => a.isUnlocked).length;
|
||||
|
||||
AchievementState copyWith({
|
||||
List<Achievement>? achievements,
|
||||
bool? isLoading,
|
||||
String? errorMessage,
|
||||
}) =>
|
||||
AchievementState(
|
||||
achievements: achievements ?? this.achievements,
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
errorMessage: errorMessage,
|
||||
);
|
||||
}
|
||||
|
||||
// ===== BLoC =====
|
||||
|
||||
/// 成就 BLoC — ChangeNotifier 模式
|
||||
class AchievementBloc extends ChangeNotifier {
|
||||
final ApiClient _api;
|
||||
AchievementState _state = const AchievementState();
|
||||
AchievementState get state => _state;
|
||||
|
||||
AchievementBloc({required ApiClient api}) : _api = api;
|
||||
|
||||
/// 加载成就列表
|
||||
void load() {
|
||||
_state = _state.copyWith(isLoading: true);
|
||||
notifyListeners();
|
||||
_fetchAchievements();
|
||||
}
|
||||
|
||||
Future<void> _fetchAchievements() async {
|
||||
try {
|
||||
final response = await _api.get('/diary/achievements');
|
||||
final body = response.data as Map<String, dynamic>;
|
||||
final list = body['data'] as List? ?? [];
|
||||
|
||||
final achievements = list.map((item) {
|
||||
final m = item as Map<String, dynamic>;
|
||||
return Achievement(
|
||||
id: m['id'] as String,
|
||||
code: m['code'] as String,
|
||||
name: m['name'] as String,
|
||||
description: m['description'] as String?,
|
||||
icon: m['icon'] as String?,
|
||||
category: m['category'] as String,
|
||||
isUnlocked: m['is_unlocked'] as bool? ?? false,
|
||||
unlockedAt: m['unlocked_at'] != null
|
||||
? DateTime.tryParse(m['unlocked_at'] as String)
|
||||
: null,
|
||||
);
|
||||
}).toList();
|
||||
|
||||
_state = _state.copyWith(isLoading: false, achievements: achievements);
|
||||
} catch (e) {
|
||||
_state = _state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: '加载成就列表失败',
|
||||
);
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
@@ -1,92 +1,109 @@
|
||||
// 成就页面 — 徽章收集展示
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:nuanji_app/core/theme/app_colors.dart';
|
||||
|
||||
/// 成就数据模型
|
||||
class Achievement {
|
||||
final String id;
|
||||
final String code;
|
||||
final String name;
|
||||
final String? description;
|
||||
final String icon;
|
||||
final String category;
|
||||
final bool isUnlocked;
|
||||
|
||||
const Achievement({
|
||||
required this.id,
|
||||
required this.code,
|
||||
required this.name,
|
||||
this.description,
|
||||
required this.icon,
|
||||
required this.category,
|
||||
this.isUnlocked = false,
|
||||
});
|
||||
}
|
||||
import 'package:nuanji_app/data/remote/api_client.dart';
|
||||
import '../bloc/achievement_bloc.dart';
|
||||
|
||||
/// 成就页面 — 徽章收集和展示
|
||||
class AchievementPage extends StatelessWidget {
|
||||
class AchievementPage extends StatefulWidget {
|
||||
const AchievementPage({super.key});
|
||||
|
||||
static const _achievements = [
|
||||
Achievement(id: '1', code: 'first_diary', name: '初次落笔', description: '写下第一篇日记', icon: '✏️', category: 'writing', isUnlocked: true),
|
||||
Achievement(id: '2', code: 'streak_7', name: '坚持一周', description: '连续写日记 7 天', icon: '🔥', category: 'writing'),
|
||||
Achievement(id: '3', code: 'streak_30', name: '月度达人', description: '连续写日记 30 天', icon: '💪', category: 'writing'),
|
||||
Achievement(id: '4', code: 'sticker_collector', name: '贴纸收藏家', description: '收集 10 张贴纸', icon: '🎨', category: 'collection'),
|
||||
Achievement(id: '5', code: 'social_butterfly', name: '分享之星', description: '分享 5 篇日记到班级', icon: '🌟', category: 'social'),
|
||||
Achievement(id: '6', code: 'mood_tracker', name: '心情记录员', description: '连续记录心情 14 天', icon: '🌈', category: 'writing'),
|
||||
Achievement(id: '7', code: 'early_bird', name: '早起日记', description: '在早上 7 点前写日记', icon: '🌅', category: 'special'),
|
||||
Achievement(id: '8', code: 'artist', name: '小画家', description: '在日记中画 10 幅涂鸦', icon: '🖌️', category: 'collection'),
|
||||
];
|
||||
@override
|
||||
State<AchievementPage> createState() => _AchievementPageState();
|
||||
}
|
||||
|
||||
class _AchievementPageState extends State<AchievementPage> {
|
||||
late final AchievementBloc _bloc;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_bloc = AchievementBloc(api: context.read<ApiClient>());
|
||||
_bloc.load();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_bloc.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
final colorScheme = theme.colorScheme;
|
||||
final unlocked = _achievements.where((a) => a.isUnlocked).length;
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('成就'),
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 进度概览
|
||||
_AchievementProgressCard(
|
||||
unlocked: unlocked,
|
||||
total: _achievements.length,
|
||||
colorScheme: colorScheme,
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
Text(
|
||||
'全部成就',
|
||||
style: theme.textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
appBar: AppBar(title: const Text('成就')),
|
||||
body: ListenableBuilder(
|
||||
listenable: _bloc,
|
||||
builder: (context, _) {
|
||||
final state = _bloc.state;
|
||||
|
||||
if (state.isLoading) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
if (state.errorMessage != null) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.error_outline, size: 48, color: colorScheme.error),
|
||||
const SizedBox(height: 12),
|
||||
Text(state.errorMessage!, style: theme.textTheme.bodyMedium),
|
||||
const SizedBox(height: 16),
|
||||
FilledButton.tonal(
|
||||
onPressed: _bloc.load,
|
||||
child: const Text('重试'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
GridView.builder(
|
||||
shrinkWrap: true,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 2,
|
||||
mainAxisSpacing: 12,
|
||||
crossAxisSpacing: 12,
|
||||
childAspectRatio: 0.75,
|
||||
),
|
||||
itemCount: _achievements.length,
|
||||
itemBuilder: (context, index) {
|
||||
return _AchievementCard(
|
||||
achievement: _achievements[index],
|
||||
);
|
||||
}
|
||||
|
||||
return SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 进度概览
|
||||
_AchievementProgressCard(
|
||||
unlocked: state.unlockedCount,
|
||||
total: state.achievements.length,
|
||||
colorScheme: colorScheme,
|
||||
);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
Text(
|
||||
'全部成就',
|
||||
style: theme.textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
GridView.builder(
|
||||
shrinkWrap: true,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 2,
|
||||
mainAxisSpacing: 12,
|
||||
crossAxisSpacing: 12,
|
||||
childAspectRatio: 0.75,
|
||||
),
|
||||
itemCount: state.achievements.length,
|
||||
itemBuilder: (context, index) {
|
||||
return _AchievementCard(
|
||||
achievement: state.achievements[index],
|
||||
colorScheme: colorScheme,
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -194,7 +211,10 @@ class _AchievementCard extends StatelessWidget {
|
||||
),
|
||||
alignment: Alignment.center,
|
||||
child: achievement.isUnlocked
|
||||
? Text(achievement.icon, style: const TextStyle(fontSize: 28))
|
||||
? Text(
|
||||
achievement.icon ?? '🏆',
|
||||
style: const TextStyle(fontSize: 28),
|
||||
)
|
||||
: Icon(
|
||||
Icons.lock_outline,
|
||||
color: colorScheme.onSurface.withValues(alpha: 0.3),
|
||||
|
||||
Reference in New Issue
Block a user