如何在具有颤动的可滚动视图中使用具有可变高度内容的TabView?

时间:2017-01-30 19:04:31

标签: dart flutter

我有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的视口不受约束,需要具有有界高度约束的祖先窗口小部件。不幸的是,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: ...

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

2 个答案:

答案 0 :(得分:7)

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

最简单的解决方案是使用Tab控制器+容器而不是TabView窗口小部件。

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')),
        ],
      ),
    );
  }
}

答案 1 :(得分:1)

通过重构滚动&#34; sliver&#34;这将变得非常容易。代码目前作为https://github.com/flutter/flutter/projects/3的一部分登陆。

SliverList https://docs.flutter.io/flutter/widgets/SliverList-class.html旨在实现这种组合,并且应该在大约两周内准备好进行一般使用。