Flutter Tabbar

 

Flutter Tabbar

In this section, we are going to learn how the tab bar works in Flutter. The tabs are mainly used for mobile navigation. The styling of tabs is different for different operating systems. For example, it is placed at the top of the screen in android devices while it is placed at the bottom in iOS devices.

Working with tabs is a common pattern in Android and iOS apps that follow the Material Design guidelines. Flutter provides a convenient way to create a tab layout. To add tabs to the app, we need to create a TabBar and TabBarView and attach them with the TabController. The controller will sync both so that we can have the behavior which we need.

Let us see step by step to create a tab bar in Flutter application.

Step 1: First, you need to create a Flutter project in your IDE. Here, I am going to use Android Studio.

Step 2: Open the app in Android Studio and navigate to the lib folder. Inside the lib folder, create two dart files and named it FirstScreen and SecondScreen.

Write the following code in the FirstScreen:

  1. import 'package:flutter/material.dart';  
  2.   
  3. class FirstScreen extends StatelessWidget {  
  4.   @override  
  5.   Widget build(BuildContext context) {  
  6.     return Container(  
  7.       child: Center(  
  8.         child: Text('It is a contact tab, which is responsible for displaying the contacts stored in your mobile',  
  9.             style: TextStyle(fontSize: 32.0),  
  10.           )  
  11.       ),  
  12.     );  
  13.   }  
  14. }  

Write the following code in the SecondScreen:

  1. import 'package:flutter/material.dart';  
  2.   
  3. class SecondScreen extends StatelessWidget {  
  4.   @override  
  5.   Widget build(BuildContext context) {  
  6.     return Container(  
  7.       child: Center(  
  8.         child: Text('It is a second layout tab, which is responsible for taking pictures from your mobile.',  
  9.         style: TextStyle(fontSize: 35.0),  
  10.         ),  
  11.       ),  
  12.     );  
  13.   }  
  14. }  

Step 3: Next, we need to create a DefaultTabController. The DefaultTabController creates a TabController and makes it available to all widgets.

  1. DefaultTabController(  
  2.   // The number of tabs to display.  
  3.   length: 2,  
  4.   child: // Complete this code in the next step.  
  5. );  

In the above code, the length property tells about the number of tabs used in the app.

Step 4: Create the tab. We can create tabs by using the TabBar widget as below code.

  1. DefaultTabController(  
  2.   length: 2,  
  3.   child: Scaffold(  
  4.     appBar: AppBar(  
  5.       bottom: TabBar(  
  6.         tabs: [  
  7.           Tab(icon: Icon(Icons.directions_car)),  
  8.           Tab(icon: Icon(Icons.directions_bike)),  
  9.         ],  
  10.       ),  
  11.     ),  
  12.   ),  
  13. );  

Step 5: Create content for each tab so that when a tab is selected, it displays the content. For this purpose, we have to use the TabBarView widget as:

  1. TabBarView(  
  2.   children: [  
  3.   ],  
  4. );  

Step 6: Finally, open the main.dart file and insert the following code

  1. import 'package:flutter/material.dart';  
  2. import './FirstScreen.dart';  
  3. import './SecondScreen.dart';  
  4.   
  5. void main() => runApp(MyApp());  
  6.   
  7. class MyApp extends StatelessWidget {  
  8.   @override  
  9.   Widget build(BuildContext context) {  
  10.     return MaterialApp(  
  11.       home: DefaultTabController(  
  12.         length: 2,  
  13.         child: Scaffold(  
  14.           appBar: AppBar(  
  15.             title: Text('Flutter Tabs Demo'),  
  16.             bottom: TabBar(  
  17.               tabs: [  
  18.                 Tab(icon: Icon(Icons.contacts), text: "Tab 1"),  
  19.                 Tab(icon: Icon(Icons.camera_alt), text: "Tab 2")  
  20.               ],  
  21.             ),  
  22.           ),  
  23.           body: TabBarView(  
  24.             children: [  
  25.               FirstScreen(),  
  26.               SecondScreen(),  
  27.             ],  
  28.           ),  
  29.         ),  
  30.       ),  
  31.     );  
  32.   }  
  33. }  

Output

Now, run the app in your Android Studio. It will give the following screen where you can see two tab icons. So when you click any of the tab icons, it will display the associated screen.

Flutter Tabbar Flutter Tabbar

Display a SnackBar

SnackBar gives a brief message at the bottom of the screen on mobile devices and lower-left on larger devices and gets confirmation from the user. The snack bar automatically disappears after a certain amount of time.

Here, we are going to learn how we can implement a snack bar in Flutter. In Flutter, the snack bar only works with a scaffold widget context.

Let us take an example where we show the list of records, and contains a delete icon corresponding to every list. When we delete any record, it shows a message at the bottom of your screen. This message gets confirmation from the user. If it does not receive any confirmation, the message goes disappear automatically. The following example helps to understand it more clearly.

Create a Flutter project in Android Studio. Open the main.dart file and replace the following code.

  1. import 'package:flutter/material.dart';  
  2.   
  3. void main() => runApp(MyApp());  
  4.   
  5. class MyApp extends StatelessWidget {  
  6.   @override  
  7.   Widget build(BuildContext context) {  
  8.     return MaterialApp(  
  9.       theme: ThemeData(  
  10.         primarySwatch: Colors.green,  
  11.       ),  
  12.       home: MyHomePage(),  
  13.     );  
  14.   }  
  15. }  
  16.   
  17. class MyHomePage extends StatefulWidget {  
  18.   @override  
  19.   _MyHomePageState createState() => _MyHomePageState();  
  20. }  
  21.   
  22. class _MyHomePageState extends State<MyHomePage> {  
  23.   var lists = List<String>.generate(11, (index) => "List : $index");  
  24.   @override  
  25.   Widget build(BuildContext context) {  
  26.     return Scaffold(  
  27.       appBar: AppBar(  
  28.         title: Text('Flutter SnackBar Demo'),  
  29.       ),  
  30.       body: ListView.builder(  
  31.         itemCount: lists.length,  
  32.         itemBuilder: (context, index) {  
  33.           return ListTile(  
  34.             title: Text(lists[index]),  
  35.             trailing: Container(  
  36.               width: 60,  
  37.               child: FlatButton(  
  38.                 child: Icon(  
  39.                   Icons.delete,  
  40.                   color: Colors.grey,  
  41.                 ),  
  42.                 onPressed: () {  
  43.                   showSnackBar(context, index);  
  44.                 },  
  45.               ),  
  46.             ),  
  47.           );  
  48.         },  
  49.       ),  
  50.     );  
  51.   }  
  52.   
  53.   void showSnackBar(BuildContext context, int index) {  
  54.     var deletedRecord = lists[index];  
  55.     setState(() {  
  56.       lists.removeAt(index);  
  57.     });  
  58.     SnackBar snackBar = SnackBar(  
  59.       content: Text('Deleted $deletedRecord'),  
  60.       action: SnackBarAction(  
  61.         label: "UNDO",  
  62.         onPressed: () {  
  63.           setState(() {  
  64.             lists.insert(index, deletedRecord);  
  65.           });  
  66.         },  
  67.       ),  
  68.     );  
  69.     Scaffold.of(context).showSnackBar(snackBar);  
  70.   }  
  71. }  

Output

Now, run the app in Android Studio. You can see the following screen in the emulator. When you delete any list, it shows a message at the bottom of the screen. If you tap on the undo, it does not remove the list.

Flutter Tabbar Flutter Tabbar



Post a Comment

Previous Post Next Post