简体   繁体   English

如何在 flutter 中的垂直列表视图中创建水平列表视图

[英]How to create Horizontal List View Inside Vertical List View in flutter

This is my Database Structure.这是我的数据库结构。 Categories is the Parent Node of Snacks and Beverages.类别是零食和饮料的父节点。

在此处输入图像描述

Here is my code for defining list:这是我定义列表的代码:

  List<CategoriesOnly> categoriesOnlyList =[];
  List<CategoryItems> categoryItemList = [];

Here is the code for Storing data into list:这是将数据存储到列表中的代码:

 var categoryName = FirebaseDatabase.instance.reference().child('Categories').once()
    .then((DataSnapshot snapShot){
      Map <dynamic, dynamic> values = snapShot.value;
      values.forEach((key1,values){
        var categoryItem = FirebaseDatabase.instance.reference().child('Categories').child(key1).once()
        .then((DataSnapshot dataSnapshot){
          print(key1);
          CategoriesOnly categoriesOnly = new CategoriesOnly(
            key1
          );
          categoriesOnlyList.add(categoriesOnly);--------Storing Category Name(i.e. Snacks and Beverages)
          var key = dataSnapshot.value.keys;
          for(var i in key){
            CategoryItems categoryItems = new CategoryItems(
                dataSnapshot.value[i]['MarketPrice'],
                dataSnapshot.value[i]['Name'],
                dataSnapshot.value[i]['OurPrice'],
                dataSnapshot.value[i]['TotalDiscount'],
                dataSnapshot.value[i]['Weight']
            );
            categoryItemList.add(categoryItems);-----Storing all their respective item.
          }
          
        });

        });
    });

Here is code for printing Category Name in Vertical ListBuilder and their Item into Horizontal ListBuilder:下面是在 Vertical ListBuilder 中打印 Category Name 并将它们的 Item 打印到 Horizontal ListBuilder 中的代码:

  Widget build(BuildContext context) {
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
      statusBarColor: Colors.black, //or set color with: Color(0xFF0000FF)
    ));
    return Scaffold(
      backgroundColor: Colors.blue,
      body: SafeArea(

        child:  ListView.builder(
          itemCount: categoriesOnlyList.length,
          itemBuilder: (context, index) =>
              Column(children: [
                Text(categoriesOnlyList[index].Name),
                Container(
                  height: 100,
                  child:
                  ListView.builder(itemCount: categoryItemList.length,
                      scrollDirection: Axis.horizontal,
                      itemBuilder: (context, index) => Text(categoryItemList[index].Name)),
                ),

              ]),
        )
      ),
    );
  }
}

Here is what I got:这是我得到的:

在此处输入图像描述

Inside of each Category name I got all the items of both categories(ie Inside Snacks I got all the items of Snacks and Beverages and same for Beverages ).在每个类别名称中,我得到了两个类别的所有项目(即在零食里面,我得到了零食和饮料的所有项目,饮料也是如此)。 But I want my code to display only those item which belongs to their parent category name.但我希望我的代码仅显示属于其父类别名称的那些项目。

In your CategoryItem class save the category name as well.在您的 CategoryItem class 中也保存类别名称。 I dont know why you are not doing that.我不知道你为什么不这样做。 Like this:像这样:

CategoryItems categoryItems = new CategoryItems(
                key, // your category type 
                dataSnapshot.value[i]['MarketPrice'],
                dataSnapshot.value[i]['Name'],
                dataSnapshot.value[i]['OurPrice'],
                dataSnapshot.value[i]['TotalDiscount'],
                dataSnapshot.value[i]['Weight']
            );

When you are building the listView using builder do this:当您使用 builder 构建 listView 时,请执行以下操作:

return Scaffold(
      backgroundColor: Colors.blue,
      body: SafeArea(

        child:  ListView.builder(
          itemCount: categoriesOnlyList.length,
          itemBuilder: (context, index) {
              List abc = categoryItemList.where((item) => item.key == categoriesOnlyList[index]).toList();
              return Column(children: [
                Text(categoriesOnlyList[index].Name),
                Container(
                  height: 100,
                  child:
                  ListView.builder(
                     itemBuilder: (c, i) {
                         return Text(abc[i].Name);
                     },
                     itemCount: abc.length,
                 ),

              ]);
          }
        )
      ),
    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM