如何删除GridView.builder中卡之间的所有空间

发布于 2025-02-14 02:14:20 字数 2478 浏览 0 评论 0原文

我需要有关我的gridview.builder,我的gridview 变得像这样这是因为我想过滤以显示具有相同ID的项目。我已经尝试放入填充:eDgetInsets.zerosizedbox.shrink,但它仍然不起作用。如何删除空白卡并仅显示可见的卡?如果我删除返回卡()

Stack(
        children: [
          StreamBuilder(
              stream: FirebaseFirestore.instance.collection('products').snapshots(),
              builder: (context, AsyncSnapshot<QuerySnapshot> snapshot){
                if(snapshot.hasError){
                  return Text("Error: ${snapshot.error}");
                }
                if(snapshot.hasData){
                  return Column(
                    children: [
                      Flexible(
                          child: GridView.builder(
                              padding: EdgeInsets.zero,
                              itemCount: snapshot.data!.docs.length,
                              gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                                crossAxisCount: 2,
                                mainAxisSpacing: 15,
                                crossAxisSpacing: 15,
                                childAspectRatio: 2/3,
                              ),
                              itemBuilder: (context, index){
                                final DocumentSnapshot documentSnapshot =
                                snapshot.data!.docs[index];
                                DateTime dt = (documentSnapshot['endDateTime'] as Timestamp).toDate();
                                if(documentSnapshot['userid'] == widget.sellerId){
                                  print(documentSnapshot['userid']);
                                  return  buildImageCard(
                                      documentSnapshot['imageURL'][0], documentSnapshot['nameP'],
                                      documentSnapshot['startPrice'], dt, documentSnapshot['id'],
                                      documentSnapshot['categoryP']);
                                }
                                return Card();
                              }
    
                          )
                      ),
                    ],
                  );
                }
                return const Center(
                  child: CircularProgressIndicator(),
                );
              }
    
    
          ),
        ],
      );

I need help regarding my gridview.builder, my gridview become like this, this happened because I want to filter to show only item that has the same id. I already try to put padding: EdgetInsets.zeroand SizedBox.shrink but it still does not work. How can I remove the blank card and only show the visible card? if I remove return Card()

Stack(
        children: [
          StreamBuilder(
              stream: FirebaseFirestore.instance.collection('products').snapshots(),
              builder: (context, AsyncSnapshot<QuerySnapshot> snapshot){
                if(snapshot.hasError){
                  return Text("Error: ${snapshot.error}");
                }
                if(snapshot.hasData){
                  return Column(
                    children: [
                      Flexible(
                          child: GridView.builder(
                              padding: EdgeInsets.zero,
                              itemCount: snapshot.data!.docs.length,
                              gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                                crossAxisCount: 2,
                                mainAxisSpacing: 15,
                                crossAxisSpacing: 15,
                                childAspectRatio: 2/3,
                              ),
                              itemBuilder: (context, index){
                                final DocumentSnapshot documentSnapshot =
                                snapshot.data!.docs[index];
                                DateTime dt = (documentSnapshot['endDateTime'] as Timestamp).toDate();
                                if(documentSnapshot['userid'] == widget.sellerId){
                                  print(documentSnapshot['userid']);
                                  return  buildImageCard(
                                      documentSnapshot['imageURL'][0], documentSnapshot['nameP'],
                                      documentSnapshot['startPrice'], dt, documentSnapshot['id'],
                                      documentSnapshot['categoryP']);
                                }
                                return Card();
                              }
    
                          )
                      ),
                    ],
                  );
                }
                return const Center(
                  child: CircularProgressIndicator(),
                );
              }
    
    
          ),
        ],
      );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦初启 2025-02-21 02:14:20

解决方案将是过滤小部件上方的文档列表,并使用ItemCount和Builder的过滤列表。

问题的原因是,每个小部件都采用您指定的长宽比(2/3) +间距,而不管子小部件的大小如何。

所以应该有点像这样

if(snapshot.hasData){
final listToUse = snapshot.data?.docs.where((documentSnapshot) => documentSnapshot['userid'] == widget.sellerId).toList() ?? [];
return Column(...

The solution would be to filter the list of docs above the widget, and use the filtered list for itemCount and the builder.

The cause of the issue is that each widget takes the aspect ratio that your specified (2/3) + the spacing regardless of the size of the child widget.

So it should be a little something like this

if(snapshot.hasData){
final listToUse = snapshot.data?.docs.where((documentSnapshot) => documentSnapshot['userid'] == widget.sellerId).toList() ?? [];
return Column(...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文