Flutter出现错误:控制器的length属性不匹配

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

当我尝试使用DefaultTabController()小部件时,我在Flutter中遇到了这个问题,但是它似乎无法按照我指定的布局工作。谁能帮助我。

这是我的代码:

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
        length: 3,
        initialIndex: 0,
        child: Scaffold(
          appBar: AppBar(
            leading: IconButton(icon: Icon(Icons.menu), onPressed: () {}),
            title: Text('Home'),
            bottom: TabBar(tabs: [
              Tab(child: Text("Videos")),
              Tab(child: Text("Live Videos")),
              Tab(child: Text("Gallery")),
            ]),
          ),
          body: TabBarView(
            children: [
              new Card(
                color: Colors.blue,
              ),
            ],
          ),
        ));
  }
}

我在这里做错了什么?

flutter flutter-layout
1个回答
0
投票

您的body元素仅包含一个元素。根据您的DefaultTabController的length属性,该数组中应该有3个元素。

body: TabBarView(
    children: [
        new Card(
            color: Colors.blue,
        ),
        new Card(
            color: Colors.red,
        ),
        new Card(
            color: Colors.orange,
        ),
    ],
),
© www.soinside.com 2019 - 2024. All rights reserved.