【问题标题】:How to Use Swipable Tabs in Flutter如何在 Flutter 中使用可滑动标签
【发布时间】:2020-11-28 03:12:23
【问题描述】:

其中一个 App Screen 需要滑动标签,所以我想顺便实现一下默认控制器,官方语法是:

home:DefaultTabController(
  length: 3,
  child: Scaffold(
    appBar: AppBar(
      bottom: TabBar(
        tabs: [
          Tab(icon: Icon(Icons.directions_car)),
          Tab(icon: Icon(Icons.directions_transit)),
          Tab(icon: Icon(Icons.directions_bike)),
        ],
      ),
    ),
  ),
);

现在,在我的项目中, main.dart 文件包含路由和 materialapp 小部件,看起来像

Widget build(BuildContext context) {
    return MaterialApp(
        title: '....',        
        theme: ThemeData(....),
        home: LoginScreen(),
        initialRoute: '/LoginScreen',
        routes: {
          '/HomeScreen': (context) => HomeScreen(),
          '/LoginScreen': (context) => LoginScreen(),
          '/MatchesScreen': (context) => MatchesScreen(),//this is the screen i want to implement tabs
          ....
          //other routes
        });
  }

在匹配屏幕(标签屏幕)中,它将返回脚手架

class _MatchesScreenState extends State<MatchesScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
          //it only holds app bar and other stuff           
    );
  }
}

这里我不能使用 home 属性,因为 home 是 MaterialApp() 的属性,我的问题是我如何在这种情况下使用选项卡,有没有办法替换 home 属性或覆盖它。帮我解决这个问题

【问题讨论】:

  • 共有三个TabBar,如果一个TabBarView是MatchesScreen,那么剩下的两个TabBarView是什么?

标签: flutter flutter-layout


【解决方案1】:

您可以使用body 属性代替home

return DefaultTabController(
    length: 3,
    child: Scaffold(
      appBar: AppBar(
        bottom: TabBar(
          tabs: [
            Tab(icon: Icon(Icons.directions_car)),
            Tab(icon: Icon(Icons.directions_transit)),
            Tab(icon: Icon(Icons.directions_bike)),
          ],
        ),
        title: Text('Flutter Tabs Example'),
      ),
      body: TabBarView(
        children: [
          MatchesScreen(),
          Text('second'),
          Text('third')
        ],
      ),
    )
);

但是有三个TabBar,如果一个TabBarView是MatchesScreen,那么剩下的两个TabBarView是什么?

【讨论】:

  • 它只是一个示例,但根据您的回答更新后它可以正常工作。
猜你喜欢
  • 1970-01-01
  • 2014-09-27
  • 2021-10-16
  • 1970-01-01
  • 1970-01-01
  • 2014-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多