Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振:如何使用GetX查询列表项_Flutter_Listview_Filter_Flutter Getx - Fatal编程技术网

Flutter 颤振:如何使用GetX查询列表项

Flutter 颤振:如何使用GetX查询列表项,flutter,listview,filter,flutter-getx,Flutter,Listview,Filter,Flutter Getx,我正在尝试根据类别进行筛选,但它会在每个类别页面上显示所有产品,但我想根据类别页面进行筛选,请检查我的代码并让我知道如何进行筛选 class CategoryNavigationController extends GetxController { final List<CategoryModel> _categories = allCategories; List<CategoryModel> get allCategory => _categorie

我正在尝试根据类别进行筛选,但它会在每个类别页面上显示所有产品,但我想根据类别页面进行筛选,请检查我的代码并让我知道如何进行筛选

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);
  }
}
class CategoryNavigationController扩展了GetxController{
最终列表_categories=所有类别;
列表获取所有类别=>\u类别;
int currentCategoryIndex;
字符串currentCategoryTitle;
列表totalCategoryTabs=[];
列出过滤产品;
控件当前选项卡;
@凌驾
void onInit(){
super.onInit();
totalCategoryTabs=List.generate(allCategory.length,(索引){
返回产品卡片列表(
总体产品:1,
产品名称:“为什么”,
productImage:“assets/images/chicken.png”,
价格:“150”,
滚动方向:轴垂直,
);
});
}
void selectCategory(int索引){
currentCategoryIndex=索引;
currentCategoryTitle=allCategory[index]。categoryName;
_filterProductData(currentCategoryTitle);
currentTab=总分类选项卡[索引];
Get.to(()=>CategoryScreen());
更新();
}
void changeTab(int索引){
currentCategoryIndex=索引;
currentCategoryTitle=allCategory[currentCategoryIndex].categoryName;
_filterProductData(currentCategoryTitle);
currentTab=totalCategoryTabs[currentCategoryIndex];
更新();
}
void\u filterProductData(查询){
打印(查询);
}
}
这是我的数据模型。每次用户拖动tab view或点击tab按钮时,我都希望重建当前的tab view

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",
  ),
];
类类别模型{
字符串类别名称;
字符串分类图像;
串分类;
分类模型({
这个.categoryName,
这个。分类图像,
这个,分类号,
});
}
列出所有类别=[
分类模型(
类别名称:“鸡肉”,
类别图像:“资产/图像/鸡.png”,
categoryIcon:“assets/svg/chicken.svg”,
),
分类模型(
类别名称:“鱼”,
分类图像:“资产/图像/海洋鱼.png”,
categoryIcon:“assets/svg/fish.svg”,
),
分类模型(
类别名称:“羊肉”,
类别图像:“资产/图像/mutton.png”,
categoryIcon:“assets/svg/mutton.svg”,
),
分类模型(
类别名称:“卤汁”,
categoryImage:“资产/图像/marinade.png”,
categoryIcon:“assets/svg/premium_meat.svg”,
),
分类模型(
类别名称:“冷切”,
分类图像:“资产/图像/海洋鱼.png”,
categoryIcon:“assets/svg/cold_cut.svg”,
),
分类模型(
categoryName:“俯卧”,
分类图像:“资产/图像/海洋鱼.png”,
categoryIcon:“assets/svg/fish.svg”,
),
];
类产品模型{
字符串名称;
字符串图像;
范畴模型范畴;
串价;
串股;
产品模型({
此.productName,
这一类,,
这个价格,,
这个股票,
这个产品形象,
});
}
列出所有产品=[
产品模型(
类别:所有类别[0],
产品名称:“chicken1”,
productImage:“assets/images/chicken.png”,
价格:“150”,
),
产品模型(
类别:所有类别[0],
产品名称:“chicken2”,
productImage:“assets/images/chicken.png”,
价格:“150”,
),
产品模型(
类别:所有类别[1],
产品名称:“fish1”,
productImage:“assets/images/fish.png”,
价格:“150”,
),
产品模型(
类别:所有类别[1],
产品名称:“fish2”,
productImage:“assets/images/fish.png”,
价格:“150”,
),
产品模型(
类别:所有类别[2],
产品名称:“羊肉1”,
productImage:“assets/images/mutton.png”,
价格:“150”,
),
产品模型(
类别:所有类别[2],
产品名称:“羊肉2”,
productImage:“assets/images/mutton.png”,
价格:“150”,
),
产品模型(
类别:所有类别[3],
产品名称:“marinade1”,
productImage:“assets/images/marinade.png”,
价格:“150”,
),
产品模型(
类别:所有类别[3],
产品名称:“marinade2”,
productImage:“assets/images/marinade.png”,
价格:“150”,
),
产品模型(
类别:所有类别[4],
产品名称:“ColdCut1”,
productImage:“assets/images/sea_fish.png”,
价格:“150”,
),
产品模型(
类别:所有类别[4],
产品名称:“ColdCut2”,
productImage:“assets/images/sea_fish.png”,
价格:“150”,
),
产品模型(
类别:所有类别[5],
产品名称:“prone1”,
productImage:“assets/images/sea_fish.png”,
价格:“150”,
),
产品模型(
类别:所有类别[5],
产品名称:“prone2”,
productImage:“assets/images/sea_fish.png”,
价格:“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(),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
GetBuilder(
建造商:(控制员)=>脚手架(
appBar:appBar(
工具栏高度:AppConfig.screenHeight(上下文)*0.06,
标题:对,
海拔:0,
背景颜色:Colors.white,
标题:正文(
“菜单”,
样式:TextStyle(
颜色:颜色(0xFF1F),
fontWeight:fontWeight.w600,
fontStyle:fontStyle.normal,
),
),
),
正文:DefaultTabController(
initialIndex:controller.currentCategoryIndex,
长度:controller.allCategory.length,
子:容器(
宽度:double.infinity,
高度:双无限,
子:列(
儿童:[
容器(
高度:AppConfig.screenHeight(上下文)*0.1,
装饰:盒子装饰(
边界:边界(
底部:边界侧(
颜色:AppConfig.disableColor,
宽度:1,
),
),
boxShadow:[
箱形阴影(
有限公司
final List<CategoryModel> _categories = allCategories;

List<CategoryModel> get allCategory => _categories;
  List<ProductModel> masterProductList = [];

  List<ProductModel> chickenList = [];
  List<ProductModel> fishList = [];
  List<ProductModel> muttonList = [];
  List<ProductModel> marinadeList = [];
  List<ProductModel> coldCutList = [];
  List<ProductModel> proneList = [];
void addProduct(ProductModel product) {
    masterProductList.add(product);
    switch (product.category.categoryName) {
      case 'Chicken':
        chickenList.add(product);
        tabController.animateTo(0); // this jumps to whatever tab you want based on the index you pass in
        break;
      case 'Fish':
        fishList.add(product);
        tabController.animateTo(1);
        break;
      case 'Mutton':
        muttonList.add(product);
        tabController.animateTo(2);
        break;
      case 'Marinade':
        marinadeList.add(product);
        tabController.animateTo(3);
        break;
      case 'Cold Cut':
        coldCutList.add(product);
        tabController.animateTo(4);
        break;
      case 'Prone':
        proneList.add(product);
        tabController.animateTo(5);
        break;
    }
    update();
  }
class TabHome extends GetView<CategoryNavigationController> {
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: controller.allCategories.length,
      child: Scaffold(
        appBar: AppBar(
          bottom: TabBar(
            tabs: controller.totalCategoryTabs,
            controller: controller.tabController,
          ),
          title: Text('Categories'),
        ),
        body: TabBarView(
          controller: controller.tabController,
          children: [
            ProductPage(list: controller.chickenList), // passing in corresponding list from GetX class
            ProductPage(list: controller.fishList),
            ProductPage(list: controller.muttonList),
            ProductPage(list: controller.marinadeList),
            ProductPage(list: controller.coldCutList),
            ProductPage(list: controller.proneList),
          ],
        ),
      ),
    );
  }
}
class ProductPage extends StatelessWidget {
  final List list;

  const ProductPage({Key key, this.list}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          GetBuilder<CategoryNavigationController>(
            builder: (controller) => Expanded(
              child: ListView.builder(
                itemCount: list.length,
                itemBuilder: (context, index) => ProductRow(product: list[index]),
              ),
            ),
          ),
          ButtonRow(),
        ],
      ),
    );
  }
}
class ProductRow extends StatelessWidget {
  final ProductModel product;

  const ProductRow({Key key, this.product}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Row(children: [Text(product.productName)]);
  }
}
 Expanded(
              child: ElevatedButton(
                onPressed: () {
                  controller.addProduct(
                    ProductModel(
                      productName: 'boneless chicken',
                      category: CategoryModel(categoryName: 'Chicken'),
                    ),
                  );
                },
                child: Text('Add chicken'),
              ),
            ),
  @override
  void onInit() {
    super.onInit();
    totalCategoryTabs = List.generate(allCategories.length, (index) {
      return ProductCardList(
        totalProducts: 1,
        productName: allCategories[index].categoryName, // auto names your tabs based on allCategories list
        productImage: "assets/images/chicken.png",
        price: "150",
        scrollDirection: Axis.vertical,
      );
    });
    tabController =
        TabController(length: totalCategoryTabs.length, vsync: this); // made possible with SingleGetTickerProviderMixin 
  }