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:
135
app/lib/features/templates/bloc/template_bloc.dart
Normal file
135
app/lib/features/templates/bloc/template_bloc.dart
Normal file
@@ -0,0 +1,135 @@
|
||||
// 模板 BLoC — 通过 API 加载模板列表
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:nuanji_app/data/remote/api_client.dart';
|
||||
|
||||
// ===== 模型 =====
|
||||
|
||||
/// 日记模板
|
||||
class Template {
|
||||
final String id;
|
||||
final String name;
|
||||
final String? description;
|
||||
final String? previewUrl;
|
||||
final Map<String, dynamic>? templateData;
|
||||
final String? category;
|
||||
final bool isFree;
|
||||
|
||||
/// 用于 UI 显示的 emoji(基于 category 推导)
|
||||
String get emoji => switch (category) {
|
||||
'日常' => '☀️',
|
||||
'旅行' => '🗺️',
|
||||
'校园' => '📚',
|
||||
'节日' => '🎄',
|
||||
'创意' => '✨',
|
||||
_ => '📝',
|
||||
};
|
||||
|
||||
const Template({
|
||||
required this.id,
|
||||
required this.name,
|
||||
this.description,
|
||||
this.previewUrl,
|
||||
this.templateData,
|
||||
this.category,
|
||||
this.isFree = true,
|
||||
});
|
||||
}
|
||||
|
||||
// ===== State =====
|
||||
|
||||
/// 模板页面状态
|
||||
class TemplateState {
|
||||
final List<Template> templates;
|
||||
final String selectedCategory;
|
||||
final bool isLoading;
|
||||
final String? errorMessage;
|
||||
|
||||
const TemplateState({
|
||||
this.templates = const [],
|
||||
this.selectedCategory = '全部',
|
||||
this.isLoading = false,
|
||||
this.errorMessage,
|
||||
});
|
||||
|
||||
/// 按分类过滤模板
|
||||
List<Template> get filteredTemplates => selectedCategory == '全部'
|
||||
? templates
|
||||
: templates.where((t) => t.category == selectedCategory).toList();
|
||||
|
||||
/// 所有分类(去重 + 加"全部")
|
||||
List<String> get categories {
|
||||
final cats = templates
|
||||
.map((t) => t.category)
|
||||
.whereType<String>()
|
||||
.toSet()
|
||||
.toList();
|
||||
return ['全部', ...cats];
|
||||
}
|
||||
|
||||
TemplateState copyWith({
|
||||
List<Template>? templates,
|
||||
String? selectedCategory,
|
||||
bool? isLoading,
|
||||
String? errorMessage,
|
||||
}) =>
|
||||
TemplateState(
|
||||
templates: templates ?? this.templates,
|
||||
selectedCategory: selectedCategory ?? this.selectedCategory,
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
errorMessage: errorMessage,
|
||||
);
|
||||
}
|
||||
|
||||
// ===== BLoC =====
|
||||
|
||||
/// 模板 BLoC — ChangeNotifier 模式
|
||||
class TemplateBloc extends ChangeNotifier {
|
||||
final ApiClient _api;
|
||||
TemplateState _state = const TemplateState();
|
||||
TemplateState get state => _state;
|
||||
|
||||
TemplateBloc({required ApiClient api}) : _api = api;
|
||||
|
||||
/// 加载模板列表
|
||||
void load() {
|
||||
_state = _state.copyWith(isLoading: true);
|
||||
notifyListeners();
|
||||
_fetchTemplates();
|
||||
}
|
||||
|
||||
/// 选择分类
|
||||
void selectCategory(String category) {
|
||||
_state = _state.copyWith(selectedCategory: category);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> _fetchTemplates() async {
|
||||
try {
|
||||
final response = await _api.get('/diary/templates');
|
||||
final body = response.data as Map<String, dynamic>;
|
||||
final list = body['data'] as List? ?? [];
|
||||
|
||||
final templates = list.map((item) {
|
||||
final m = item as Map<String, dynamic>;
|
||||
return Template(
|
||||
id: m['id'] as String,
|
||||
name: m['name'] as String,
|
||||
description: m['description'] as String?,
|
||||
previewUrl: m['preview_url'] as String?,
|
||||
templateData: m['template_data'] as Map<String, dynamic>?,
|
||||
category: m['category'] as String?,
|
||||
isFree: m['is_free'] as bool? ?? true,
|
||||
);
|
||||
}).toList();
|
||||
|
||||
_state = _state.copyWith(isLoading: false, templates: templates);
|
||||
} catch (e) {
|
||||
_state = _state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: '加载模板列表失败',
|
||||
);
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
@@ -1,27 +1,11 @@
|
||||
// 模板画廊页面 — 日记模板浏览和选择
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:nuanji_app/core/theme/app_colors.dart';
|
||||
|
||||
/// 模板数据模型
|
||||
class Template {
|
||||
final String id;
|
||||
final String name;
|
||||
final String emoji;
|
||||
final String? category;
|
||||
final bool isFree;
|
||||
final String? description;
|
||||
|
||||
const Template({
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.emoji,
|
||||
this.category,
|
||||
this.isFree = true,
|
||||
this.description,
|
||||
});
|
||||
}
|
||||
import 'package:nuanji_app/data/remote/api_client.dart';
|
||||
import '../bloc/template_bloc.dart';
|
||||
|
||||
/// 模板画廊页面 — 浏览和选择日记模板
|
||||
class TemplateGalleryPage extends StatefulWidget {
|
||||
@@ -32,81 +16,104 @@ class TemplateGalleryPage extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _TemplateGalleryPageState extends State<TemplateGalleryPage> {
|
||||
String _selectedCategory = '全部';
|
||||
late final TemplateBloc _bloc;
|
||||
|
||||
final _categories = ['全部', '日常', '旅行', '校园', '节日', '创意'];
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_bloc = TemplateBloc(api: context.read<ApiClient>());
|
||||
_bloc.load();
|
||||
}
|
||||
|
||||
// Phase 1 占位数据
|
||||
final _templates = const [
|
||||
Template(id: '1', name: '今日心情', emoji: '💭', category: '日常', description: '记录今天的心情和感受'),
|
||||
Template(id: '2', name: '校园日记', emoji: '📚', category: '校园', description: '在学校的一天'),
|
||||
Template(id: '3', name: '旅行手账', emoji: '🗺️', category: '旅行', description: '记录旅行中的美好瞬间'),
|
||||
Template(id: '4', name: '美食记录', emoji: '🍜', category: '日常', description: '记录今天吃到的美食'),
|
||||
Template(id: '5', name: '读书笔记', emoji: '📖', category: '校园', description: '记录读完一本书的感想'),
|
||||
Template(id: '6', name: '节日特辑', emoji: '🎄', category: '节日', description: '特别的节日记录'),
|
||||
Template(id: '7', name: '自然观察', emoji: '🌿', category: '创意', description: '记录大自然的发现'),
|
||||
Template(id: '8', name: '梦想清单', emoji: '✨', category: '创意', description: '写下心中的梦想'),
|
||||
Template(id: '9', name: '周末时光', emoji: '☀️', category: '日常', description: '悠闲的周末记录'),
|
||||
Template(id: '10', name: '运动打卡', emoji: '🏃', category: '日常', description: '记录运动和锻炼'),
|
||||
];
|
||||
@override
|
||||
void dispose() {
|
||||
_bloc.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
final colorScheme = theme.colorScheme;
|
||||
|
||||
final filtered = _selectedCategory == '全部'
|
||||
? _templates
|
||||
: _templates.where((t) => t.category == _selectedCategory).toList();
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('模板画廊'),
|
||||
),
|
||||
body: Column(
|
||||
children: [
|
||||
// 分类选择器
|
||||
SizedBox(
|
||||
height: 48,
|
||||
child: ListView(
|
||||
scrollDirection: Axis.horizontal,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16),
|
||||
children: _categories.map((cat) {
|
||||
final isSelected = cat == _selectedCategory;
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(right: 8),
|
||||
child: FilterChip(
|
||||
selected: isSelected,
|
||||
label: Text(cat),
|
||||
onSelected: (_) {
|
||||
setState(() => _selectedCategory = cat);
|
||||
},
|
||||
selectedColor: colorScheme.primaryContainer,
|
||||
checkmarkColor: colorScheme.primary,
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
appBar: AppBar(title: const Text('模板画廊')),
|
||||
body: ListenableBuilder(
|
||||
listenable: _bloc,
|
||||
builder: (context, _) {
|
||||
final state = _bloc.state;
|
||||
|
||||
// 模板网格
|
||||
Expanded(
|
||||
child: GridView.builder(
|
||||
padding: const EdgeInsets.all(16),
|
||||
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 2,
|
||||
mainAxisSpacing: 12,
|
||||
crossAxisSpacing: 12,
|
||||
childAspectRatio: 0.78,
|
||||
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: 16),
|
||||
FilledButton.tonal(
|
||||
onPressed: _bloc.load,
|
||||
child: const Text('重试'),
|
||||
),
|
||||
],
|
||||
),
|
||||
itemCount: filtered.length,
|
||||
itemBuilder: (context, index) {
|
||||
return _TemplateCard(template: filtered[index]);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
final categories = state.categories;
|
||||
|
||||
return Column(
|
||||
children: [
|
||||
// 分类选择器
|
||||
SizedBox(
|
||||
height: 48,
|
||||
child: ListView(
|
||||
scrollDirection: Axis.horizontal,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16),
|
||||
children: categories.map((cat) {
|
||||
final isSelected = cat == state.selectedCategory;
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(right: 8),
|
||||
child: FilterChip(
|
||||
selected: isSelected,
|
||||
label: Text(cat),
|
||||
onSelected: (_) => _bloc.selectCategory(cat),
|
||||
selectedColor: colorScheme.primaryContainer,
|
||||
checkmarkColor: colorScheme.primary,
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
|
||||
// 模板网格
|
||||
Expanded(
|
||||
child: state.filteredTemplates.isEmpty
|
||||
? const Center(child: Text('暂无模板'))
|
||||
: GridView.builder(
|
||||
padding: const EdgeInsets.all(16),
|
||||
gridDelegate:
|
||||
const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 2,
|
||||
mainAxisSpacing: 12,
|
||||
crossAxisSpacing: 12,
|
||||
childAspectRatio: 0.78,
|
||||
),
|
||||
itemCount: state.filteredTemplates.length,
|
||||
itemBuilder: (context, index) {
|
||||
return _TemplateCard(
|
||||
template: state.filteredTemplates[index],
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user