From 1fbacae1f405e96f0789a87ec7872b77023cc5c2 Mon Sep 17 00:00:00 2001 From: efrilm Date: Wed, 8 Oct 2025 13:01:26 +0700 Subject: [PATCH] fix category tabbar --- .../category_loader/category_loader_bloc.dart | 4 +-- lib/presentation/home/pages/home_page.dart | 1 + .../home/widgets/category_tab_bar.dart | 30 +++++++++++++++++++ pubspec.yaml | 2 +- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/lib/presentation/home/bloc/category_loader/category_loader_bloc.dart b/lib/presentation/home/bloc/category_loader/category_loader_bloc.dart index 98b37d2..4721532 100644 --- a/lib/presentation/home/bloc/category_loader/category_loader_bloc.dart +++ b/lib/presentation/home/bloc/category_loader/category_loader_bloc.dart @@ -60,13 +60,13 @@ class CategoryLoaderBloc }, (response) async { final categories = response.data.categories; + categories.insert(0, CategoryModel.all()); + final totalPages = response.data.totalPages; final hasReachedMax = categories.length < 10 || 1 >= totalPages; log('✅ Categories loaded: ${categories.length}, hasReachedMax: $hasReachedMax'); - categories.insert(0, CategoryModel.all()); - emit(CategoryLoaderState.loaded( categories: categories, hasReachedMax: hasReachedMax, diff --git a/lib/presentation/home/pages/home_page.dart b/lib/presentation/home/pages/home_page.dart index 14dd377..505986c 100644 --- a/lib/presentation/home/pages/home_page.dart +++ b/lib/presentation/home/pages/home_page.dart @@ -228,6 +228,7 @@ class _HomePageState extends State { child: BlocBuilder( builder: (context, productState) { return CategoryTabBar( + key: ValueKey(categories.length), categories: categories, tabViews: categories.map((category) { return SizedBox( diff --git a/lib/presentation/home/widgets/category_tab_bar.dart b/lib/presentation/home/widgets/category_tab_bar.dart index 8dbbd6f..e2c88ad 100644 --- a/lib/presentation/home/widgets/category_tab_bar.dart +++ b/lib/presentation/home/widgets/category_tab_bar.dart @@ -41,6 +41,36 @@ class _CategoryTabBarState extends State }); } + @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().add( + ProductLoaderEvent.getProduct(), + ); + } else { + selectedCategoryId = widget.categories[_tabController.index].id; + context.read().add( + ProductLoaderEvent.getProduct(categoryId: selectedCategoryId), + ); + } + } + }); + } + } + @override void dispose() { _tabController.dispose(); diff --git a/pubspec.yaml b/pubspec.yaml index 6879e02..b4d40c8 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,7 +16,7 @@ publish_to: "none" # Remove this line if you wish to publish to pub.dev # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. -version: 1.0.2+4 +version: 1.0.2+5 environment: sdk: ">=3.2.4 <4.0.0"