如何使底页高度遵循JSON的数量

时间:2019-07-09 09:33:35

标签: flutter dart bottom-sheet

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

这是用于创建底部工作表的代码:

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模拟器运行

Run from iOS Emulator

  1. 从Android模拟器运行

Run from Android Emulator

1 个答案:

答案 0 :(得分:0)

问题:

  1. 您在父母Column中使用

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


解决方案:

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