TABBARVIEW在可滚动页面中
我在让布局在 Flutter 中工作时遇到了很大的困难。
我尝试创建的布局:
- 一个
ListView
包含:- 一个
容器
。 - 一个
TabBar
。 - 一个
TabBarView
,其中每个TabBarView
包含一个Column
。
- 一个
- 我希望整个页面可以滚动。
下面是布局的示意图:

示例代码
这是一个最小的示例代码(删除了精确的小部件定义):
return DefaultTabController(
length: 2,
child: ListView(
children: [
// TOP CONTAINER //
Container(height: 30),
// TAB BAR //
const TabBar(tabs: [
Tab(child: Text("Tab 1")),
Tab(child: Text("Tab 2")),
]),
// TAB BAR VIEWS //
SizedBox(
height: MediaQuery.of(context).size.height,
child: TabBarView(
children: [
Container(height: 5000),
Container(height: 5000),
],
),
)
],
),
);
问题:
当窗口的高度变小时,我在底部收到溢出错误:
我所做的:
- 我首先尝试将内部
Column
转换为ListView
,这修复了溢出,但导致两个单独的可滚动区域(单个选项卡视图和整个选项卡视图)页),这不是我想要的 - 我想要一个可滚动区域。将此ListView
的physicals
属性设置为NeverScrollablePhysics()
并不能解决此问题,并会导致一些奇怪的行为。 - 我尝试使用
NestedScrollView
和Silvers
(来自 如何创建有界可滚动 TabBarView)。但这会导致在浏览选项卡时出现以下异常:提供的 ScrollController 当前附加到多个 ScrollPosition。
,并产生一些不可靠的滚动机制。 - 我尝试使用
CustomScrollView
但这不起作用。
未提供有效解决方案的类似问题:
- 在没有预定义大小的Column中颤动可滚动TabBarView
- 如何创建有界可滚动 TabBarView
- 如何使用 tabBar 实现 sliverAppBar
- 获取“水平视口被赋予无限高度”。在 flutter 中使用 TabBarView
我很困惑为什么它不起作用,因为我觉得这是一件非常简单的事情。本质上,它与查看个人资料时 Instragram 应用程序(以及其他应用程序)中使用的布局相同(请参阅:https://unblast.com/wp-content/uploads/2020/01/Instagram-UI-Profile-1.jpg)。
I am having real trouble getting a layout to work within Flutter.
The layout I am trying to create:
- A
ListView
that contains a:- A
Container
. - A
TabBar
. - A
TabBarView
, where eachTabBarView
contains aColumn
.
- A
- And I want the whole page to be scrollable.
Here is the schematic for the layout:
Example Code
Here is a minimum example code (with exact widget definitions removed):
return DefaultTabController(
length: 2,
child: ListView(
children: [
// TOP CONTAINER //
Container(height: 30),
// TAB BAR //
const TabBar(tabs: [
Tab(child: Text("Tab 1")),
Tab(child: Text("Tab 2")),
]),
// TAB BAR VIEWS //
SizedBox(
height: MediaQuery.of(context).size.height,
child: TabBarView(
children: [
Container(height: 5000),
Container(height: 5000),
],
),
)
],
),
);
The Problem:
When the height of the window gets smaller, I get an overflow error at the bottom:
What I have Done:
- I first tried converting the inner
Column
into aListView
, which fixed the overflow, but resulted in two separate scrollable areas (the individual tab views and the whole page), which is not what I want - I want a single scrollable area. Setting thephysics
property of thisListView
toNeverScrollablePhysics()
doesnt fix this and results in some weird behaviour. - I tried using a
NestedScrollView
withSilvers
(from How to create a bounded scrollable TabBarView). But this results in the following exception when navigating through the tabs:The provided ScrollController is currently attached to more than one ScrollPosition.
, and produces some dodgy scroll mechanics. - I tried using a
CustomScrollView
but that didnt work.
Similar Questions that Didnt provide a working solution:
- Flutter scrollable TabBarView in Column without predefined size
- How to create a bounded scrollable TabBarView
- how to implement a sliverAppBar with a tabBar
- Getting 'Horizontal viewport was given unbounded height.' with TabBarView in flutter
I am very confused as to why it is not working as I feel this is a very simple thing. Essentially, it is the same layout used in the Instragram app (among others) when viewing your personal profile (see: https://unblast.com/wp-content/uploads/2020/01/Instagram-UI-Profile-1.jpg).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从注释中,您可以将页面包装在
singlechildscrollview
中,禁用列表视图的滚动物理,因为父级已经可滚动。**选项2 **
您可以使用
customScrollView
或nestedScrollView
From the comments you can wrap your page in a
singlechildscrollview
, disable scroll physics for the listview as the parent is already scrollable.** Option2 **
you can use a
customScrollView
or anestedScrollView
我已经在
tabbarview
上划过了一个问题。不幸的是,它在滚动诸如滚动和无法正常工作的用例中被打破,内容溢出。
因此,我的解决方案通过回答如何滚动整个页面来回答OP问题。它用不同的机制代替
TABBARVIEW
。我的解决方案是跳过
tabbarview
并使用tabbar
和tabController
从widget < /代码>列表。
代码:
I have scratched my head around
TabBarView
many times with a question 'How to put dynamic-height content insideTabBarView
while it is inside a scrollable container?'Unfortunately, it is broken for such use cases as the scroll and does not work correctly and the content overflows.
Hence, my solution answers the OP question by answering how to achieve the whole page scrollable. It replaces
TabBarView
with different mechanism.My solution is to skip
TabBarView
and useTabBar
andTabController
to get aWidget
from theWidget
list.Code: