如何在Flutter PageView中设置儿童的固定高度?
默认情况下,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。
列表需要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.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
用这样的“对齐”包裹大小的窗口小部件:
Wrap the Sizedbox widget with "Align" like this :
交换容器和小型框怎么样?使容器成为高度100的小型盒子的母体。
How about swapping container and sizedbox? Make Container the parent of SizedBox with height 100.