Warm tip: This article is reproduced from serverfault.com, please click

其他-如何在Flutter中使用可滑动选项卡

(其他 - How to Use Swipable Tabs in Flutter)

发布于 2020-11-28 03:12:23

应用程序屏幕之一需要可滑动的选项卡,所以我只想实现默认控制器,官方语法是:

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属性或覆盖home属性。帮我解决这个问题

Questioner
KARTHIKEYAN N
Viewed
0
Bensal 2020-11-28 12:43:05

你可以使用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是什么?