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

How to Use Swipable Tabs in Flutter

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

One of App Screen need swipable tabs , so i just wanna implement default controller by the way, the official syntax is :

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)),
        ],
      ),
    ),
  ),
);

Now, In my project the main.dart file holds routes and materialapp widget , it seems like

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
        });
  }

In Matches Screen(tabs screen) it will return Scaffold

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

here i could not use home property because home is the property of MaterialApp(), my question is how can i use tabs in this scenario , is there any way to replace the home property or override it. help me to resolve this problem

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

You can use body property instead of 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')
        ],
      ),
    )
);

But there are three TabBars, and if one TabBarView is MatchesScreen, then what are the two remaining TabBarView ?