首页 > 解决方案 > 如何在 ListView.builder 中从上一个列表停止的位置开始 itemCount?

问题描述

我有 2 行显示兴趣的 Listview 构建器(可水平滚动)。

希望第一行显示前 6 个兴趣,下一行显示列表中的其余部分。

如何将第二个中的 itemCount 设置为从 7 一直到最后的范围?

任何帮助表示赞赏。代码片段如下:

Padding(
                      padding: const EdgeInsets.fromLTRB(6, 6, 0, 0),
                      child: Container(
                        width: MediaQuery.of(context).size.width,
                        height: MediaQuery.of(context).size.height * 0.045,
                        child: ListView.builder(
                            shrinkWrap: true,
                            scrollDirection: Axis.horizontal,
                            padding: const EdgeInsets.all(1),
                            itemCount: 6,
                            itemBuilder: (context, int index) {
                              return Interests2(AvailableInterestChosen(
                                allInterests[index],
                                isChosen: false,
                              ));
                            }),
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.fromLTRB(6, 3, 0, 0),
                      child: Container(
                        width: MediaQuery.of(context).size.width,
                        height: MediaQuery.of(context).size.height * 0.045,
                        child: ListView.builder(
                            shrinkWrap: true,
                            scrollDirection: Axis.horizontal,
                            padding: const EdgeInsets.all(1),
                            **// itemCount: allInterests range from 6 to end of list,**
                            itemBuilder: (context, int index) {
                              return Interests2(AvailableInterestChosen(
                                allInterests[index],
                                isChosen: false,
                              ));
                            }),
                      ),
                    ),
...

第二个填充中的粗体部分是我在构建范围时遇到问题的地方。

标签: flutterlistviewcountrange

解决方案


作为第二个列表视图的参数,您可以使用 allInterests.size()-6 来计算项目数,使用 index + 6 来访问 allInterests 列表的索引。


推荐阅读