【问题标题】:Grid height based on the screen size基于屏幕尺寸的网格高度
【发布时间】:2023-04-02 22:55:03
【问题描述】:

朋友们,我在根据我尝试过的屏幕设置视图的卡片高度时遇到了问题,方法是使用下面的代码动态设置屏幕文本,如果我不使用媒体查询,我尝试使用动态 MediaQuery 的次数不是很多,它会给我带来错误,例如屏幕下方的额外空间,我也无法使用大小合适的框,请在我使用时帮助朋友交错的网格视图底部有空间

@override
  Widget build(BuildContext context) {
    var size = MediaQuery.of(context).size;
    final double itemHeight = (size.height - kToolbarHeight - 24) / 2.3;
    final double itemWidth = size.width / 2;
    return livevideolist != null
        ? new GridView.builder(
            itemCount: livevideolist == null ? 0 : livevideolist.length,
            gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
              childAspectRatio: (itemWidth / itemHeight),
                crossAxisCount: 2),
            itemBuilder: (BuildContext context, int index) {
              return new GestureDetector(
                onTap: () {
                  String youtubeid = livevideolist[index]['url'];
                  playYoutubeVideo(youtubeid);
                },
                child: new Card(
                  elevation: 4.0,
                  margin: EdgeInsets.only(left: 10.0, right: 10.0, top: 5.0),
                  child: new Column(
                    children: <Widget>[
                      new Container(
                        height: 150.0,
                        width: double.infinity,
                        decoration: new BoxDecoration(
                          image: new DecorationImage(
                            image:
                                new NetworkImage(livevideolist[index]['image']),
                            fit: BoxFit.fill,
                          ),
                        ),
                      ),
                      Expanded(
                        child: new Container(
                          child: new Text(livevideolist[index]['title']),
                          margin: EdgeInsets.only(left: 10.0, top: 10.0),
                        ),
                      ),
                    ],
                  ),
                ),
              );
            },
          )
        : new Center(
            child: new CircularProgressIndicator(),
          );
  }

【问题讨论】:

    标签: flutter flutter-layout flutter-sliver


    【解决方案1】:

    你可以为此使用一个包,检查这个很棒的包:https://pub.dartlang.org/packages/flutter_staggered_grid_view

    你可以这样使用:

              Widget build(BuildContext context) {
                return livevideolist != null
                    ? new StaggeredGridView.countBuilder(
                        crossAxisCount: 2,
                        itemCount: livevideolist == null ? 0 : livevideolist.length,
                        staggeredTileBuilder: (int index) => new StaggeredTile.fit(1),
                        itemBuilder: (BuildContext context, int index) {
                          return new GestureDetector(
                            onTap: () {},
                            child: new Card(
                              elevation: 4.0,
                              margin: EdgeInsets.only(left: 10.0, right: 10.0, top: 5.0),
                              child: new Column(
                                mainAxisSize: MainAxisSize.min,
                                children: <Widget>[
                                  new Container(
                                    height: 150.0,
                                    width: double.infinity,
                                    decoration: new BoxDecoration(
                                      image: new DecorationImage(
                                        image: new NetworkImage(
                                            "https://upload.wikimedia.org/wikipedia/en/thumb/d/d9/ImageFight_arcadeflyer.png/256px-ImageFight_arcadeflyer.png"),
                                        fit: BoxFit.cover,
                                      ),
                                    ),
                                  ),
                                  new Padding(
                                    child: new Text(
                                        "Use a very long text here to see how it expands"),
                                    padding: EdgeInsets.only(left: 10.0, top: 10.0),
                                  ),
                                ],
                              ),
                            ),
                          );
                        },
                      )
                    : new Center(child: new CircularProgressIndicator());
              }
    

    只需替换为您正在使用的属性即可。

    根据您的需要将 maxLines 添加到您的文本中:

    Text("Your long text....",
            maxLines: 2 )
    

    为您的图像使用 fit: BoxFit.cover,而不是 fit: BoxFit.fill

    所以看起来你的文本有不同的大小,你可以强制父容器的高度:

    new Container(
       height: 80.0, //you can change this value
                                child: new Text(
                                    "Use a very long text here to see how it expands"),
                                padding: EdgeInsets.only(left: 10.0, top: 10.0),
                              ),
    

    【讨论】:

    • 但我需要所有标题的网格高度必须相同吗?
    • 哦,我以为你想要一个动态高度取决于文本
    • 检查代码,物品的高度将根据您的文字描述
    • 好的,我会按照你给出的那样尝试让你知道
    • 我已经按照你说的尝试过你的代码,但是第二个孩子的身高越来越大了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多