Monday, October 15, 2018

Flutter working with Tabs

Below steps are involved in creating tabs


    Create a TabController
    Create the tabs
    Create content for each tab


Inorder for the tabs to work, one to need to keep the selected tab and the content are in sync. The TabController takes care of this

Below is how one can do that

import 'package:flutter/material.dart';

void main() {
  runApp(TabBarDemo());
}

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)),
                Tab(icon: Icon(Icons.directions_transit)),
                Tab(icon: Icon(Icons.directions_bike)),
              ],
            ),
            title: Text('Tabs Demo'),
          ),
          body: TabBarView(
            children: [
              Icon(Icons.directions_car),
              Icon(Icons.directions_transit),
              Icon(Icons.directions_bike),
            ],
          ),
        ),
      ),
    );
  }
}

References:
https://flutter.io/cookbook/design/tabs/

No comments:

Post a Comment