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

后端 未结 4 2047
一生所求
一生所求 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:06

    I solved it by adding TabBar inside Container and TabBarView inside Expanded:

    DefaultTabController(
        length: 3,
        child: Column(
          children: <Widget>[
            Container(child: TabBar(..)),
            Expanded(child: TabBarView(..)),
          ],
        ),
      );
    
    0 讨论(0)
  • 2021-01-01 15:10

    based on @Yamin answer I used SizeBox Like below to get full page

        SizedBox.expand(
            child: TabBarView(),
          )
    

    or any other size :

    SizedBox(
            height: height:MediaQuery.of(context).size.height // or every other size ,
            child: TabBarView(),
          )
    
    0 讨论(0)
  • 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: <Widget>[
           .
           .
           .
                  Expanded(
                    child: TabBarView(
                      children: <Widget>[
                        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: <Widget>[
                      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.

    0 讨论(0)
  • 2021-01-01 15:26

    try to use IndexedStack instead of TabBarView

    i tried Expanded, shrinkWrap = true , ... but no one work's fine just try example.

    Example:

    class Product extends StatefulWidget {
      @override
      _ProductState createState() => _ProductState();
    }
    
    class _ProductState extends State<Product> with SingleTickerProviderStateMixin {
      TabController tabController;
      int selectedIndex = 0;
    
      @override
      void initState() {
        super.initState();
        tabController = TabController(length: 5, vsync: this, initialIndex: 0);
      }
    
      @override
      void dispose() {
        tabController.dispose();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return DefaultTabController(
          length: 3,
          initialIndex: 0,
          child: Scaffold(
            body: ListView(
              shrinkWrap: true,
              children: [
                TabBar(
             
                  tabs: <Widget>[
                    Tab(
                      text: 'one',
                    ),
                    Tab(
                      text: 'two',
                    ),
                    Tab(
                      text: 'three',
                    ),
                  ],
                  controller: tabController,
                  onTap: (index) {
                    setState(() {
                      selectedIndex = index;
                      tabController.animateTo(index);
                    });
                  },
                ),
                IndexedStack(
                  children: <Widget>[
                    Visibility(
                      child: Text('test1'),
                      maintainState: true,
                      visible: selectedIndex == 0,
                    ),
                    Visibility(
                      child: Text('test2'),
                      maintainState: true,
                      visible: selectedIndex == 1,
                    ),
                    Visibility(
                      child: Text('test3'),
                      maintainState: true,
                      visible: selectedIndex == 2,
                    ),
                  ],
                  index: selectedIndex,
                ),
              ],
            ),
          ),
        );
      }
    }
    

    special thank's to @arbalest

    0 讨论(0)
提交回复
热议问题