我正在尝试根据类别进行过滤,但它在每个类别页面上显示所有产品,但我想根据类别页面进行过滤,请检查我的代码并让我知道我该怎么做。
class CategoryNavigationController extends GetxController {
final List<CategoryModel> _categories = allCategories;
List<CategoryModel> get allCategory => _categories;
int currentCategoryIndex;
String currentCategoryTitle;
List<Widget> totalCategoryTabs = [];
List<ProductModel> _filtertedProducts;
Widget currentTab;
@override
void onInit() {
super.onInit();
totalCategoryTabs = List.generate(allCategory.length, (index) {
return ProductCardList(
totalProducts: 1,
productName: "WHy",
productImage: "assets/images/chicken.png",
price: "150",
scrollDirection: Axis.vertical,
);
});
}
void selectCategory(int index) {
currentCategoryIndex = index;
currentCategoryTitle = allCategory[index].categoryName;
_filterProductData(currentCategoryTitle);
currentTab = totalCategoryTabs[index];
Get.to(() => CategoryScreen());
update();
}
void changeTab(int index) {
currentCategoryIndex = index;
currentCategoryTitle = allCategory[currentCategoryIndex].categoryName;
_filterProductData(currentCategoryTitle);
currentTab = totalCategoryTabs[currentCategoryIndex];
update();
}
void _filterProductData(query) {
print(query);
}
}
这是我的数据模型。每次用户拖动选项卡视图或点击选项卡按钮时,我都想重建当前的 TabView
class CategoryModel {
String categoryName;
String categoryImage;
String categoryIcon;
CategoryModel({
this.categoryName,
this.categoryImage,
this.categoryIcon,
});
}
List<CategoryModel> allCategories = <CategoryModel>[
CategoryModel(
categoryName: "Chicken",
categoryImage: "assets/images/chicken.png",
categoryIcon: "assets/svg/chicken.svg",
),
CategoryModel(
categoryName: "Fish",
categoryImage: "assets/images/sea_fish.png",
categoryIcon: "assets/svg/fish.svg",
),
CategoryModel(
categoryName: "Mutton",
categoryImage: "assets/images/mutton.png",
categoryIcon: "assets/svg/mutton.svg",
),
CategoryModel(
categoryName: "Marinade",
categoryImage: "assets/images/marinade.png",
categoryIcon: "assets/svg/premium_meat.svg",
),
CategoryModel(
categoryName: "Cold Cut",
categoryImage: "assets/images/sea_fish.png",
categoryIcon: "assets/svg/cold_cut.svg",
),
CategoryModel(
categoryName: "Prone",
categoryImage: "assets/images/sea_fish.png",
categoryIcon: "assets/svg/fish.svg",
),
];
class ProductModel {
String productName;
String productImage;
CategoryModel category;
String price;
String stock;
ProductModel({
this.productName,
this.category,
this.price,
this.stock,
this.productImage,
});
}
List<ProductModel> allProducts = <ProductModel>[
ProductModel(
category: allCategories[0],
productName: "chicken1",
productImage: "assets/images/chicken.png",
price: "150",
),
ProductModel(
category: allCategories[0],
productName: "chicken2",
productImage: "assets/images/chicken.png",
price: "150",
),
ProductModel(
category: allCategories[1],
productName: "fish1",
productImage: "assets/images/fish.png",
price: "150",
),
ProductModel(
category: allCategories[1],
productName: "fish2",
productImage: "assets/images/fish.png",
price: "150",
),
ProductModel(
category: allCategories[2],
productName: "mutton1",
productImage: "assets/images/mutton.png",
price: "150",
),
ProductModel(
category: allCategories[2],
productName: "mutton2",
productImage: "assets/images/mutton.png",
price: "150",
),
ProductModel(
category: allCategories[3],
productName: "marinade1",
productImage: "assets/images/marinade.png",
price: "150",
),
ProductModel(
category: allCategories[3],
productName: "marinade2",
productImage: "assets/images/marinade.png",
price: "150",
),
ProductModel(
category: allCategories[4],
productName: "ColdCut1",
productImage: "assets/images/sea_fish.png",
price: "150",
),
ProductModel(
category: allCategories[4],
productName: "ColdCut2",
productImage: "assets/images/sea_fish.png",
price: "150",
),
ProductModel(
category: allCategories[5],
productName: "prone1",
productImage: "assets/images/sea_fish.png",
price: "150",
),
ProductModel(
category: allCategories[5],
productName: "prone2",
productImage: "assets/images/sea_fish.png",
price: "150",
),
];
这是我的类别屏幕,我在其中使用 TabBar 和 TabView。
GetBuilder<CategoryNavigationController>(
builder: (controller) => Scaffold(
appBar: AppBar(
toolbarHeight: AppConfig.screenHeight(context) * 0.06,
centerTitle: true,
elevation: 0,
backgroundColor: Colors.white,
title: Text(
"MENU",
style: TextStyle(
color: Color(0xff1F1F1F),
fontWeight: FontWeight.w600,
fontStyle: FontStyle.normal,
),
),
),
body: DefaultTabController(
initialIndex: controller.currentCategoryIndex,
length: controller.allCategory.length,
child: Container(
width: double.infinity,
height: double.infinity,
child: Column(
children: [
Container(
height: AppConfig.screenHeight(context) * 0.1,
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: AppConfig.disableColor,
width: 1,
),
),
boxShadow: [
BoxShadow(
color: AppConfig.disableColor.withOpacity(0.5),
offset: Offset(0, 1),
spreadRadius: 1,
blurRadius: 1,
),
],
color: Colors.white,
),
child: TabBar(
controller: controller.tabController,
onTap: (index) {
controller.changeTab();
},
isScrollable: true,
indicator: BoxDecoration(
color: AppConfig.disableColor,
border: Border(
bottom: BorderSide(
color: AppConfig.primaryColor,
),
),
),
tabs: List.generate(
controller.allCategory.length,
(index) {
return CategoryButtons(
buttonIcon: SvgPicture.asset(
controller.allCategory[index].categoryIcon,
width: AppConfig.screenWidth(context) * 0.085,
),
buttonName:
controller.allCategory[index].categoryName,
);
},
),
),
),
Expanded(
child: TabBarView(
controller: controller.tabController,
children:
controller.allCategory.map((CategoryModel category) {
return controller.currentTab;
}).toList(),
),
),
],
),
),
),
),
);