Listview中的Flutter页面视图自动高度问题

kwvwclae  于 2023-02-25  发布在  Flutter
关注(0)|答案(2)|浏览(251)

我试图使网页浏览量自动高度的基础上,其子女,因为内容的子女是动态的。所以,固定的高度不是一个解决方案,我。

ListView(
  children: [
    Container(
      //height: 300, //i dont want the fixed height
      decoration: BoxDecoration(color: Colors.amber),
      child: PageView(
        children: [
          Text(
            "1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1"), //this would be a dynamic content //this would be a dynamic content
            Text(
            "1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1") //this would be a dynamic content //this would be a dynamic content
        ],
      ),
    )
  ],
)

错误消息

这就是我要实现的

drnojrws

drnojrws1#

您可以使用AspectRatio Package 小部件。

ListView(
  children: [
    AspectRatio(
      aspectRatio: 1 / .4, //width /height
      child: PageView(
        children: [
gmxoilav

gmxoilav2#

代码示例:

LayoutBuilder(
  builder: (context, constraints) {
    return ListView(
      children: [
        Container(
          decoration: BoxDecoration(color: Colors.amber),
          child: SizedBox(
            height: constraints.maxHeight,
            child: PageView(
              children: [
                Text(
                  "1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1",
                ), // This would be dynamic content
                Text(
                  "1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1",
                ), // This would be dynamic content
              ],
            ),
          ),
        )
      ],
    );
  },
);

LayoutBuilder小部件用于获取其父小部件(在本例中为ListView)的最大高度,然后为SizedBox指定一个与ListView的最大高度相等的高度,以确保PageView可以占用最大可用高度,而不会导致无限的高度约束。

相关问题