Getting 'Horizontal viewport was given unbounded height.' with TabBarView in flutter

后端 未结 4 2046
一生所求
一生所求 2021-01-01 14:45

I\'m trying to make a profile page, where the users info is at the top. And then have a tab view below that for different views.

This is the code I\'m using

4条回答
  •  走了就别回头了
    2021-01-01 15:21

    The error description is clear, the TabBarView doesn't have a bounded height. the parent widget also doesn't have a bounded height. So, the Expanded widget will not solve this issue.

    EDIT: below solutions are for above question(with columns).In general cases, use a ListView with shrinkWrap: true.(Or any other widgets with shrinkWrap)

    There are some options:

    First Solution:

    Wrap the parent widget(Column) with a limited height widget like SizedBox or AspectRatio. Then use the Expanded widget like this:

    child: SizedBox(
              height: 300.0,
              child: Column(
                children: [
           .
           .
           .
                  Expanded(
                    child: TabBarView(
                      children: [
                        Container(
                          height: 200.0,
                          color: Colors.grey,
                        ),
                        Container(
                          height: 200.0,
                          color: Colors.green,
                        ),
                        Container(
                          height: 200.0,
                          color: Colors.purple,
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
    

    Second Solution:

    Use a bounded widget like SizedBox or AspectRatio on the TabBarView itself:

    SizedBox(
                height: 300.0,
                child: TabBarView(
                    children: [
                      Container(
                        height: 200.0,
                        color: Colors.grey,
                      ),
                      Container(
                        height: 200.0,
                        color: Colors.green,
                      ),
                      Container(
                        height: 200.0,
                        color: Colors.purple,
                      ),
                    ],
                  ),
                ),
    

    Note Your can also calcuate the height dynamicly if the height is not static.

提交回复
热议问题