【发布时间】:2019-04-01 18:46:22
【问题描述】:
我有一个名为 HorizontalList 的自定义小部件,它接收一个列表并水平显示它。
我制作了一个垂直列表,其行由这些水平列表组成。
我的问题是,即使传递给每个水平列表的列表与其他列表不同,渲染的所有行都与我使用相同列表构建小部件一样。
我检查了widget本身的构造函数,数据是正确的,但是渲染的时候出了点问题。
小部件构造函数:
HorizontalList(List<RestInfos> infos){
_rests = new List<RestInfos>();
_rests = infos;
print("horizontal list "+ _rests.length.toString());
if(_rests.length != 0)
print("horizontal list 1: " + _rests.elementAt(0).name);
}
小部件构建:
child: new ListView(
scrollDirection: Axis.horizontal,
children: new List <Widget>.generate(_rests.length, (int index) {
if(_rests.length == 0) {
//return null;
return new Card();
}
print("hotizontal " + _rests.length.toString());
print("indice "+ index.toString());
print("nome " + _rests.elementAt(index).name);
return new GestureDetector(
child:
new Card(...)
)
在主页中,我尝试像这样构建小部件:
child: new ListView(
children: <Widget>[
new HorizontalList(list1),
new HorizontalList(list2),
new HorizontalList(list3),
new HorizontalList(list4),
]
)
【问题讨论】:
-
给我们更多的信息,也许信息是一样的。添加屏幕截图或打印输出以检查数据是否不同
-
不,信息不一样,我在
new HorizontalList在文本小部件中打印列表的尺寸之前检查过,它们是正确的。在我的代码中,在构造函数中打印传递给小部件的每个列表的第一个元素,我得到了正确的元素,总是不同的。渲染的总是 list_4(最后一个通过的列表) -
在水平列表构建的列表视图中,添加 shrinkWrap: true
-
它没有解决任何问题。问题不在于列表的维度,而是我有 5 个列表被渲染为只有最后一个应该呈现的事实。