flutter - 在具有可变高度的小部件树中使用 DefaultTabController
问题描述
在我的应用程序中,我有一个 PageView,然后在那个 PageView 中有一些选项卡式内容(通过使用 DefaultTabController)。在这些选项卡中,我有一些可变高度的内容。无论我做什么,每当我尝试使用 DefaultTabController 时,Flutter 都会抛出这个错误:
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.
The relevant error-causing widget was:
TabBarView file:///C:/code/samples/tabbed/lib/main.dart:104:23
我试过了:
- 使用 shrinkWrap 将所有内容包装在 ListView 中:true
- 使用具有最小主轴尺寸的列
无论我做什么,TabBarView 总是试图渲染到无穷大。如何使用 DefaultTabController 显示可变高度选项卡?
这是我的代码:
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: PageView(
children: [
Container(
height: 200,
color: Colors.blueGrey,
child: DefaultTabController(
length: 2,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
TabBar(
tabs: [
Tab(
text: "Okay",
),
Tab(
text: "Go",
)
],
),
ListView(
scrollDirection: Axis.vertical,
shrinkWrap: true,
children: [
TabBarView(
children: [
Text("one"),
Text("two"),
],
)
],
),
],
),
),
),
Container(
color: Colors.deepOrange,
),
Container(
color: Colors.cyan,
)
],
),
);
}
解决方案
删除并用ListView
包裹,你应该很高兴:TabBarView
Expanded
Expanded(
child: TabBarView(
children: [
Text("one"),
Text("two"),
],
),
)
TabBarView
请注意,现在您在和滑动手势之间存在冲突PageView
(您只能PageView
从滑动 ,TabBar
因为后者下方的所有内容都被 覆盖TabBarView
)
推荐阅读
- react-native - 在 react-native-bootsplash 中调整初始屏幕图像的大小
- html - 如何连接旋转的 div 元素?
- c# - 如何在允许混淆的同时重用 .NET 项目
- r - 如何使用 sample() 将某个概率归因于每个事件
- batch-file - 您可以在 SET PATH=%PATH% 中使用带有通配符的路径吗
- typescript - 无法从 TypeScript 反射系统推断 GraphQL 类型。为“UpdateCoordinatesInput”类的“list_of_coordinate_ids”提供显式类型
- android - Android 暗模式:我的菜单文本在深灰色上是黑色的,为什么会这样?
- java - JUnit 集成测试中的更新问题
- python - 如何循环最后一行,用户将在其中输入他想从给定数量的铲子中制作多少铲子,但如果他输入 moar 程序循环
- amazon-web-services - 如何使用 AWS API Gateway 返回方法请求正文作为响应?