Files
nj/app/lib/features/templates/views/template_gallery_page.dart
iven 7e3597dc77 feat(diary): B4+B5+B6 后端服务 + F5/F6/F7 前端模块
后端 (erp-diary):
- B4: CommentService 班级成员验证 + 删除评语 + SSE 通知推送
- B4: NotificationService 评语/主题/成就三类通知事件
- B5: StickerService 贴纸包列表 + 贴纸查询 + 模板管理
- B5: AchievementService 成就列表 + 解锁 + SSE 通知
- B6: MoodStatsService 心情统计 + 连续天数
- B6: ContentSafetyService 敏感词过滤框架
- SSE handler 增加 diary.notification.* 事件处理
- 新增 14 个 API 端点 + diary.comment.delete 权限

前端 (Flutter):
- F5: CalendarBloc + 月视图日历 + 日记列表
- F6: MoodBloc + fl_chart 心情饼图 + 统计卡片 + 连续天数
- F7: 贴纸库分类浏览 + 模板画廊
- 首页改为日记流 + 心情快速选择
- 成就页改为徽章收集展示

验证: cargo check ✓ cargo test 17/17 ✓ flutter analyze 0 error
2026-06-01 09:32:09 +08:00

191 lines
6.5 KiB
Dart

// 模板画廊页面 — 日记模板浏览和选择
import 'package:flutter/material.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,
});
}
/// 模板画廊页面 — 浏览和选择日记模板
class TemplateGalleryPage extends StatefulWidget {
const TemplateGalleryPage({super.key});
@override
State<TemplateGalleryPage> createState() => _TemplateGalleryPageState();
}
class _TemplateGalleryPageState extends State<TemplateGalleryPage> {
String _selectedCategory = '全部';
final _categories = ['全部', '日常', '旅行', '校园', '节日', '创意'];
// 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
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),
// 模板网格
Expanded(
child: GridView.builder(
padding: const EdgeInsets.all(16),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
mainAxisSpacing: 12,
crossAxisSpacing: 12,
childAspectRatio: 0.78,
),
itemCount: filtered.length,
itemBuilder: (context, index) {
return _TemplateCard(template: filtered[index]);
},
),
),
],
),
);
}
}
/// 模板卡片
class _TemplateCard extends StatelessWidget {
const _TemplateCard({required this.template});
final Template template;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final colorScheme = theme.colorScheme;
return Card(
elevation: 0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
side: BorderSide(color: colorScheme.outlineVariant),
),
child: InkWell(
onTap: () {
// 使用模板创建日记
context.go('/editor?template=${template.id}');
},
borderRadius: BorderRadius.circular(16),
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// 模板预览区
Container(
width: 72,
height: 72,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
colorScheme.primaryContainer.withValues(alpha: 0.5),
AppColors.tertiary.withValues(alpha: 0.3),
],
),
borderRadius: BorderRadius.circular(16),
),
alignment: Alignment.center,
child: Text(
template.emoji,
style: const TextStyle(fontSize: 36),
),
),
const SizedBox(height: 12),
// 模板名称
Text(
template.name,
style: theme.textTheme.titleSmall?.copyWith(
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 4),
// 描述
if (template.description != null)
Text(
template.description!,
style: theme.textTheme.bodySmall?.copyWith(
color: colorScheme.onSurface.withValues(alpha: 0.5),
),
maxLines: 2,
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.center,
),
],
),
),
),
);
}
}