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:
181
app/lib/features/stickers/bloc/sticker_bloc.dart
Normal file
181
app/lib/features/stickers/bloc/sticker_bloc.dart
Normal file
@@ -0,0 +1,181 @@
|
||||
// 贴纸 BLoC — 通过 API 加载贴纸包和贴纸数据
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:nuanji_app/data/remote/api_client.dart';
|
||||
|
||||
// ===== 模型 =====
|
||||
|
||||
/// 贴纸包
|
||||
class StickerPack {
|
||||
final String id;
|
||||
final String name;
|
||||
final String? description;
|
||||
final String? coverImageUrl;
|
||||
final int stickerCount;
|
||||
final bool isFree;
|
||||
final String? category;
|
||||
|
||||
const StickerPack({
|
||||
required this.id,
|
||||
required this.name,
|
||||
this.description,
|
||||
this.coverImageUrl,
|
||||
this.stickerCount = 0,
|
||||
this.isFree = true,
|
||||
this.category,
|
||||
});
|
||||
|
||||
/// 兼容旧 UI 的 emoji 封面(优先用 coverImageUrl,否则用默认)
|
||||
String get displayCover => coverImageUrl ?? '🎨';
|
||||
}
|
||||
|
||||
/// 单张贴纸
|
||||
class Sticker {
|
||||
final String id;
|
||||
final String packId;
|
||||
final String name;
|
||||
final String imageUrl;
|
||||
final String? category;
|
||||
|
||||
const Sticker({
|
||||
required this.id,
|
||||
required this.packId,
|
||||
required this.name,
|
||||
required this.imageUrl,
|
||||
this.category,
|
||||
});
|
||||
}
|
||||
|
||||
// ===== State =====
|
||||
|
||||
/// 贴纸页面状态
|
||||
class StickerState {
|
||||
final List<StickerPack> packs;
|
||||
final String selectedCategory;
|
||||
final bool isLoading;
|
||||
final String? errorMessage;
|
||||
|
||||
const StickerState({
|
||||
this.packs = const [],
|
||||
this.selectedCategory = '全部',
|
||||
this.isLoading = false,
|
||||
this.errorMessage,
|
||||
});
|
||||
|
||||
/// 按分类过滤贴纸包
|
||||
List<StickerPack> get filteredPacks => selectedCategory == '全部'
|
||||
? packs
|
||||
: packs.where((p) => p.category == selectedCategory).toList();
|
||||
|
||||
/// 所有分类(去重 + 加"全部")
|
||||
List<String> get categories {
|
||||
final cats = packs
|
||||
.map((p) => p.category)
|
||||
.whereType<String>()
|
||||
.toSet()
|
||||
.toList();
|
||||
return ['全部', ...cats];
|
||||
}
|
||||
|
||||
StickerState copyWith({
|
||||
List<StickerPack>? packs,
|
||||
String? selectedCategory,
|
||||
bool? isLoading,
|
||||
String? errorMessage,
|
||||
}) =>
|
||||
StickerState(
|
||||
packs: packs ?? this.packs,
|
||||
selectedCategory: selectedCategory ?? this.selectedCategory,
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
errorMessage: errorMessage,
|
||||
);
|
||||
}
|
||||
|
||||
// ===== BLoC =====
|
||||
|
||||
/// 贴纸 BLoC — ChangeNotifier 模式
|
||||
class StickerBloc extends ChangeNotifier {
|
||||
final ApiClient _api;
|
||||
StickerState _state = const StickerState();
|
||||
StickerState get state => _state;
|
||||
|
||||
StickerBloc({required ApiClient api}) : _api = api;
|
||||
|
||||
/// 加载贴纸包列表
|
||||
void load() {
|
||||
_state = _state.copyWith(isLoading: true);
|
||||
notifyListeners();
|
||||
_fetchPacks();
|
||||
}
|
||||
|
||||
/// 选择分类
|
||||
void selectCategory(String category) {
|
||||
_state = _state.copyWith(selectedCategory: category);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// 按分类加载贴纸包
|
||||
void loadByCategory(String? category) {
|
||||
_state = _state.copyWith(isLoading: true);
|
||||
notifyListeners();
|
||||
_fetchPacks(category: category);
|
||||
}
|
||||
|
||||
Future<void> _fetchPacks({String? category}) async {
|
||||
try {
|
||||
final queryParams = category != null && category != '全部'
|
||||
? {'category': category}
|
||||
: null;
|
||||
|
||||
final response = await _api.get(
|
||||
'/diary/sticker-packs',
|
||||
queryParams: queryParams,
|
||||
);
|
||||
final body = response.data as Map<String, dynamic>;
|
||||
final list = body['data'] as List? ?? [];
|
||||
|
||||
final packs = list.map((item) {
|
||||
final m = item as Map<String, dynamic>;
|
||||
return StickerPack(
|
||||
id: m['id'] as String,
|
||||
name: m['name'] as String,
|
||||
description: m['description'] as String?,
|
||||
coverImageUrl: m['cover_image_url'] as String?,
|
||||
stickerCount: (m['sticker_count'] as num?)?.toInt() ?? 0,
|
||||
isFree: m['is_free'] as bool? ?? true,
|
||||
category: m['category'] as String?,
|
||||
);
|
||||
}).toList();
|
||||
|
||||
_state = _state.copyWith(isLoading: false, packs: packs);
|
||||
} catch (e) {
|
||||
_state = _state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: '加载贴纸包失败',
|
||||
);
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// 获取贴纸包内的贴纸列表
|
||||
Future<List<Sticker>> fetchStickersInPack(String packId) async {
|
||||
try {
|
||||
final response = await _api.get('/diary/sticker-packs/$packId/stickers');
|
||||
final body = response.data as Map<String, dynamic>;
|
||||
final list = body['data'] as List? ?? [];
|
||||
|
||||
return list.map((item) {
|
||||
final m = item as Map<String, dynamic>;
|
||||
return Sticker(
|
||||
id: m['id'] as String,
|
||||
packId: m['pack_id'] as String,
|
||||
name: m['name'] as String,
|
||||
imageUrl: m['image_url'] as String,
|
||||
category: m['category'] as String?,
|
||||
);
|
||||
}).toList();
|
||||
} catch (e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,26 +1,10 @@
|
||||
// 贴纸库页面 — 贴纸包浏览 + 贴纸网格
|
||||
// 贴纸库页面 — 贴纸包浏览 + 分类 Tab
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:nuanji_app/core/theme/app_colors.dart';
|
||||
|
||||
/// 贴纸包数据模型
|
||||
class StickerPack {
|
||||
final String id;
|
||||
final String name;
|
||||
final String? coverEmoji;
|
||||
final int stickerCount;
|
||||
final bool isFree;
|
||||
final String? category;
|
||||
|
||||
const StickerPack({
|
||||
required this.id,
|
||||
required this.name,
|
||||
this.coverEmoji,
|
||||
this.stickerCount = 0,
|
||||
this.isFree = true,
|
||||
this.category,
|
||||
});
|
||||
}
|
||||
import 'package:nuanji_app/data/remote/api_client.dart';
|
||||
import '../bloc/sticker_bloc.dart';
|
||||
|
||||
/// 贴纸库页面 — 分类浏览贴纸包
|
||||
class StickerLibraryPage extends StatefulWidget {
|
||||
@@ -30,93 +14,106 @@ class StickerLibraryPage extends StatefulWidget {
|
||||
State<StickerLibraryPage> createState() => _StickerLibraryPageState();
|
||||
}
|
||||
|
||||
class _StickerLibraryPageState extends State<StickerLibraryPage>
|
||||
with SingleTickerProviderStateMixin {
|
||||
late TabController _tabController;
|
||||
|
||||
// Phase 1 占位数据
|
||||
final _categories = ['全部', '动物', '食物', '自然', '节日', '表情'];
|
||||
|
||||
final _packs = const [
|
||||
StickerPack(id: '1', name: '可爱猫咪', coverEmoji: '🐱', stickerCount: 24, isFree: true, category: '动物'),
|
||||
StickerPack(id: '2', name: '小兔子系列', coverEmoji: '🐰', stickerCount: 20, isFree: true, category: '动物'),
|
||||
StickerPack(id: '3', name: '甜品派对', coverEmoji: '🍰', stickerCount: 18, isFree: true, category: '食物'),
|
||||
StickerPack(id: '4', name: '花朵合集', coverEmoji: '🌸', stickerCount: 22, isFree: true, category: '自然'),
|
||||
StickerPack(id: '5', name: '夏日清凉', coverEmoji: '🍉', stickerCount: 16, isFree: true, category: '食物'),
|
||||
StickerPack(id: '6', name: '星空物语', coverEmoji: '⭐', stickerCount: 20, isFree: false, category: '自然'),
|
||||
StickerPack(id: '7', name: '开心表情', coverEmoji: '😄', stickerCount: 30, isFree: true, category: '表情'),
|
||||
StickerPack(id: '8', name: '新年快乐', coverEmoji: '🎉', stickerCount: 15, isFree: false, category: '节日'),
|
||||
];
|
||||
class _StickerLibraryPageState extends State<StickerLibraryPage> {
|
||||
late final StickerBloc _bloc;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_tabController = TabController(length: _categories.length, vsync: this);
|
||||
_bloc = StickerBloc(api: context.read<ApiClient>());
|
||||
_bloc.load();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_tabController.dispose();
|
||||
_bloc.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
final colorScheme = theme.colorScheme;
|
||||
final colorScheme = Theme.of(context).colorScheme;
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('贴纸库'),
|
||||
bottom: TabBar(
|
||||
controller: _tabController,
|
||||
isScrollable: true,
|
||||
tabAlignment: TabAlignment.start,
|
||||
tabs: _categories.map((c) => Tab(text: c)).toList(),
|
||||
),
|
||||
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: 16),
|
||||
FilledButton.tonal(
|
||||
onPressed: _bloc.load,
|
||||
child: const Text('重试'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final categories = state.categories;
|
||||
|
||||
return Column(
|
||||
children: [
|
||||
// 分类选择器(横向滚动 Chips)
|
||||
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.filteredPacks.isEmpty
|
||||
? const Center(child: Text('暂无贴纸包'))
|
||||
: GridView.builder(
|
||||
padding: const EdgeInsets.all(16),
|
||||
gridDelegate:
|
||||
const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 2,
|
||||
mainAxisSpacing: 12,
|
||||
crossAxisSpacing: 12,
|
||||
childAspectRatio: 0.85,
|
||||
),
|
||||
itemCount: state.filteredPacks.length,
|
||||
itemBuilder: (context, index) {
|
||||
return _StickerPackCard(
|
||||
pack: state.filteredPacks[index],
|
||||
colorScheme: colorScheme,
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
body: TabBarView(
|
||||
controller: _tabController,
|
||||
children: _categories.map((category) {
|
||||
final filtered = category == '全部'
|
||||
? _packs
|
||||
: _packs.where((p) => p.category == category).toList();
|
||||
return _StickerPackGrid(packs: filtered, colorScheme: colorScheme);
|
||||
}).toList(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 贴纸包网格
|
||||
class _StickerPackGrid extends StatelessWidget {
|
||||
const _StickerPackGrid({
|
||||
required this.packs,
|
||||
required this.colorScheme,
|
||||
});
|
||||
|
||||
final List<StickerPack> packs;
|
||||
final ColorScheme colorScheme;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (packs.isEmpty) {
|
||||
return const Center(child: Text('暂无贴纸包'));
|
||||
}
|
||||
|
||||
return GridView.builder(
|
||||
padding: const EdgeInsets.all(16),
|
||||
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 2,
|
||||
mainAxisSpacing: 12,
|
||||
crossAxisSpacing: 12,
|
||||
childAspectRatio: 0.85,
|
||||
),
|
||||
itemCount: packs.length,
|
||||
itemBuilder: (context, index) {
|
||||
final pack = packs[index];
|
||||
return _StickerPackCard(pack: pack, colorScheme: colorScheme);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -143,7 +140,6 @@ class _StickerPackCard extends StatelessWidget {
|
||||
),
|
||||
child: InkWell(
|
||||
onTap: () {
|
||||
// Phase 1: 展示贴纸包详情页(待实现)
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('打开贴纸包: ${pack.name}')),
|
||||
);
|
||||
@@ -164,7 +160,7 @@ class _StickerPackCard extends StatelessWidget {
|
||||
),
|
||||
alignment: Alignment.center,
|
||||
child: Text(
|
||||
pack.coverEmoji ?? '🎨',
|
||||
pack.coverImageUrl != null ? '🎨' : pack.displayCover,
|
||||
style: const TextStyle(fontSize: 32),
|
||||
),
|
||||
),
|
||||
@@ -192,7 +188,8 @@ class _StickerPackCard extends StatelessWidget {
|
||||
if (!pack.isFree) ...[
|
||||
const SizedBox(width: 8),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 6, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.accent.withValues(alpha: 0.15),
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
|
||||
Reference in New Issue
Block a user