首页 > 解决方案 > itemCount 长度留出空间

问题描述

我有一个这样的程序,它没有给出任何错误,但是 item.lengt 占用了整个课程,程序留下空格而不是消除的空格(显示在代码中)。

我无法消除上面的列表,因为我已经完成了那里的索引,但我无法将它弄出来。

    return BlocBuilder(
    bloc: _lessonsBloc,
    builder: (BuildContext context, LessonsState state) {

    if (state is LessonsLoading) {
      return Center(
        child: CircularProgressIndicator(),
      );
    } else if (state is LessonsLoaded) {
      return ListView.builder(


     //here need to change
        itemCount: state.lessons.length,
        itemBuilder: (context, index) {
       //Here I eliminate items from my list its because my index here :/
          final displayedLessons = state.lessons[index];
            if(tab.text == displayedLessons.day){ 
              return ListTile(
            title: Text(displayedLessons.name),
            subtitle:
                Text(displayedLessons.subname),
            trailing: _buildUpdateDeleteButtons(displayedLessons), 
          );
            } else{print('nope');}
              return ListTile();           
        },
      );
    }
    return CircularProgressIndicator();
  },

);
}

如果我能弄清楚这一点,我会很高兴,但我认为我应该使用另一个小部件。我只需要一个想法。

https://pasteboard.co/Ivr3FjK.jpg
https://pasteboard.co/Ivr4gRU.jpg
https://pasteboard.co/Ivr4piY.jpg

标签: flutterdart

解决方案


一般情况下的解决方案:

每当您想为给定条件(例如)从构建器函数中跳过一个小部件时,只要该给定条件不满足ListView.builder,就有条件地返回一个没有维度的空。Container()

示例代码(用于 ListView.builder):

ListView.builder
  (
    itemCount: 10,
    itemBuilder: (BuildContext context, int i) {
       i=i+1;
       if(i%2==0) return Text(i.toString());
       else return Container();
    }
  )

推荐阅读