繁体   English   中英

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

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

这是我的数据库结构。 类别是零食和饮料的父节点。

在此处输入图像描述

这是我定义列表的代码:

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

这是将数据存储到列表中的代码:

 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.
          }
          
        });

        });
    });

下面是在 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)),
                ),

              ]),
        )
      ),
    );
  }
}

这是我得到的:

在此处输入图像描述

在每个类别名称中,我得到了两个类别的所有项目(即在零食里面,我得到了零食和饮料的所有项目,饮料也是如此)。 但我希望我的代码仅显示属于其父类别名称的那些项目。

在您的 CategoryItem class 中也保存类别名称。 我不知道你为什么不这样做。 像这样:

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']
            );

当您使用 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