ListView.builder和Wrap小部件会重复数据。

huangapple go评论57阅读模式
英文:

ListView.builder with Wrap widget duplicates data

问题

我有一个包含日期的列表。我使用ListView.builder中的Wrap小部件显示这个日期列表,数据显示出来,但出现了重复...

如何去除重复项?

ListView.builder(
  physics: const NeverScrollableScrollPhysics(),
  scrollDirection: Axis.vertical,
  shrinkWrap: true,
  itemCount: days == null ? 0 : days.length,
  itemBuilder: (BuildContext context, int index) {
    return Wrap(
      children: <Widget>[
        ...days.map((date) {
          return Container(
            width: MediaQuery.of(context).size.width / 2.4,
            decoration: const BoxDecoration(
              borderRadius: BorderRadius.all(Radius.circular(20.0)),
              color: Colors.white,
            ),
            child: ListTile(
              onTap: () {
                Navigator.pushNamed(context, '/logEntry');
              },
              title: const DefaultText(
                size: 18,
                text: "Day 1",
                color: Colors.green,
                weight: FontWeight.w500,
              ),
              subtitle: DefaultText(
                size: 15,
                text: "${date.day}/${date.month}/${date.year}".toString(),
                color: Colors.green,
                weight: FontWeight.w500,
              ),
              trailing: const Icon(Icons.arrow_forward_ios),
            ),
          );
        }).toList(),
      ],
    );
  },
),
英文:

I have a list which contains days. I displayed this list of days using Wrap widget in a ListView.builder, the data displays but it duplicates...

How can I remove the duplications?

 ListView.builder(
                    physics: const NeverScrollableScrollPhysics(),
                    scrollDirection: Axis.vertical,
                    shrinkWrap: true,
                    itemCount: days == null ? 0 : days.length,
                    itemBuilder: (BuildContext context, int index) {
                      return Wrap(
                        children: &lt;Widget&gt;[
                          ...days.map((date) {
                            return Container(
                              width: MediaQuery.of(context).size.width / 2.4,
                              decoration: const BoxDecoration(
                                borderRadius:
                                    BorderRadius.all(Radius.circular(20.0)),
                                color: Colors.white,
                              ),
                              child: ListTile(
                                onTap: () {
                                  Navigator.pushNamed(context, &#39;/logEntry&#39;);
                                },
                                title: const DefaultText(
                                  size: 18,
                                  text: &quot;Day 1&quot;,
                                  color: Colors.green,
                                  weight: FontWeight.w500,
                                ),
                                subtitle: DefaultText(
                                  size: 15,
                                  text: &quot;${date.day}/${date.month}/${date.year}&quot;
                                      .toString(),
                                  color: Colors.green,
                                  weight: FontWeight.w500,
                                ),
                                trailing: const Icon(Icons.arrow_forward_ios),
                              ),
                            );
                          }).toList(),
                        ],
                      );
                    },
                  ),
               

答案1

得分: 0

你不需要像这样使用ListView.builder,只需像这样使用ListView来处理高度溢出。

ListView(
  children: [
    Wrap(),
  ],
),
英文:

You don't need to use ListView.builder like with wrap like this. You can shift to ListView just to handle height-overflow.

ListView(
  children: [
    Wrap(),
  ],
),

huangapple
  • 本文由 发表于 2023年2月18日 00:54:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定