Use the TabBar widget with the BLoC (Business Logic Component) pattern in Flutter

Cronative Technologies
2 min readDec 22, 2022

The TabBar widget in Flutter is a horizontal row of tabs that allow the user to navigate between different views or pages within an app. To use the TabBar widget with the BLoC (Business Logic Component) pattern in Flutter, you'll need to do the following:

  1. Create a TabController: A TabController is responsible for coordinating the state of the TabBar and the TabBarView. You'll need to create a single TabController and share it between the TabBar and the TabBarView.
  2. Create a TabBar: The TabBar widget displays the tabs and allows the user to switch between them. You'll need to provide the TabController to the TabBar using the controller property. You can also customize the appearance of the TabBar using various properties such as tabs, indicatorColor, and labelColor.
  3. Create a TabBarView: The TabBarView widget displays the content of each tab. You'll need to provide the TabController to the TabBarView using the controller property. You can also customize the appearance of the TabBarView using various properties such as children and physics.

Here’s an example of how you might use the TabBar and TabBarView widgets with the BLoC pattern in Flutter:

class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
TabController _tabController;

@override
void initState() {
super.initState();
_tabController = TabController(vsync: this, length: 2);
}

@override
void dispose() {
_tabController.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('My App'),
bottom: TabBar(
controller: _tabController,
tabs: [
Tab(text: 'Tab 1'),
Tab(text: 'Tab 2'),
],
),
),
body: TabBarView(
controller: _tabController,
children: [
Text('Content of tab 1'),
Text('Content of tab 2'),
],
),
);
}
}

I hope this helps! Let me know if you have any questions.

--

--

Cronative Technologies

Cronative Technologies is combines the benefits of cross-platform development with the performance and capabilities of native app development.