首页 > 解决方案 > 如何在 Flutter 中实现动态 GridView?

问题描述

我想使用SliverGridDelegateWithMaxCrossAxisExtent, this 而不是SliverGridDelegateWithFixedCrossAxisCount,但是当我这样做时,GridView.builder什么也不显示。

我的问题SliverGridDelegateWithFixedCrossAxisCount是它使用固定数量的空间,即使实际上没有任何东西可以占用它。

这是我现在的代码:

GridView.builder(
                shrinkWrap: true,
                itemCount: someDynamicValueRightHere, // from hive db                  
                gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 5,
                  crossAxisSpacing: 5,
                  mainAxisSpacing: 5,
                ),
                itemBuilder: (ctx, index) => Card(
                  shape: const RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(
                      Radius.circular(15),
                    ),
                  ),
                  child: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Column(
                      children: [
                        Align(
                          alignment: Alignment.topRight,
                          child: OptionsMenu(
                            index: index,
                          ),
                        ),
                        Text(
                          'foo',
                          textAlign: TextAlign.center,
                          style: TextStyle(
                            fontSize: 20,
                            color:
                                Theme.of(context).textTheme.bodyText1!.color,
                          ),
                        ),
                        const SizedBox(
                          height: 20,
                        ),
                        Text(
                          'foo',
                          textAlign: TextAlign.center,
                          style: TextStyle(
                            fontSize: 20,
                            color:
                                Theme.of(context).textTheme.bodyText1!.color,
                          ),
                        ),
                        const Spacer(),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          children: [
                            IconButton(
                              onPressed: () =>foo()
                                                                  ),
                            IconButton(
                              onPressed: () =>
                                  foo(),
                            )
                          ],
                        )
                      ],
                    ),
                  ),
                ),
              ),

我该如何使用SliverGridDelegateWithMaxCrossAxisExtent?这似乎是我最好的选择,但当我这样做时,屏幕上什么也没有显示。请帮忙 !

标签: flutterflutter-layout

解决方案


推荐阅读