如何在Flutter Firebase Stream Builder中跳过构建项生成器网格视图.builder

9
我想删除 Firebase 上的某些内容,当 Streambuilder(gridview.builder)到达空索引时,它会忽略它并不在该位置添加任何小部件,并将其保留给其他部件。
这是它的样子: App Screenshot 我想让左右两侧的空白消失,并让其他小部件填补它们的位置。
Firebase 数据库图像: Database Image
 classList(userUid) {
    print("user uid: $userUid");
    return StreamBuilder(
        stream: FirebaseDatabase.instance
            .reference()
            .child("user")
            .child(userUid)
            .child("classData")
            .onValue,
        builder: (BuildContext context, AsyncSnapshot<Event> snapshot) {
          if (snapshot.hasData) { //            Map<dynamic, dynamic> map = snapshot.data.snapshot.value; //            snapshot.data.snapshot.value.forEach((dynamic, v) => print(v["className"]));
            if (snapshot.data.snapshot.value != null) {
              return GridView.builder(
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 3),
                itemCount: snapshot.data.snapshot.value.toList().length,
                padding: EdgeInsets.all(2.0),
                scrollDirection: Axis.vertical,
                itemBuilder: (BuildContext context, int index) {
                  RandomColor _randomColor = RandomColor();

                  Color _color = _randomColor.randomColor(
                      colorBrightness: ColorBrightness.veryLight);

                  Color _color2 = _randomColor.randomColor(
                      colorBrightness: ColorBrightness.veryDark);
                  if (snapshot.data.snapshot.value.toList()[index] != null) {
                    return Container(
                      child: RaisedButton(
                        splashColor: _color2,
                        color: _color,
                        shape: SuperellipseShape(
                            borderRadius: BorderRadius.circular(90)),
                        onPressed: () {
                          Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => ClassPage(
                                        className: snapshot.data.snapshot.value
                                            .toList()[index]["className"]
                                            .toString()
                                            .toUpperCase(),
                                        classSection: snapshot
                                            .data.snapshot.value
                                            .toList()[index]["classSection"]
                                            .toString()
                                            .toUpperCase(),
                                        classIndex: snapshot.data.snapshot.value
                                            .toList()[index],
                                      )));
                        },
                        child: Text(
                          "${snapshot.data.snapshot.value.toList()[index]["className"].toString().toUpperCase()} \n ${snapshot.data.snapshot.value.toList()[index]["classSection"].toString().toUpperCase()}",
                          style: TextStyle(fontSize: 20, shadows: [
                            Shadow(
                                // bottomLeft
                                offset: Offset(-1.5, -1.5),
                                color: Colors.black),
                            Shadow(
                                // bottomRight
                                offset: Offset(1.5, -1.5),
                                color: Colors.black),
                            Shadow(
                                // topRight
                                offset: Offset(1.5, 1.5),
                                color: Colors.black),
                            Shadow(
                                // topLeft
                                offset: Offset(-1.5, 1.5),
                                color: Colors.black),
                          ]),
                        ),
                      ),
                      padding: EdgeInsets.all(4.0),
                    );
                  } else {
                    return Visibility(
                      child: Text("Gone"),
                      visible: false,
                    );
                  }
                },
              );
            }
            return Center(
                child: Text(
              "No Class Registered",
              style: TextStyle(fontSize: 42),
            ));
          } else {
            return CircularProgressIndicator();
          }
        });   }

嗨@Zyzto,你找到解决方案了吗?我已经尝试了几个小时了... - FilipeOS
我放弃了,并制作了“删除全部”按钮。 - Zyzto
我也是,我刚刚添加了自定义容器... - FilipeOS
1个回答

6

我解决网格中存在空白空间的方法是在使用小部件之前,先过滤小部件列表:

List visibleWidgets = allWidgets.where((widget) => widget.show == true).toList();


谢谢,这比我看到的其他解决方案更好(例如返回一个“容器”)。 - Sam Doggett

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接