【问题标题】:ListView take dimension of childListView 获取孩子的维度
【发布时间】:2020-08-28 21:50:34
【问题描述】:

像标题一样,我试图在 ListView 中放置一些元素,并具有一定的高度和宽度(动态获取),所以我不想硬编码 ListView 的尺寸。
是否有任何方法可以将ListView 的高度设置为其子项的高度?

class HomepageScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Theme.of(context).backgroundColor,
      body: SafeArea(
        child: Column(
          children: <Widget>[
            Padding(
              padding: EdgeInsets.all(22),
              child: Column(
                children: <Widget>[
                  // OTHER WIDGETS
                ],
              ),
            ),
            Container(
              height: 80,
              child: ListView.builder(
                itemCount: rectangles.length,
                scrollDirection: Axis.horizontal,
                itemBuilder: (BuildContext context, int index) {
                  CustomRectangle rectangle = rectangles[index];
                  return GestureDetector(
                    onTap: () => print('detailed'),
                    child: Container(
                      margin: EdgeInsets.symmetric(horizontal: 20),
                      child: RectangleWidget(
                        color: rectangle.color,
                        height: rectangle.height,
                        width: rectangle.width,
                        title: rectangle.title,
                        subtitle: rectangle.subtitle,
                      ),
                    ),
                  );
                },
              ),
            )
          ],
        ),
      ),
    );
  }
}

提前致谢:)

【问题讨论】:

    标签: listview flutter dart


    【解决方案1】:

    我找到的解决方案是创建一个返回双精度的方法:

    方法

      double getHeightOfRectangle(List<CustomRectangle> rectangles) {
        if (rectangles.isNotEmpty) {
          return rectangles.map((e) => e.height).reduce(max);
        }
        return 80.0;
      }
    

    使用方法

    @override
      Widget build(BuildContext context) {
        return Scaffold(
          backgroundColor: Theme.of(context).backgroundColor,
          body: SafeArea(
            child: Column(
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.all(22),
                  child: Column(
                    children: <Widget>[
                      // OTHER WIDGETS
                    ],
                  ),
                ),
                Container(
                  height: getHeightOfRectangle(rectangles),
                  child: ListView.builder(
                    shrinkWrap: true,
                    itemCount: rectangles.length,
                    scrollDirection: Axis.horizontal,
                    itemBuilder: (BuildContext context, int index) {
                      CustomRectangle rectangle = rectangles[index];
                      return GestureDetector(
                        onTap: () => print('detailed'),
                        child: Container(
                          margin: EdgeInsets.symmetric(horizontal: 20),
                          child: RectangleWidget(
                            color: rectangle.color,
                            height: rectangle.height,
                            width: rectangle.width,
                            title: rectangle.title,
                            subtitle: rectangle.subtitle,
                          ),
                        ),
                      );
                    },
                  ),
                )
              ],
            ),
          ),
        );
      }
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用shrinkWrap: true 实现您要查找的内容,您可以更改此行为,以便 ListView 只占用它需要的空间(当有更多项目时它仍然会滚动)。

      在你的情况下最终会是这样的:

      class HomepageScreen extends StatelessWidget {
        @override
        Widget build(BuildContext context) {
          return Scaffold(
            backgroundColor: Theme.of(context).backgroundColor,
            body: SafeArea(
              child: Column(
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.all(22),
                    child: Column(
                      children: <Widget>[
                        // OTHER WIDGETS
                      ],
                    ),
                  ),
                  ListView.builder(
                    //Apparently only works on a vertical scroll direction
                    shrinkWrap: true,
                    itemCount: rectangles.length,
                    scrollDirection: Axis.horizontal,
                    itemBuilder: (BuildContext context, int index) {
                      CustomRectangle rectangle = rectangles[index];
                      return GestureDetector(
                        onTap: () => print('detailed'),
                        child: Container(
                          margin: EdgeInsets.symmetric(horizontal: 20),
                          child: RectangleWidget(
                            color: rectangle.color,
                            height: rectangle.height,
                            width: rectangle.width,
                            title: rectangle.title,
                            subtitle: rectangle.subtitle,
                          ),
                        ),
                      );
                    },
                  ),
                ],
              ),
            ),
          );
        }
      }
      

      【讨论】:

      • 已经尝试过了,但在这种情况下,我得到了这个错误:'constraints.hasBoundedHeight': is not true. 似乎ListView 没有占用孩子的高度
      • @Shifenis 所以我尝试了你的代码,但确实没有用,它只有在滚动方向为vertical 时才有效,但由于你的是horizontal,我让它工作的唯一方法是使用包装 ListView 的 Expanded 或 Flexible 小部件,但不知道这是否适合您的需求。
      • 在这种情况下,ExpandedFlexible 不适合我,因为这样ListView 会占用它可以占用的所有高度,而不是占用其子级的高度。可能我会创建一些方法来迭代列表的每个元素并获取高度。这是我唯一的解决方案
      猜你喜欢
      • 2012-01-29
      • 2013-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多