android-studio - Flutter中向TabView添加选项卡标签

标签 android-studio dart flutter

我正在尝试扩展作为答案提出的 TabView here

具体来说,我想要有类别标签,而不是灰点,并将与该类别相对应的小部件作为选项卡内容加载。

我尝试遵循文档,但如果我更改 AppBar 中的某些内容,它只会给我一个重新渲染错误。

最佳答案

您可以通过DefaultTabController小部件实现您想要的。你可以看例子here仅包含图标。这是带有标题的修改版本。 TabBar 部分用于选项卡及其规范。 TabBarView 用于内部 View 。

class TabBarDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: DefaultTabController(
        length: 3,
        child: Scaffold(
          appBar: AppBar(
            bottom: TabBar(
              tabs: [
                Tab(icon: Icon(Icons.directions_car), text: "Car",),
                Tab(icon: Icon(Icons.directions_transit), text: "Transit"),
                Tab(icon: Icon(Icons.directions_bike), text: "Bike",),
              ],
            ),
            title: Text('Tabs Demo'),
          ),
          body: TabBarView(
            children: [
              Icon(Icons.directions_car),
              Icon(Icons.directions_transit),
              Icon(Icons.directions_bike),
            ],
          ),
        ),
      ),
    );
  }
}

如果您使用上面的代码,您将获得一个带有解释性文本的 3 个选项卡式选项卡 View 。

编辑:

//Call the widget as follows
new TabBarDemo(createTabsDynamically());

//method to use for dynamic creation.
List<Tab> createTabsDynamically() {

    List<String> titles = ["Car", "Transit", "Bike"];
    List<IconData> iconData = [Icons.directions_car, Icons.directions_transit, Icons.directions_bike];
    List<Tab> tabs = new List();
    // Assuming titles and icons have the same length
    for (int i = 0; i< titles.length; i++) {
      tabs.add(Tab(icon: Icon(iconData[i]), text: titles[i],));
    }

    return tabs;
}

class TabBarDemo extends StatelessWidget {

  List<Tab> tabs;
  TabBarDemo(this.tabs);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: DefaultTabController(
        length: 3,
        child: Scaffold(
          appBar: AppBar(
            bottom: TabBar(
              tabs: tabs,
            ),
            title: Text('Tabs Demo'),
          ),
          body: TabBarView(
            children: [
              Icon(Icons.directions_car),
              Icon(Icons.directions_transit),
              Icon(Icons.directions_bike),
            ],
          ),
        ),
      ),
    );
  }
}

关于android-studio - Flutter中向TabView添加选项卡标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51124651/

相关文章:

android-studio - Android Studio 4.0 中的BottomNavigationView 和TabLayout 之间的Tab UI 有何不同?

Dart:快照与 AOT

android - 即使安装了应用程序,Firebase 动态链接也会重定向到 Playstore

flutter - Flutter 中 PreferredSize 小部件有什么用?

android - 获取依赖运行时列表

android - Android Studio 中的项目不再起作用

android - 如果使用的资源 ID 属于另一个 Activity 布局,如何显示警告/错误

flutter - 在 Flutter 中,是否可以将我的 MaterialApp 的 themeData 设置为使用大写作为所有文本值的默认值?

firebase - 将Firestore数据过滤为仅包含当前用户信息

flutter - 找不到名为 "devtools-server-address"的选项。尝试从 AndroidStudio 运行 flutter 应用程序时