Files
wushu/lib/views/favorites_page.dart
2026-04-02 22:30:49 +08:00

240 lines
7.1 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../constants/app_constants.dart';
import '../views/footprint/liked_poetry_manager.dart';
import '../widgets/tabbed_nav_app_bar.dart';
import 'active/active_search_page.dart';
import 'footprint/all_list.dart';
import 'footprint/collect_notes.dart';
import 'footprint/local_jilu.dart';
import '../services/get/favorites_controller.dart';
import '../services/get/theme_controller.dart';
/// 时间: 2026-03-22
/// 功能: 收藏页面
/// 介绍: 展示用户收藏的内容,支持分类管理和搜索
/// 最新变化: 笔记列表组件独立到 local_jilu.dart
class FavoritesPage extends StatefulWidget {
const FavoritesPage({super.key});
@override
State<FavoritesPage> createState() => _FavoritesPageState();
}
class _FavoritesPageState extends State<FavoritesPage>
with SingleTickerProviderStateMixin {
late TabController _tabController;
final controller = Get.put(FavoritesController());
final themeController = Get.find<ThemeController>();
final GlobalKey<AllListPageState> _allListKey = GlobalKey<AllListPageState>();
@override
void initState() {
super.initState();
_tabController = TabController(
length: controller.categories.length,
vsync: this,
);
_tabController.addListener(() {
controller.setCurrentTabIndex(_tabController.index);
});
// 监听排序变化更新AllListPage
ever(controller.sortByTime, (bool newSortByTime) {
if (_allListKey.currentState != null && mounted) {
_allListKey.currentState!.sortByTime(newSortByTime);
}
});
ever(controller.likesFirst, (bool newLikesFirst) {
if (_allListKey.currentState != null && mounted) {
_allListKey.currentState!.toggleLikesFirst();
}
});
}
@override
void dispose() {
_tabController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Obx(() {
final isDark = themeController.isDarkMode;
return Scaffold(
backgroundColor: isDark ? const Color(0xFF1A1A1A) : null,
appBar: TabbedNavAppBar.build(
title: '足迹',
tabLabels: controller.categories,
tabController: _tabController,
tabBarScrollable: true,
tabLabelPadding: const EdgeInsets.symmetric(horizontal: 10),
backgroundColor: isDark ? const Color(0xFF1A1A1A) : Colors.white,
foregroundColor: isDark
? Colors.white
: themeController.currentThemeColor,
actions: _buildAppBarActions(controller, context),
),
body: Column(
children: [
_buildSearchBar(controller, isDark),
Expanded(
child: TabBarView(
controller: _tabController,
children: controller.categories
.map((category) => _buildFavoriteList(category, controller))
.toList(),
),
),
],
),
);
});
}
// 构建AppBar操作按钮
List<Widget> _buildAppBarActions(
FavoritesController controller,
BuildContext context,
) {
final isDark = themeController.isDarkMode;
List<Widget> actions = [
// 统一显示添加笔记按钮
IconButton(
icon: Icon(Icons.add, color: isDark ? Colors.white70 : null),
onPressed: () {
Get.to(const CollectNotesPage());
},
),
IconButton(
icon: Icon(
controller.isGridView.value ? Icons.view_list : Icons.grid_view,
color: isDark ? Colors.white70 : null,
),
onPressed: controller.toggleViewMode,
),
IconButton(
icon: Icon(Icons.filter_list, color: isDark ? Colors.white70 : null),
onPressed: () {
// 使用 Get.context 而不是传递的 context
if (Get.context != null) {
controller.showFilterOptions(Get.context!);
}
},
),
];
return actions;
}
Widget _buildSearchBar(FavoritesController controller, bool isDark) {
return Container(
margin: EdgeInsets.fromLTRB(
AppConstants.pageHorizontalPadding,
0,
AppConstants.pageHorizontalPadding,
16,
),
decoration: BoxDecoration(
color: isDark ? const Color(0xFF2A2A2A) : Colors.grey[100],
borderRadius: BorderRadius.circular(12),
),
child: TextField(
readOnly: true,
onTap: () {
Get.to(
ActiveSearchPage(
initialQuery: controller.searchQuery.value.isEmpty
? null
: controller.searchQuery.value,
),
);
},
decoration: InputDecoration(
hintText: '点按搜索全站诗词…',
hintStyle: TextStyle(color: isDark ? Colors.grey[400] : Colors.grey),
prefixIcon: Icon(
Icons.search,
color: isDark ? Colors.grey[400] : Colors.grey,
),
suffixIcon: Icon(
Icons.chevron_right,
color: isDark ? Colors.grey[400] : Colors.grey[500],
),
border: InputBorder.none,
contentPadding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 12,
),
),
),
);
}
Widget _buildFavoriteList(String category, FavoritesController controller) {
final isDark = themeController.isDarkMode;
// 如果是"全部"标签,显示统一的收藏列表(点赞+笔记)
if (category == '全部') {
return AllListPage(
key: _allListKey,
initialSortByTime: controller.sortByTime.value,
initialLikesFirst: controller.likesFirst.value,
);
}
// 如果是"点赞"标签,显示点赞内容管理器
if (category == '点赞') {
return const LikedPoetryManager();
}
// 如果是"笔记"标签,显示笔记列表
if (category == '笔记') {
return const LocalNotesList();
}
// 其他标签显示占位内容,但支持下拉刷新
return RefreshIndicator(
onRefresh: controller.refreshContent,
color: AppConstants.primaryColor,
backgroundColor: isDark ? const Color(0xFF2A2A2A) : Colors.white,
child: _buildPlaceholderContent(category),
);
}
Widget _buildPlaceholderContent(String category) {
final isDark = themeController.isDarkMode;
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.category_outlined,
size: 64,
color: isDark ? Colors.grey[600] : Colors.grey[400],
),
const SizedBox(height: 16),
Text(
'$category功能暂未开放',
style: TextStyle(
fontSize: 16,
color: isDark ? Colors.grey[400] : Colors.grey[600],
),
),
const SizedBox(height: 8),
Text(
'敬请期待后续更新',
style: TextStyle(
fontSize: 14,
color: isDark ? Colors.grey[500] : Colors.grey[500],
),
),
],
),
);
}
}