【问题标题】:Append static items to a ListView.builder generated from the database data将静态项附加到从数据库数据生成的 ListView.builder
【发布时间】:2019-09-21 03:59:26
【问题描述】:

我从 firestore 数据库中获取了一些数据,并使用这些数据创建了一个 ListView 来显示我的数据。

我想在这个列表的底部附加 3 个与生成的不同的静态项目。

我构建列表的代码是:

child: StreamBuilder<QuerySnapshot>(
  stream: Firestore.instance
      .collection('users').document(userAdminId)
      .collection('events')
        .where('operatore', isEqualTo: _user)
      .snapshots(),
  builder: (BuildContext context,
      AsyncSnapshot<QuerySnapshot> snapshot) {
    if (snapshot.hasError)
      return Text('Error: ${snapshot.error}');
    switch (snapshot.data) {
      case null:
        return Container();
      default:
        return ListView.builder(
          itemCount: snapshot.data.documents.length,
          itemBuilder: (context, index) {
            final item = snapshot.data.documents[index];
            final itemID =
                snapshot.data.documents[index].documentID;
            final list =
                snapshot.data.documents;
            return Card(
                         ...

                        },
                      );
                  }
                },
              ),

现在它生成 2 张卡片,因为我从数据库中获得了 2 个事件

我想做的是在这个列表的底部再追加 3 张卡片,

越来越少的是这样的:

Card(
  child: Row(
    children: <Widget>[
      Icon(
        Icons.camera_alt
      ),
      Text('Camera')
    ],
  ),
)

Card(
  child: Row(
    children: <Widget>[
      Icon(
        Icons.folder
      ),
      Text('Media')
    ],
  ),
)

Card(
  child: Row(
    children: <Widget>[
      Icon(
        Icons.users
      ),
      Text('Friends')
    ],
  ),
)

不管它从数据库中生成多少项目,我都想在底部附加这3张卡片

示例:

如果我得到 3 个事件,我的列表应该是:

[event1]
[event2]
[event3]
[camera]
[media]
[friends]

如果我只收到一个事件:


[event1]
[camera]
[media]
[friends]

【问题讨论】:

标签: listview flutter static


【解决方案1】:

我通过SliverList 小部件解决了这个问题

此代码是一个示例:

Widget buildGridFilesToExport() {
  return new StreamBuilder(
    stream: Firestore.instance
        .collection('users')
        .document(dataUserGlobal.userAdminId)
        .collection('events')
        .document(dataUserGlobal.eventId)
        .snapshots(),
    builder: (context, snapshot) {
      print(snapshot);
      if (snapshot.hasError) return new Text('Error: ${snapshot.error}');
      switch (snapshot.connectionState) {
        case ConnectionState.waiting:
          return dataUserGlobal.showLoading(dataUserGlobal.purple);
        default:
          List videosList = snapshot.data['thumbnailsUrl'];
          return videosList != null ?

          CustomScrollView(
            slivers: [
              SliverGrid(
                gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
                  maxCrossAxisExtent: 200.0,
                  mainAxisSpacing: 10.0,
                  crossAxisSpacing: 10.0,
                  childAspectRatio: 1,
                ),
                delegate: SliverChildBuilderDelegate((context, index) {
                  return Container(
                      padding: EdgeInsets.all(5.0),
                      child: Column(
                        children: <Widget>[
                          Expanded(
                              flex: 7,
                              child: Stack(
                                children: <Widget>[
                                  Container(
                                    margin:
                                    EdgeInsets.only(bottom: 2.0),
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.all(
                                          Radius.circular(5.0)),
                                      image: DecorationImage(
                                        image: NetworkImage(snapshot
                                            .data['thumbnailsUrl']
                                        [index]),
                                        fit: BoxFit.cover,
                                      ),
                                    ),
                                  ),

                                ],
                              )),

                        ],
                      ));
                }, childCount: snapshot.data['thumbnailsUrl'].length),
              ),
              SliverList(
                delegate: SliverChildListDelegate([
                  closeEvent(),
                ]),
              )
            ],
          )
              :
          Center(
              child: Container(
                width: 250,
                child: Text(
                  'Ancora nessun video!\nCarica i video dalla sezione media, oppure vai nella sezione amici e seleziona i video da spostare qui!',
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontFamily: 'acumin-pro',
                    fontSize: 22,
                  ),
                ),
              )
          );
      }
    },
  );
}

【讨论】:

    猜你喜欢
    • 2013-02-16
    • 1970-01-01
    • 2022-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多