TabBarView内部的一个Positioned错误,水平视图窗口给定了无界限的高度 – Flutter

huangapple go评论67阅读模式
英文:

TabBarView inside a Positioned error Horizontal viewport was given unbounded height - Flutter

问题

I'm getting the error below when I try to run the code. I need the Containers inside the TabBarView to have their sizes set to (200 and 150), but I can't make it happen. My code only stops breaking when I use Positioned.fill or wrap the TabBarView in an Expanded, forcing the maximum height. However, I need the children of the TabBarView to have their minimum size, I've tried wrapping the TabBarView in a column and passing MainAxisSize.min, but I haven't had success either. Does anyone know what I can do to make the children's sizes have their minimum height? Note: I need to use two Positioned in this case.

FlutterError (水平视口给定无限高度。
视口在横轴上扩展以填充其容器,并限制其子元素以匹配其横轴上的范围。在这种情况下,水平视口被给定了无限量的垂直空间来扩展。)

TabBarView内部的一个Positioned错误,水平视图窗口给定了无界限的高度 – Flutter

英文:

I'm getting the error below when I try to run the code. I need the Containers inside the TabBarView to have their sizes set to (200 and 150), but I can't make it happen. My code only stops breaking when I use Positioned.fill or wrap the TabBarView in an Expanded, forcing the maximum height. However, I need the children of the TabBarView to have their minimum size, I've tried wrapping the TabBarView in a column and passing MainAxisSize.min, but I haven't had success either. Does anyone know what I can do to make the children's sizes have their minimum height? Note: I need to use two Positioned in this case.

> FlutterError (Horizontal viewport was given unbounded height.
Viewports expand in the cross axis to fill their container and constrain their children to match their extent in the cross axis. In this case, a horizontal viewport was given an unlimited amount of vertical space in which to expand.)

Scaffold(
      backgroundColor: Colors.grey,
      appBar: AppBar(),
      body: SafeArea(
        child: Stack(
          children: [
            Positioned( // <--  Not using Positioned.fill
              top: 43,
              left: 0,
              right: 0,
              child: TabBarView( // <-- And not using Expanded there, the children of TabBarView is not expanded, need min size
                controller: _tabController,
                children: [
                  Container(height: 200,
               decoration: BoxDecoration(borderRadius: 
               BorderRadius.circular(15.0),                           
            color:Theme.of(context).colorScheme.surface,
                              ),),
                  Container(height: 150,color: Colors.green),
                ],
              ),
            ),
            Positioned(
              top: 0,
              left: 0,
              right: 0,
              child: TabBar(
                controller: _tabController,
                tabs: const [
                  Tab(text: 'Tab 1'),
                  Tab(text: 'Tab 2'),
                ],
              ),
            ),
          ],
        ),
      ),
    );

TabBarView内部的一个Positioned错误,水平视图窗口给定了无界限的高度 – Flutter

答案1

得分: 2

希望这有所帮助。

我不确定为什么在这里需要Stack。如果这是必需的,请告诉我。下面的代码和内联注释将帮助您更好地理解如何使用TabBarView

提示1: 您需要使用ExpandedFlexible来包装您的TabBarView,以使其作为TabBarView具有无限高度。

提示2:Column包装您的TabBarView容器,并指定保持大小为最小。

return Scaffold(
  backgroundColor: Colors.grey,
  appBar: AppBar(),
  body: SafeArea(
    child: Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        TabBar(
          controller: _tabController,
          tabs: const [
            Tab(text: 'Tab 1'),
            Tab(text: 'Tab 2'),
          ],
        ),
        Flexible(
          // 使用Flexible而不是Expanded来根据子项高度缩小高度

          child: TabBarView(
            // <--- 并不在那里使用Expanded,TabBarView的子项没有展开,需要最小尺寸
            controller: _tabController,
            children: [
              Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Container(
                    decoration: const BoxDecoration(
                      color: Colors.black54,
                      borderRadius: BorderRadius.only(
                        topRight: Radius.circular(20),
                      ),
                    ),
                    height: 200,
                  ),
                ],
              ),
              Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Container(
                    decoration: const BoxDecoration(
                      color: Colors.black54,
                      borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(20),
                      ),
                    ),
                    height: 150,
                  ),
                ],
              ),
            ],
          ),
        ),
      ],
    ),
  ),
);
英文:

Hope this helps.

I am not sure why Stack is required here. If it's mandatory, let me know. The below code and inline comment will help you understand using TabBarView in a better way.

Tip 1: You need either Expanded or Flexible to wrap your TabBarView to make it work as TabBarView has unbounded height

Tip 2: Wrap your TabBarView containers with Column and mention to keep the size as min.

return Scaffold(
      backgroundColor: Colors.grey,
      appBar: AppBar(),
      body: SafeArea(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            TabBar(
              controller: _tabController,
              tabs: const [
                Tab(text: &#39;Tab 1&#39;),
                Tab(text: &#39;Tab 2&#39;),
              ],
            ),
            Flexible(
              //Use Flexible instead of Expanded to shrink the height based on the children heights

              child: TabBarView(
                // &lt;-- And not using Expanded there, the children of TabBarView is not expanded, need min size
                controller: _tabController,
                children: [
                  Column(
                    mainAxisSize: MainAxisSize
                        .min, //Wrap your view container with Column and mention to keep the size as min
                    children: [
                      Container(
                        decoration: const BoxDecoration(
                          color: Colors.black54,
                          borderRadius: BorderRadius.only(
                            topRight: Radius.circular(20),
                          ),
                        ),
                        height: 200,
                      ),
                    ],
                  ),
                  Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Container(
                        decoration: const BoxDecoration(
                          color: Colors.black54,
                          borderRadius: BorderRadius.only(
                            topLeft: Radius.circular(20),
                          ),
                        ),
                        height: 150,
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );

huangapple
  • 本文由 发表于 2023年6月8日 10:11:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定