feat: Tab Category Home

This commit is contained in:
efrilm
2025-08-06 17:41:04 +07:00
parent 7ee7db225c
commit 561aec371d
10 changed files with 1138 additions and 224 deletions
@@ -0,0 +1,96 @@
import 'package:enaklo_pos/core/constants/colors.dart';
import 'package:enaklo_pos/data/models/response/category_response_model.dart';
import 'package:enaklo_pos/presentation/home/bloc/product_loader/product_loader_bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class CategoryTabBar extends StatefulWidget {
final List<CategoryModel> categories;
final List<Widget> tabViews;
const CategoryTabBar(
{super.key, required this.categories, required this.tabViews});
@override
State<CategoryTabBar> createState() => _CategoryTabBarState();
}
class _CategoryTabBarState extends State<CategoryTabBar>
with SingleTickerProviderStateMixin {
late TabController _tabController;
String? selectedCategoryId;
@override
void initState() {
super.initState();
_tabController =
TabController(length: widget.categories.length, vsync: this);
_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();
super.dispose();
}
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: widget.categories.length,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Material(
elevation: 0,
color: Colors.white,
borderOnForeground: false,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 16),
child: TabBar(
controller: _tabController,
isScrollable: true,
tabAlignment: TabAlignment.start,
labelColor: AppColors.primary,
labelStyle: TextStyle(
fontWeight: FontWeight.bold,
),
dividerColor: AppColors.primary,
unselectedLabelColor: AppColors.primary,
indicatorSize: TabBarIndicatorSize.label,
indicatorWeight: 4,
indicatorColor: AppColors.primary,
tabs: widget.categories
.map((category) => Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Tab(text: category.name),
))
.toList(),
),
),
),
Expanded(
// ✅ ini bagian penting
child: TabBarView(
controller: _tabController,
children: widget.tabViews,
),
),
],
),
);
}
}