所有一级页面和不分二级页面
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'dart:convert';
|
||||
@@ -9,8 +10,9 @@ class TabConfigInitial extends TabConfigState {}
|
||||
|
||||
class TabConfigLoaded extends TabConfigState {
|
||||
final TabConfig config;
|
||||
final int selectedIndex;
|
||||
|
||||
TabConfigLoaded(this.config);
|
||||
TabConfigLoaded(this.config, {this.selectedIndex = 0});
|
||||
}
|
||||
|
||||
class TabConfigCubit extends Cubit<TabConfigState> {
|
||||
@@ -22,31 +24,13 @@ class TabConfigCubit extends Cubit<TabConfigState> {
|
||||
}
|
||||
|
||||
void _loadConfig() {
|
||||
try {
|
||||
final configJson = _prefs.getString(_configKey);
|
||||
if (configJson != null) {
|
||||
final List<dynamic> decoded = jsonDecode(configJson);
|
||||
final items = decoded.map((e) {
|
||||
final map = Map<String, dynamic>.from(e);
|
||||
return TabConfigItem(
|
||||
id: map['id'] as String,
|
||||
name: map['name'] as String,
|
||||
nameEn: map['nameEn'] as String,
|
||||
icon: map['icon'] as String,
|
||||
isEnabled: map['isEnabled'] as bool,
|
||||
order: map['order'] as int,
|
||||
);
|
||||
}).toList();
|
||||
emit(TabConfigLoaded(TabConfig(items: items)));
|
||||
} else {
|
||||
final defaultConfig = TabConfig.defaultConfig();
|
||||
_saveConfig(defaultConfig);
|
||||
emit(TabConfigLoaded(defaultConfig));
|
||||
}
|
||||
} catch (e) {
|
||||
final defaultConfig = TabConfig.defaultConfig();
|
||||
emit(TabConfigLoaded(defaultConfig));
|
||||
}
|
||||
debugPrint('🔍 [TabConfigCubit] _loadConfig 被调用');
|
||||
// 直接使用代码中的默认配置,不持久化
|
||||
final defaultConfig = TabConfig.defaultConfig();
|
||||
debugPrint('🔍 [TabConfigCubit] 默认配置 Tab 数量: ${defaultConfig.items.length}');
|
||||
debugPrint('🔍 [TabConfigCubit] 启用的 Tab: ${defaultConfig.enabledItems.map((e) => e.id).toList()}');
|
||||
emit(TabConfigLoaded(defaultConfig));
|
||||
debugPrint('✅ [TabConfigCubit] 状态已 emit');
|
||||
}
|
||||
|
||||
void _saveConfig(TabConfig config) {
|
||||
@@ -70,17 +54,25 @@ class TabConfigCubit extends Cubit<TabConfigState> {
|
||||
final currentState = state as TabConfigLoaded;
|
||||
final newConfig = currentState.config.toggleItem(itemId);
|
||||
|
||||
// 只有配置真正改变时才保存和 emit
|
||||
if (newConfig != currentState.config) {
|
||||
_saveConfig(newConfig);
|
||||
emit(TabConfigLoaded(newConfig));
|
||||
// 不保存,直接更新状态
|
||||
emit(TabConfigLoaded(newConfig, selectedIndex: currentState.selectedIndex));
|
||||
}
|
||||
}
|
||||
|
||||
void selectTab(int index) {
|
||||
if (state is TabConfigLoaded) {
|
||||
final currentState = state as TabConfigLoaded;
|
||||
final enabledTabs = currentState.config.enabledItems;
|
||||
|
||||
// 确保索引在有效范围内
|
||||
if (index >= 0 && index < enabledTabs.length) {
|
||||
emit(TabConfigLoaded(currentState.config, selectedIndex: index));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void resetToDefault() {
|
||||
final defaultConfig = TabConfig.defaultConfig();
|
||||
_saveConfig(defaultConfig);
|
||||
emit(TabConfigLoaded(defaultConfig));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user