Files
wushu/lib/services/get/favorites_controller.dart
Developer 3738f5a0a3 补充
2026-04-21 02:30:26 +08:00

182 lines
5.8 KiB
Dart

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'theme_controller.dart';
class FavoritesController extends GetxController {
static const String _isGridViewKey = 'favorites_is_grid_view';
final ThemeController _themeController = Get.find<ThemeController>();
var categories = ['全部', '点赞', '笔记', '推送', '每日一句'].obs;
var isGridView = true.obs;
var currentTabIndex = 0.obs;
var searchQuery = ''.obs;
var sortByTime = true.obs; // true: 按时间排序, false: 按分类排序
var likesFirst = true.obs; // true: 点赞在前, false: 笔记在前
@override
void onInit() {
super.onInit();
_loadPreferences();
}
Future<void> _loadPreferences() async {
try {
final prefs = await SharedPreferences.getInstance();
isGridView.value = prefs.getBool(_isGridViewKey) ?? true;
} catch (e) {
debugPrint('加载收藏页面设置失败: $e');
}
}
Future<void> _savePreferences() async {
try {
final prefs = await SharedPreferences.getInstance();
await prefs.setBool(_isGridViewKey, isGridView.value);
} catch (e) {
debugPrint('保存收藏页面设置失败: $e');
}
}
void toggleViewMode() {
isGridView.value = !isGridView.value;
_savePreferences();
}
void setCurrentTabIndex(int index) {
currentTabIndex.value = index;
}
void setSearchQuery(String query) {
searchQuery.value = query;
}
Future<void> refreshContent() async {
await Future.delayed(const Duration(milliseconds: 500));
Get.snackbar('提示', '内容已刷新', colorText: _themeController.currentThemeColor);
}
void showFilterOptions(BuildContext context) {
final isDark = _themeController.isDarkMode;
final themeColor = _themeController.currentThemeColor;
showModalBottomSheet(
context: context,
backgroundColor: isDark ? const Color(0xFF2A2A2A) : Colors.white,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
),
builder: (BuildContext context) => Container(
padding: const EdgeInsets.all(16),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
width: 40,
height: 4,
margin: const EdgeInsets.only(bottom: 16),
decoration: BoxDecoration(
color: isDark ? Colors.grey[600] : Colors.grey[300],
borderRadius: BorderRadius.circular(2),
),
),
Text(
'排序选项',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: isDark ? Colors.white : Colors.black87,
),
),
const SizedBox(height: 16),
Obx(
() => ListTile(
leading: Icon(Icons.date_range, color: themeColor),
title: Text(
'按时间排序',
style: TextStyle(
color: isDark ? Colors.white : Colors.black87,
),
),
trailing: Icon(
sortByTime.value
? Icons.radio_button_checked
: Icons.radio_button_unchecked,
color: themeColor,
),
onTap: () {
Navigator.pop(context);
sortByTime.value = true;
Future.delayed(const Duration(milliseconds: 100), () {
Get.snackbar('提示', '已按时间排序', colorText: themeColor);
});
},
),
),
Obx(
() => ListTile(
leading: Icon(Icons.title, color: themeColor),
title: Text(
'按分类排序',
style: TextStyle(
color: isDark ? Colors.white : Colors.black87,
),
),
trailing: Icon(
!sortByTime.value
? Icons.radio_button_checked
: Icons.radio_button_unchecked,
color: themeColor,
),
onTap: () {
Navigator.pop(context);
sortByTime.value = false;
Future.delayed(const Duration(milliseconds: 100), () {
Get.snackbar('提示', '已按分类排序', colorText: themeColor);
});
},
),
),
const Divider(color: Colors.grey),
Obx(
() => ListTile(
leading: Icon(Icons.swap_vert, color: themeColor),
title: Text(
likesFirst.value ? '点赞在前' : '笔记在前',
style: TextStyle(
color: isDark ? Colors.white : Colors.black87,
),
),
trailing: Icon(Icons.swap_vert, color: themeColor),
onTap: () {
Navigator.pop(context);
likesFirst.value = !likesFirst.value;
Future.delayed(const Duration(milliseconds: 100), () {
Get.snackbar(
'提示',
likesFirst.value ? '点赞在前' : '笔记在前',
colorText: themeColor,
);
});
},
),
),
],
),
),
);
}
void navigateToSearch() {
Get.toNamed(
'/search',
arguments: searchQuery.value.isEmpty ? null : searchQuery.value,
);
}
void navigateToCollectNotes() {
Get.toNamed('/collect-notes');
}
}