关怀模式
This commit is contained in:
87
lib/services/get/care_controller.dart
Normal file
87
lib/services/get/care_controller.dart
Normal file
@@ -0,0 +1,87 @@
|
||||
import 'package:get/get.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class CareController extends GetxController {
|
||||
static const String _careModeKey = 'care_mode_enabled';
|
||||
static const String _userTypeKey = 'care_user_type';
|
||||
static const String _pinyinEnabledKey = 'care_pinyin_enabled';
|
||||
static const String _selectedOptionsKey = 'care_selected_options';
|
||||
static const String _careNavigationIndexKey = 'care_navigation_index';
|
||||
|
||||
final RxBool _isCareModeEnabled = false.obs;
|
||||
final RxString _userType = '儿童'.obs;
|
||||
final RxBool _pinyinEnabled = false.obs;
|
||||
final RxSet<String> _selectedOptions = {'诗词', '出处'}.obs;
|
||||
final RxBool isCareButtonVisible = false.obs;
|
||||
final RxInt _careNavigationIndex = 0.obs;
|
||||
|
||||
bool get isCareModeEnabled => _isCareModeEnabled.value;
|
||||
String get userType => _userType.value;
|
||||
bool get pinyinEnabled => _pinyinEnabled.value;
|
||||
Set<String> get selectedOptions => _selectedOptions.toSet();
|
||||
int get careNavigationIndex => _careNavigationIndex.value;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
_loadCareSettings();
|
||||
}
|
||||
|
||||
Future<void> _loadCareSettings() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
_isCareModeEnabled.value = prefs.getBool(_careModeKey) ?? false;
|
||||
_userType.value = prefs.getString(_userTypeKey) ?? '儿童';
|
||||
_pinyinEnabled.value = prefs.getBool(_pinyinEnabledKey) ?? false;
|
||||
_careNavigationIndex.value = prefs.getInt(_careNavigationIndexKey) ?? 0;
|
||||
|
||||
final savedOptions = prefs.getStringList(_selectedOptionsKey);
|
||||
if (savedOptions != null && savedOptions.isNotEmpty) {
|
||||
_selectedOptions.clear();
|
||||
_selectedOptions.addAll(savedOptions);
|
||||
if (!_selectedOptions.contains('诗词')) {
|
||||
_selectedOptions.add('诗词');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> toggleCareMode() async {
|
||||
_isCareModeEnabled.value = !_isCareModeEnabled.value;
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setBool(_careModeKey, _isCareModeEnabled.value);
|
||||
}
|
||||
|
||||
Future<void> setUserType(String type) async {
|
||||
_userType.value = type;
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString(_userTypeKey, type);
|
||||
}
|
||||
|
||||
Future<void> setPinyinEnabled(bool enabled) async {
|
||||
_pinyinEnabled.value = enabled;
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setBool(_pinyinEnabledKey, enabled);
|
||||
}
|
||||
|
||||
Future<void> toggleOption(String option) async {
|
||||
if (option == '诗词') return; // 诗词选项不可取消
|
||||
|
||||
if (_selectedOptions.contains(option)) {
|
||||
_selectedOptions.remove(option);
|
||||
} else {
|
||||
_selectedOptions.add(option);
|
||||
}
|
||||
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setStringList(_selectedOptionsKey, _selectedOptions.toList());
|
||||
}
|
||||
|
||||
void toggleCareButtonVisibility() {
|
||||
isCareButtonVisible.value = !isCareButtonVisible.value;
|
||||
}
|
||||
|
||||
Future<void> switchCareNavigation(int index) async {
|
||||
_careNavigationIndex.value = index;
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setInt(_careNavigationIndexKey, index);
|
||||
}
|
||||
}
|
||||
@@ -28,9 +28,13 @@ class FavoritesController extends GetxController {
|
||||
}
|
||||
|
||||
void showFilterOptions(BuildContext context) {
|
||||
// 先获取当前值,避免在弹窗中使用 Obx
|
||||
final currentSortByTime = sortByTime.value;
|
||||
final currentLikesFirst = likesFirst.value;
|
||||
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
builder: (context) => Container(
|
||||
builder: (BuildContext context) => Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
@@ -43,54 +47,51 @@ class FavoritesController extends GetxController {
|
||||
ListTile(
|
||||
leading: const Icon(Icons.date_range),
|
||||
title: const Text('按时间排序'),
|
||||
trailing: Obx(
|
||||
() => Icon(
|
||||
sortByTime.value
|
||||
? Icons.radio_button_checked
|
||||
: Icons.radio_button_unchecked,
|
||||
color: AppConstants.primaryColor,
|
||||
),
|
||||
trailing: Icon(
|
||||
currentSortByTime
|
||||
? Icons.radio_button_checked
|
||||
: Icons.radio_button_unchecked,
|
||||
color: AppConstants.primaryColor,
|
||||
),
|
||||
onTap: () {
|
||||
Navigator.pop(context);
|
||||
sortByTime.value = true;
|
||||
Get.snackbar('提示', '已按时间排序');
|
||||
// 触发排序更新
|
||||
update();
|
||||
// 使用 Future.delayed 确保弹窗完全关闭后再显示 snackbar
|
||||
Future.delayed(const Duration(milliseconds: 100), () {
|
||||
Get.snackbar('提示', '已按时间排序');
|
||||
});
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.title),
|
||||
title: const Text('按分类排序'),
|
||||
trailing: Obx(
|
||||
() => Icon(
|
||||
!sortByTime.value
|
||||
? Icons.radio_button_checked
|
||||
: Icons.radio_button_unchecked,
|
||||
color: AppConstants.primaryColor,
|
||||
),
|
||||
trailing: Icon(
|
||||
!currentSortByTime
|
||||
? Icons.radio_button_checked
|
||||
: Icons.radio_button_unchecked,
|
||||
color: AppConstants.primaryColor,
|
||||
),
|
||||
onTap: () {
|
||||
Navigator.pop(context);
|
||||
sortByTime.value = false;
|
||||
Get.snackbar('提示', '已按分类排序');
|
||||
// 触发排序更新
|
||||
update();
|
||||
// 使用 Future.delayed 确保弹窗完全关闭后再显示 snackbar
|
||||
Future.delayed(const Duration(milliseconds: 100), () {
|
||||
Get.snackbar('提示', '已按分类排序');
|
||||
});
|
||||
},
|
||||
),
|
||||
const Divider(),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.swap_vert),
|
||||
title: Text(likesFirst.value ? '点赞在前' : '笔记在前'),
|
||||
trailing: Obx(
|
||||
() => Icon(Icons.swap_vert, color: AppConstants.primaryColor),
|
||||
),
|
||||
title: Text(currentLikesFirst ? '点赞在前' : '笔记在前'),
|
||||
trailing: Icon(Icons.swap_vert, color: AppConstants.primaryColor),
|
||||
onTap: () {
|
||||
Navigator.pop(context);
|
||||
likesFirst.value = !likesFirst.value;
|
||||
Get.snackbar('提示', likesFirst.value ? '点赞在前' : '笔记在前');
|
||||
// 触发顺序更新
|
||||
update();
|
||||
// 使用 Future.delayed 确保弹窗完全关闭后再显示 snackbar
|
||||
Future.delayed(const Duration(milliseconds: 100), () {
|
||||
Get.snackbar('提示', likesFirst.value ? '点赞在前' : '笔记在前');
|
||||
});
|
||||
},
|
||||
),
|
||||
],
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import '../../models/colors/theme_colors.dart';
|
||||
import '../../models/night-mode/theme_model.dart';
|
||||
|
||||
/// 主题控制器
|
||||
@@ -40,6 +41,14 @@ class ThemeController extends GetxController {
|
||||
bool get enableAnimation => _enableAnimation.value;
|
||||
bool get enableBlurEffect => _enableBlurEffect.value;
|
||||
|
||||
// 获取当前主题颜色
|
||||
Color get currentThemeColor =>
|
||||
ThemeColors.getThemeColor(_themeColorIndex.value);
|
||||
|
||||
// 获取当前强调色
|
||||
Color get currentAccentColor =>
|
||||
ThemeColors.getAccentColor(_accentColorIndex.value);
|
||||
|
||||
/// 获取当前 Flutter ThemeMode
|
||||
ThemeMode get currentThemeMode {
|
||||
if (_isDarkMode.value) {
|
||||
@@ -71,9 +80,11 @@ class ThemeController extends GetxController {
|
||||
_prefs = await SharedPreferences.getInstance();
|
||||
|
||||
_isDarkMode.value = _prefs?.getBool(_darkModeKey) ?? false;
|
||||
_themeMode.value = AppThemeMode.values[
|
||||
(_prefs?.getInt(_themeModeKey) ?? 0).clamp(0, AppThemeMode.values.length - 1)
|
||||
];
|
||||
_themeMode.value =
|
||||
AppThemeMode.values[(_prefs?.getInt(_themeModeKey) ?? 0).clamp(
|
||||
0,
|
||||
AppThemeMode.values.length - 1,
|
||||
)];
|
||||
_themeColorIndex.value = _prefs?.getInt(_themeColorIndexKey) ?? 0;
|
||||
_accentColorIndex.value = _prefs?.getInt(_accentColorIndexKey) ?? 0;
|
||||
_fontSizeIndex.value = _prefs?.getInt(_fontSizeIndexKey) ?? 1;
|
||||
@@ -102,6 +113,14 @@ class ThemeController extends GetxController {
|
||||
Get.changeThemeMode(currentThemeMode);
|
||||
}
|
||||
|
||||
/// 应用主题颜色到 GetX
|
||||
void _applyThemeColor() {
|
||||
// 这里可以根据需要创建新的主题并应用
|
||||
// 由于我们使用的是 GetX 的主题系统,
|
||||
// 我们可以通过更新全局状态来让组件响应主题颜色变化
|
||||
update();
|
||||
}
|
||||
|
||||
/// 切换深色模式
|
||||
/// [enabled] true 开启深色模式, false 关闭深色模式
|
||||
Future<void> toggleDarkMode(bool enabled) async {
|
||||
@@ -144,6 +163,17 @@ class ThemeController extends GetxController {
|
||||
|
||||
_themeColorIndex.value = index;
|
||||
await _saveThemeSettings();
|
||||
_applyThemeColor();
|
||||
|
||||
// 显示提示
|
||||
Get.snackbar(
|
||||
'主题颜色',
|
||||
'已更换主题颜色',
|
||||
snackPosition: SnackPosition.BOTTOM,
|
||||
duration: const Duration(seconds: 2),
|
||||
margin: const EdgeInsets.all(16),
|
||||
borderRadius: 12,
|
||||
);
|
||||
}
|
||||
|
||||
/// 设置强调色索引
|
||||
@@ -152,6 +182,17 @@ class ThemeController extends GetxController {
|
||||
|
||||
_accentColorIndex.value = index;
|
||||
await _saveThemeSettings();
|
||||
_applyThemeColor();
|
||||
|
||||
// 显示提示
|
||||
Get.snackbar(
|
||||
'强调颜色',
|
||||
'已更换强调颜色',
|
||||
snackPosition: SnackPosition.BOTTOM,
|
||||
duration: const Duration(seconds: 2),
|
||||
margin: const EdgeInsets.all(16),
|
||||
borderRadius: 12,
|
||||
);
|
||||
}
|
||||
|
||||
/// 设置字体大小索引
|
||||
|
||||
Reference in New Issue
Block a user