【问题标题】:add an item to ListView.builder flutter firebase向 ListView.builder 添加一个项目颤振火力基地
【发布时间】:2018-07-08 20:29:15
【问题描述】:

我正在尝试在列表的开头添加一个标题项,所以这是我使用 firebase 构建它的方法

new Expanded(
            child: new StreamBuilder(
                stream: Firestore.instance
                    .collection("users")
                    .document("dana")
                    .collection("Channels")
                    .snapshots(),
                builder: (context, snapshot) {
                  return new ListView.builder(
                    scrollDirection: Axis.vertical,
                    itemCount: snapshot.data.documents.length,
                    itemBuilder: (context, index) => _buildList(
                        context, snapshot.data.documents[index]),
                  );
                }),
          )

“_buildList”只是小部件

Widget _buildListItem(BuildContext context, DocumentSnapshot document) 

所以我基本上迷路了,我不知道如何添加另一个小部件作为标题 有什么建议吗?

【问题讨论】:

    标签: firebase google-cloud-firestore flutter


    【解决方案1】:

    当列表位于索引 0 上时,您可以返回“标题”小部件(您希望在列表视图上方但仍在其中的小部件):

    return new ListView.builder( 
        scrollDirection: Axis.vertical, 
        itemCount: snapshot.data.documents.length, 
        itemBuilder: (context, index) { 
            if (index == 0) {
                return someWidget, // return the widget you want as "header" here
            } else {
               return _buildList( context, snapshot.data.documents[index-1]), 
            }
        }
    );
    

    【讨论】:

    • 它起作用了,但[index+1] 没有,只​​是[index]有点问题
    猜你喜欢
    • 2021-06-05
    • 2021-03-10
    • 2021-07-03
    • 2021-03-19
    • 2021-02-03
    • 1970-01-01
    • 2020-06-26
    • 2020-07-28
    • 2020-03-25
    相关资源
    最近更新 更多