gpt4 book ai didi

dart - 如何在带有 Flutter 的可 ScrollView 中拥有具有可变高度内容的 TabView?

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

我有一个 TabBar & TabBarView嵌套在 Block 中.这是因为 TabBarView 内部的内容是动态的,直到运行时才知道内容的高度。

我无法将 Block 嵌套在 TabBarView 内,因为 TabBar 前面的内容应与 TabBarView 内容一起滚动。

图表:

Layout Diagram

代码:

new DefaultTabController(
length: 3,
child: new Block(
children: [
new Container(
height: 200.0,
decoration: new BoxDecoration(
backgroundColor: Colors.blue[500],
),
),
new TabBar(
tabs: <Widget>[
new Tab(text: '1'),
new Tab(text: '2'),
new Tab(text: '3'),
],
),
new TabBarView(
children: [
new Text('one'),
new Text('two'),
new Text('three'),
],
),
],
)
);

但是,TabBarView 的 Viewport 不受约束,需要一个具有有限高度约束的祖先小部件。不幸的是,由于它的可滚动特性,Block 没有给出有界的高度。

我在运行布局时收到此错误:

══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY    ╞═════════════════════════════════════════════════════════
I/flutter (28715): The following assertion was thrown during performLayout():
I/flutter (28715): RenderList object was given an infinite size during layout.
I/flutter (28715): This probably means that it is a render object that tries to be as big as possible, but it was put
I/flutter (28715): inside another render object that allows its children to pick their own size.
I/flutter (28715): The nearest ancestor providing an unbounded height constraint is: ...

关于如何最好地解决这个问题的任何想法?

最佳答案

我不知道什么是 block (链接不再起作用)。但正如我从问题中看到的,您需要将 TableView 放在一些可滚动的组件中。

最简单的解决方案是使用标签 Controller +容器而不是TabView Widget。

enter image description here

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}

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;
int _tabIndex = 0;

@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(() {
_tabIndex = _tabController.index;
});
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
children: <Widget>[
Container(
height: 120,
child: Center(
child: Text('something on top'),
),
),

TabBar(
controller: _tabController,
labelColor: Colors.redAccent,
isScrollable: true,
tabs: myTabs,
),
Center(
child: [
Text('second tab'),
Column(
children: List.generate(20, (index) => Text('line: $index')).toList(),
),
Text('third tab')
][_tabIndex],
),
Container(child: Text('another component')),
],
),
);
}
}

关于dart - 如何在带有 Flutter 的可 ScrollView 中拥有具有可变高度内容的 TabView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41943551/

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