gpt4 book ai didi

flutter - 在 ListView 内具有可变高度的 TabBarView

转载 作者:IT老高 更新时间:2023-10-28 12:38:18 32 4
gpt4 key购买 nike

我有一个包含多种类型项目的 ListView,其中之一是 TabBar 和 TabBarView 的小部件。
问题是每个标签页的高度不同,我希望 ListView 根据动态包装标签小部件这是高度
Like this
但是 TabBarView 不接受无界高度,ListView 不能为其子级提供高度。
有没有办法做到这一点?或者我是否必须将 TabBar 与可以像 Column 一样包装其内容并牺牲在选项卡之间滑动的能力的东西一起使用?

最佳答案

你不需要 TabView 来显示标签内容。这种方法的缺点是你失去了动画和滑动,所以如果你真的需要的话,你需要自己做。

enter image description here

class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
final List<Widget> myTabs = [
Tab(text: 'one'),
Tab(text: 'two'),
Tab(text: 'three'),
];

TabController _tabController;

@override
void dispose() {
_tabController.dispose();
super.dispose();
}

@override
void initState() {
_tabController = TabController(length: 3, vsync: this);
_tabController.addListener(_handleTabSelection);
super.initState();
}

_handleTabSelection() {
if (_tabController.indexIsChanging) {
setState(() {});
}
}

_listItem() {
return Container(
decoration: BoxDecoration(
border: Border.all(
width: 1,
color: Colors.blueAccent,
),
),
height: 120,
child: Center(
child: Text('List Item', style: TextStyle(fontSize: 20.0)),
),
);
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
children: <Widget>[
_listItem(),
TabBar(
controller: _tabController,
labelColor: Colors.redAccent,
tabs: myTabs,
),
Center(
child: [
Text('first tab'),
Column(
children: [
Text('second tab'),
...List.generate(10, (index) => Text('line: $index'))
],
),
Column(
children: [
Text('third tab'),
...List.generate(20, (index) => Text('line: $index'))
],
),
][_tabController.index],
),
_listItem(),
_listItem(),
],
),
);
}
}

关于flutter - 在 ListView 内具有可变高度的 TabBarView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52547731/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com