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

Dvd*_*ibi 10 dart flutter

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

我无法将Block嵌套在TabBarView中,因为TabBar之前的内容应该与TabBarView内容一起滚动.

图:

布局图

码:

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'),
        ],
      ),
    ],
  )
);
Run Code Online (Sandbox Code Playgroud)

但是,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: ...
Run Code Online (Sandbox Code Playgroud)

关于如何最好地接近这个的任何想法?

Khe*_*rel 5

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

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

在此处输入图片说明

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')),
        ],
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)


Eri*_*del 1

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

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