我试图在屏幕中央创建一个标签栏,在try 的时候我在一个专栏中给了TabBarView,我被困在了这个错误中.请解决这个问题.

I/flutter ( 3983): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter ( 3983): The following assertion was thrown during performResize():
I/flutter ( 3983): Horizontal viewport was given unbounded height.
I/flutter ( 3983): Viewports expand in the cross axis to fill their container and constrain their children to match
I/flutter ( 3983): their extent in the cross axis. In this case, a horizontal viewport was given an unlimited amount of
I/flutter ( 3983): vertical space in which to expand.

源代码是

class profilePage extends StatefulWidget {
  @override
  profilePageState createState() => profilePageState();
}

class profilePageState extends State<profilePage>
    with SingleTickerProviderStateMixin {
  TabController _tabController;
  @override
  void initState() {
    _tabController = new TabController(length: 2, vsync: this);
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: [
          Container(
            child: Column(crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                SizedBox(
                  height: 40,
                ),DefaultTabController(
                  length: 2,
                  child: Column(children: [TabBar(
                      unselectedLabelColor: Colors.black,
                      labelColor: Colors.red,
                      tabs: <Widget>[
                        Tab(
                          icon: Icon(Icons.people),
                        ),
                        Tab(
                          icon: Icon(Icons.person),
                        )
                      ],controller: _tabController,
                      indicatorSize: TabBarIndicatorSize.tab,
                    ),TabBarView(
                      children: <Widget>[Text('people'), Text('Person')],
                      controller: _tabController,
                    ),
                  ]),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

您可以看到上面的图像模型,这是我想要实现的.我试过很多方法,但我还是呆在这里.

如何纠正此错误,以及如何在屏幕中心创建选项卡栏?

推荐答案

我添加了您想要获取的内容的演示(我关注了您发布的图片):

NOTE:我不得不对您的小部件树的排列方式做一些更改.

class profilePage extends StatefulWidget {
  @override
  profilePageState createState() => profilePageState();
}

class profilePageState extends State<profilePage> {
 
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 2,
      child: Scaffold(
        appBar: AppBar(
          title: Text(
            'My Profile',
          ),
          centerTitle: true,
          backgroundColor: Colors.grey[700].withOpacity(0.4),
          elevation: 0,
          // give the app bar rounded corners
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.only(
              bottomLeft: Radius.circular(20.0),
              bottomRight: Radius.circular(20.0),
            ),
          ),
          leading: Icon(
            Icons.menu,
          ),
        ),
        body: Column(
          children: <Widget>[
            // construct the profile details widget here
            SizedBox(
              height: 180,
              child: Center(
                child: Text(
                  'Profile Details Goes here',
                ),
              ),
            ),

            // the tab bar with two items
            SizedBox(
              height: 50,
              child: AppBar(
                bottom: TabBar(
                  tabs: [
                    Tab(
                      icon: Icon(Icons.directions_bike),
                    ),
                    Tab(
                      icon: Icon(
                        Icons.directions_car,
                      ),
                    ),
                  ],
                ),
              ),
            ),

            // create widgets for each tab bar here
            Expanded(
              child: TabBarView(
                children: [
                  // first tab bar view widget
                  Container(
                     color: Colors.red,
                    child: Center(
                      child: Text(
                        'Bike',
                      ),
                    ),
                  ),

                  // second tab bar viiew widget
                  Container(
                     color: Colors.pink,
                    child: Center(
                      child: Text(
                        'Car',
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

输出:

enter image description here

Dart相关问答推荐

如何扩展 Dart 套接字 BroadcastStream 缓冲区大小为 1024 字节?

NetworkImage 正在缓存旧图像

如何使用 dart 将 Uint8list 转换为 List

无法在 IOS 上使用 firebase 运行Flutter应用程序

FlutterError (setState() 在 dispose() 之后调用:(生命周期状态:defunct,not mounted)

Flutter/Dart:子类化冻结的数据类

在polymer和dart中将内容标签渲染为模板的一部分

在Dart streams中,listen和forEach有什么区别?

在Flatter中滚动列表视图时,网络图像一直消失?

如何垂直对齐行元素?

Flutter-如何更改文本字段边框 colored颜色 ?

从Dart中的另一个文件导入扩展名方法

如何从目录中获取文件列表并将其传递给ListView?

如何正确管理 Flutter 应用中的全局 textScaleFactor?

为什么 Dart 有编译时常量?

如何从列表中的元素创建逗号分隔的字符串

如何使异步 Dart 调用同步?

包裹构造函数参数的花括号代表什么?

从函数返回多个值

如何在 Dart 中将 double 转换为 int?