fix category tabbar

This commit is contained in:
efrilm
2025-10-08 13:01:26 +07:00
parent 613b216c04
commit 1fbacae1f4
4 changed files with 34 additions and 3 deletions
@@ -41,6 +41,36 @@ class _CategoryTabBarState extends State<CategoryTabBar>
});
}
@override
void didUpdateWidget(CategoryTabBar oldWidget) {
super.didUpdateWidget(oldWidget);
// ✅ Update TabController when categories length changes
if (oldWidget.categories.length != widget.categories.length) {
_tabController.dispose();
_tabController = TabController(
length: widget.categories.length,
vsync: this,
initialIndex: 0, // Reset to first tab
);
_tabController.addListener(() {
if (_tabController.indexIsChanging) {
if (_tabController.index == 0) {
context.read<ProductLoaderBloc>().add(
ProductLoaderEvent.getProduct(),
);
} else {
selectedCategoryId = widget.categories[_tabController.index].id;
context.read<ProductLoaderBloc>().add(
ProductLoaderEvent.getProduct(categoryId: selectedCategoryId),
);
}
}
});
}
}
@override
void dispose() {
_tabController.dispose();