首页 > 解决方案 > 有条件地从 ListView.builder 渲染小部件

问题描述

我的颤振电子商务应用程序已连接到 Cloud Firestore 后端。我有cart几个文档的集合,products其中包含用户添加的标题。每个产品都有一个字段order_status,可能是in_cart或者checked_out值。现在我想在我的购物车屏幕上呈现它们,并根据order_status. 我正在使用列表视图。Builder方法,但由于存在itemCount我无法呈现同一集合中的特定文档。这样做的正确方法是什么?我应该将产品存储在两个集合中吗?

代码:

ListView.builder(
                  scrollDirection: Axis.vertical,
                  shrinkWrap: true,
                  itemCount: snapshot.data.length,
                  itemBuilder: (_, index) {
                    if (snapshot.data[index]['order_status'] == 'in_cart') {
                      _total = _total + snapshot.data[index]['total'];
                      return Container(
                        padding: EdgeInsets.only(bottom: 12.5),
                        child: Column(
                          children: [
                            Container(
                              height: (MediaQuery.of(context).size.height) / 7,
                              width: MediaQuery.of(context).size.width,
                              child: Container(
                                height: 105,
                                width: 85,
                                child: DecoratedBox(
                                  decoration: BoxDecoration(
                                    color: Colors.black12,
                                  ),
                                  child: Container(
                                    padding: EdgeInsets.all(10.0),
                                    child: Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.spaceBetween,
                                      crossAxisAlignment:
                                          CrossAxisAlignment.center,
                                      children: [
                                        SizedBox(
                                          width: 12.0,
                                        ),
                                        IconButton(
                                          icon: Image.network(
                                             ),
                                          iconSize: 75.0,
                                          onPressed: () {},
                                        ),
                                        SizedBox(
                                          width: 30.0,
                                        ),
                                        Column(
                                          children: [
                                            Text(
                                              snapshot.data[index]['title'],
                                              style: kHeading1.copyWith(
                                                  fontSize: 22.5),
                                            ),
                                            SizedBox(
                                              height: 5.0,
                                            ),
                                            Text(
                                              'Rs.' +
                                                  snapshot.data[index]['price']
                                                      .toString() +
                                                  '/' +
                                                  snapshot.data[index]
                                                      ['product_unit'],
                                              style: kTitle1,
                                            ),
                                            SizedBox(
                                              height: 5.0,
                                            ),
                                            Text(
                                              'Total Rs.' +
                                                  snapshot.data[index]['total']
                                                      .toString(),
                                              style: kTitle1,
                                            ),
                                          ],
                                        ),
                                        SizedBox(
                                          width: 20.0,
                                        ),
                                        Row(
                                          children: [
                                            IconButton(
                                                icon: Icon(
                                                  Icons.keyboard_arrow_up,
                                                  color: Colors.black,
                                                ),
                                                onPressed: () {
                                                  increaseCart(
                                                      snapshot.data[index]
                                                          ['title'],
                                                      snapshot.data[index]
                                                          ['unit'],
                                                      snapshot.data[index]
                                                          ['price']);
                                                }),
                                            Text(
                                              snapshot.data[index]['unit']
                                                  .toString(),
                                              style: kHeading2,
                                            ),
                                            IconButton(
                                                icon: Icon(
                                                    Icons.keyboard_arrow_down,
                                                    color: Colors.black),
                                                onPressed: () {
                                                  decreaseCart(
                                                      snapshot.data[index]
                                                          ['title'],
                                                      snapshot.data[index]
                                                          ['unit'],
                                                      snapshot.data[index]
                                                          ['price']);
                                                }),
                                          ],
                                        ),
                                      ],
                                    ),
                                  ),
                                ),
                              ),
                            ),
                          ],
                        ),
                      );
                    } else {
                      return null;
                    }
                  }),

火库火库

标签: firebaseflutterdartgoogle-cloud-firestore

解决方案


推荐阅读