skip to Main Content

I’m trying to make the pageview be auto height based on its child since the content in child is dynamic. So, the fixed height is not a solution for me.

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

Error Message
Error Message

This is what I’m going to achieve
enter image description here

2

Answers


  1. You can wrap the widget with AspectRatio.

    ListView(
      children: [
        AspectRatio(
          aspectRatio: 1 / .4, //width /height
          child: PageView(
            children: [
    
    Login or Signup to reply.
  2. Code Exemple:

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

    The LayoutBuilder widget is used to get the maximum height of its parent widget, which is the ListView in this case. The SizedBox is then given a height equal to the maximum height of the ListView, ensuring that the PageView can take up the maximum available height without causing an infinite height constraint.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search