Home > Back-end >  How to set fixed height of children in Flutter PageView?
How to set fixed height of children in Flutter PageView?

Time:04-24

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.

CodePudding user response:

Wrap the Sizedbox widget with "Align" like this :

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

CodePudding user response:

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

  • Related