底部tab的灵活可控
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:maibu_satabot_v2/features/main_container/domain/tab_config.dart';
|
||||
|
||||
class TabConfigState {
|
||||
final TabConfig config;
|
||||
final int selectedIndex;
|
||||
|
||||
const TabConfigState({
|
||||
required this.config,
|
||||
this.selectedIndex = 0,
|
||||
});
|
||||
|
||||
TabConfigState copyWith({
|
||||
TabConfig? config,
|
||||
int? selectedIndex,
|
||||
}) {
|
||||
return TabConfigState(
|
||||
config: config ?? this.config,
|
||||
selectedIndex: selectedIndex ?? this.selectedIndex,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class TabConfigCubit extends Cubit<TabConfigState> {
|
||||
TabConfigCubit() : super(TabConfigState(config: TabConfig.defaultConfig()));
|
||||
|
||||
/// 切换 Tab 的启用状态
|
||||
void toggleTab(String tabId) {
|
||||
final newConfig = state.config.toggleItem(tabId);
|
||||
|
||||
// 如果当前选中的 Tab 被禁用,自动切换到第一个启用的 Tab
|
||||
int newIndex = state.selectedIndex;
|
||||
if (newIndex >= newConfig.enabledItems.length) {
|
||||
newIndex = 0;
|
||||
}
|
||||
|
||||
emit(state.copyWith(config: newConfig, selectedIndex: newIndex));
|
||||
}
|
||||
|
||||
/// 切换选中的 Tab
|
||||
void selectTab(int index) {
|
||||
if (index >= 0 && index < state.config.enabledItems.length) {
|
||||
emit(state.copyWith(selectedIndex: index));
|
||||
}
|
||||
}
|
||||
|
||||
/// 重置为默认配置
|
||||
void resetToDefault() {
|
||||
emit(TabConfigState(config: TabConfig.defaultConfig()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user