在具有可变高度的控件树中使用DefaultTabController

问题描述 投票:0回答:1

在我的应用程序中,我有一个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,
          )
        ],
      ),
    );
  }
flutter flutter-layout flutter-pageview
1个回答
0
投票

删除ListView并用TabBarView包装Expanded,您应该会很好:

Expanded(
  child: TabBarView(
    children: [
      Text("one"),
      Text("two"),
    ],
  ),
)

请注意,现在TabBarViewPageView滑动手势之间存在冲突(由于[C​​0]覆盖了PageView下方的所有内容,因此只能从TabBar滑动TabBarView

© www.soinside.com 2019 - 2024. All rights reserved.