gpt4 book ai didi

flutter - 如何迭代列表以将其合并到包装小部件中?

转载 作者:IT王子 更新时间:2023-10-29 07:03:06 27 4
gpt4 key购买 nike

我试图将 Row 中的项目(容器)限制为 8,如果有更多项目,则应将它们包装起来。我有一个容器列表,但我不能将 listView 用作 Wrap 的子项,因为 listView 会滚动。有什么办法可以修复此布局?

  • 我尝试过使用 for 循环,但是当它第一次点击返回时,它就跳出了循环。

  • 我已经尝试使用 gridView 而不是 wrap,但它没有给我结果,因为 gridView 是可滚动的。

             Expanded(
flex: 4,
child: Wrap(
direction: Axis.horizontal,
spacing: 0.5,
runSpacing: 0.5,
crossAxisAlignment: WrapCrossAlignment.center,
children: <Widget>[
// I want something that works like following line
//Container(child: kids1)

//currently I can get results with following code

kids1[1],
kids1[2],
kids1[3], kids1[4], kids1[5], kids1[6],
kids1[7], kids1[8], kids1[9], kids1[10],
kids1[11]

],
),
),

kids 是容器列表

最佳答案

为什么不在构建函数返回之前创建子列表:

@override
Widget build(BuildContext context) {

List<Widget> children = List.generate(myContainerList.length, (e) => myContainerList[e]);

return Wrap(
children: children,
);
}

关于flutter - 如何迭代列表以将其合并到包装小部件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56449105/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com