flutter - 如何使 Bottom Sheet 格的高度跟随 JSON 的项目数量

BottomSheet 在 Flutter 中使高度跟随项目数(来自 JSON 的数据)是什么?因为我让高度不跟随数据量,而是跟随每部手机屏幕的 1/2。因此,如果手机很长,则底部有空白空间。如果手机短,数据会被下面的屏幕截断。

这是创建 Bottom Sheet 的代码:

void _showModalBottomSheet(AsyncSnapshot<CatlistResult> snapshot) {
    showModalBottomSheet(
        context: context,
        builder: (BuildContext bc) {
          return Card(
            elevation: 3.0,
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.only(topLeft: Radius.circular(15), topRight: Radius.circular(15))),
            child: Container(
              padding: EdgeInsets.only(left: 16.0, top: 10.0, right: 16.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisSize: MainAxisSize.max,
                children: <Widget>[
                  Container(
                    margin: EdgeInsets.only(bottom: 10.0),
                    child: Image.asset('assets/images/main/more_2lines_20dp.png', scale: 3.5),
                  ),
                  Expanded(
                    child: GridView.builder(
                      physics: NeverScrollableScrollPhysics(), //kill scrollable
                      shrinkWrap: true,
                      itemCount: snapshot == null ? 0 : snapshot.data.catlist.length,
                      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 4),
                      itemBuilder: (BuildContext context, int index) {
                        var numItems = snapshot.data.catlist[index];
                        if (numItems.f == 1) {
                          if (numItems.b == 0) {
                            return GestureDetector(
                              child: Card(
                                elevation: 0.0,
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: <Widget>[
                                    Image.asset('assets/images/main/cat_${numItems.a}.png', fit: BoxFit.cover, width: 50.0, height: 50.0),
                                    Container(
                                      margin: EdgeInsets.only(top: 10.0),
                                      child: Text(
                                        numItems.c,
                                        maxLines: 2,
                                        textAlign: TextAlign.center,
                                        style: TextStyle(fontSize: 10),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            );
                          }
                        }
                      },
                    ),
                  ),
                ],
              ),
            ),
          );
        }
    );
  }

这是我的代码的结果:

  1. 从 iOS 模拟器运行

  1. 从 Android 模拟器运行

最佳答案

问题:

  1. 在您正在使用的父 Column

    mainAxisSize: MainAxisSize.max
    
  2. 您还在此 Column 中使用 Expanded,如果 GridView 中的项目较少,则会创建空白空间。移除这个 Expanded 小部件。


解决方案:

Column(
  crossAxisAlignment: CrossAxisAlignment.center,
  mainAxisSize: MainAxisSize.min, // 1st use min not max
  children: <Widget>[
    Container(...),
    GridView.builder(...), // 2nd remove Expanded
  ],
)

https://stackoverflow.com/questions/56949553/

相关文章:

intellij-idea - 如何启用 Flutter/Dart 语言实验?

authentication - 如何在 Flutter 应用中集成 Azure AD SSO

dart - 如何在 Flutter/Dart 中将参数从命令行传递到 main?

dart - 使用键和值从 map 创建小部件列表

charts - 如何在 flutter 中制作动态图表?

architecture - flutter BLoC : Is using nested Stre

flutter - 通过 Firebase 使用 Google 登录时, 'ApiExpceptio

dart - ScrollController 如何检测滚动开始、停止和滚动?

ios - 将今天的扩展添加到 flutter 应用程序的问题

flutter - 在 Flutter 中监听设备方向变化