如何在Flutter PageView中设置儿童的固定高度?

发布于 2025-01-23 18:08:05 字数 1198 浏览 2 评论 0原文

默认情况下,pageview的孩子将采用pageview的100%高度。我想要的是为每个孩子设置固定高度(例如100px)。我知道您可以设置viewPortFraction,但这无济于事,因为ListView的高度是动态的(屏幕的2/3),这也使孩子的身高也动态。

到目前为止,我已经尝试过的...

PageView(
      scrollDirection: Axis.vertical,
      controller: PageController(
        initialPage: 0,
      ),
      children: <Widget>[
        SizedBox(
          height: 100,
          child: Container(
            color: Colors.red,
          ),
        ),
        SizedBox(
          height: 100,
          child: Container(
            color: Colors.green,
          ),
    ),
    SizedBox(
      height: 100,
      child: Container(
        color: Colors.blue,
      ),
    ),
  ],
)

我希望sizedbox能给每个孩子的高度100。

编辑:详细说明我想要的东西。 < < /em>

列表需要2/3的屏幕大小,其中列表中的每个项目都有固定的高度。可见的顶部项目是“选择”的(默认情况下为索引0)。
当用户滚动时,第一个项目被隐藏了,现在第二个项目是“选择”的(这就是为什么我需要捕捉)。

因此,列表需要具有获取当前索引的能力,能够抢购(这就是为什么我使用pageview的原因),最重要的是,最重要的项目是“选择”的,而不是中间项目。

By default the children of PageView will take 100% height of PageView. What i want is to set fixed height for each child (let's say 100px). I know that you can set viewportFraction but that doesn't help because the height of ListView is dynamic (2/3 of screen) which makes the children height dynamic as well.

What I've tried so far...

PageView(
      scrollDirection: Axis.vertical,
      controller: PageController(
        initialPage: 0,
      ),
      children: <Widget>[
        SizedBox(
          height: 100,
          child: Container(
            color: Colors.red,
          ),
        ),
        SizedBox(
          height: 100,
          child: Container(
            color: Colors.green,
          ),
    ),
    SizedBox(
      height: 100,
      child: Container(
        color: Colors.blue,
      ),
    ),
  ],
)

I was hoping that SizedBox will give each child height of 100.

EDIT: Detailed explanation of what I want.
enter image description here

The list takes 2/3 of screen size with every item in list having fixed height. The top item that is visible is "selected" (at index 0 by default).
When user scrolls up the first item gets hidden and now the second item is "selected" (that's why I need snapping).

So the list needs to have ability to get the current index, be able to snap (this is why I went with PageView) and most importantly that the top item is "selected", not the middle one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橘和柠 2025-01-30 18:08:05

用这样的“对齐”包裹大小的窗口小部件:

        Align(
          child: SizedBox(
            height: 100,
            child: Container(
              color: Colors.red,
            ),
          ),
        )

Wrap the Sizedbox widget with "Align" like this :

        Align(
          child: SizedBox(
            height: 100,
            child: Container(
              color: Colors.red,
            ),
          ),
        )
二手情话 2025-01-30 18:08:05
SizedBox(
  height: 100,
  child: PageView(
  children: [

  ]),
);
SizedBox(
  height: 100,
  child: PageView(
  children: [

  ]),
);
画离情绘悲伤 2025-01-30 18:08:05

交换容器和小型框怎么样?使容器成为高度100的小型盒子的母体。

How about swapping container and sizedbox? Make Container the parent of SizedBox with height 100.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文