I have three tabs in my tabBar, a button icon and two texts. I want the icon to be smaller than the texts. But they are all the same width. I didn't find any property that seven this width inside tabBar.
In gray you can see what I wanted to decrease the width, is this possible?
Below is my code, where I tried to wrap the tabs in a container but I was not successful.
return TabBar(
labelColor: Colors.black,
unselectedLabelColor: Colors.blue,
indicatorColor: Colors.blue,
indicatorSize: TabBarIndicatorSize.label,
tabs: [
Container(
color: Colors.purple,
width: widthScreen * .10,
child: Tab(
icon: Icon(
(CustomIcons.arrow_back),
size: 30,
),
),
),
Container(
color: Colors.amber,
width: widthScreen * .70,
child: Tab(text: 'TEST 1')),
Container(
color: Colors.red,
width: widthScreen * .70,
child: Tab(
text: 'TEST 2',
),
),
],
);
Include isScrollable: true, on TabBar.
return TabBar(
isScrollable: true, //this
labelColor: Colors.black,
unselectedLabelColor: Colors.blue,
indicatorColor: Colors.blue,
I have 3 TabBars and I want to place the label to the left of the categories.
The TabBar I want:
My TabBar:
My code:
// BOTTOM
bottom: PreferredSize(
preferredSize: const Size.fromHeight(kToolbarHeight),
child: Align(
alignment: Alignment.centerLeft,
child: Container(
width: MediaQuery.of(context).size.width / 1.1,
child: TabBar(
labelPadding: EdgeInsets.all(0),
labelColor: Colors.white,
labelStyle: poppins.copyWith(
fontSize: 15,
fontWeight: bold,
),
unselectedLabelColor: Color(0xff585861),
indicatorColor: Colors.white,
indicatorSize: TabBarIndicatorSize.label,
// TABS
tabs: [
Tab(
text: 'Following',
),
Tab(
text: 'Trending',
),
Tab(
text: 'Search',
),
],
),
),
),
),
Refer below code and try to add isScrollable: true, inside TabBar() widget
Refer Tabbar here
Refer isScrollable here
bottom: PreferredSize(
preferredSize: const Size.fromHeight(kToolbarHeight),
child: Align(
alignment: Alignment.centerLeft,
child: Container(
width: MediaQuery.of(context).size.width / 1.1,
child: TabBar(
labelPadding: EdgeInsets.all(5),
labelColor: Colors.white,
isScrollable: true,// add this property
unselectedLabelColor: Color(0xff585861),
indicatorColor: Colors.white,
indicatorSize: TabBarIndicatorSize.label,
// TABS
tabs: [
Tab(
text: 'Following',
),
Tab(
text: 'Trending',
),
Tab(
text: 'Search',
),
],
),
),
),
),
Your result screen->
I'm able to find the solution to this.
Just by adding isScrollable: true parameter to TabBar() all tabs shrink to one side.
Without setting isScrollable: true all tabs items were taking all the space they have in the given widget.
bottom: PreferredSize(
preferredSize: const Size.fromHeight(kToolbarHeight),
child: Align(
alignment: Alignment.centerLeft,
child: Container(
width: MediaQuery.of(context).size.width / 1.1,
child: TabBar(
isScrollable: true,
labelPadding: EdgeInsets.all(0),
labelColor: Colors.white,
labelStyle: poppins.copyWith(
fontSize: 15,
fontWeight: bold,
),
unselectedLabelColor: Color(0xff585861),
indicatorColor: Colors.white,
indicatorSize: TabBarIndicatorSize.label,
// TABS
tabs: [
Tab(
text: 'Following',
),
Tab(
text: 'Trending',
),
Tab(
text: 'Search',
),
],
),
),
),
)
I want to give borders between UN-selected tabs i tried this but not working. Border between around live so when i switch tabs it will look grey instead of empty space
Container(
clipBehavior: Clip.hardEdge,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(16),
border: Border.all(color: lightGrey)),
child: TabBar(
indicator: BoxDecoration(
color: Colors.green,
border: Border.symmetric(
vertical: BorderSide(color: lightGrey),
)),
labelStyle: body14_500(Colors.white),
unselectedLabelStyle: body14_500(dark3),
indicatorColor: white,
labelPadding: EdgeInsets.symmetric(horizontal: 24),
unselectedLabelColor: dark3,
labelColor: Colors.white,
tabs: [
Tab(text: "Upcoming"),
Container(
decoration: BoxDecoration(
border: Border.symmetric(
vertical: BorderSide(color: lightGrey),
),),
child: Tab(text: "Live")),
Tab(text: "Completed"),
],
),
)
this is how it look
Expected this
this worked for me
tabs: [
Tab(text: "Upcoming"),
Tab(
child:Row(
MainAxisAlignment.spaceBetween,
children: <Widget>[
VerticalDivider(),
Text("Lives"),
VerticalDivider(),
]
),
),
Tab(text: "Completed"),
],
You can use CupertinoSegmentedControl to achieve this
CupertinoSegmentedControl<int>(
onValueChanged: (value) => setState(() => selectedIndex = value),
children: cupertinoMap,
padding: const EdgeInsets.all(12),
groupValue: selectedIndex,
selectedColor: Colors.green,
pressedColor: Colors.green.shade100,
unselectedColor: Colors.white,
borderColor: Colors.grey.shade300,
),
Full code at dartpad
Screenshot
I tried to use NestedScrollView but was unsuccessful, I don't know where I can put the tabbarview how can I use tabbarview in this code
my app looks like this:
SliverToBoxAdapter _buildRegionTabBar() { //brasil e mundo
return SliverToBoxAdapter(
child: DefaultTabController(
length: 2,
child: Container(
margin: const EdgeInsets.symmetric(horizontal: 20.0),
height: 50.0,
decoration: BoxDecoration(
color: Colors.white24,
borderRadius: BorderRadius.circular(25.0),
),
child: TabBar(
indicator: BubbleTabIndicator(
tabBarIndicatorSize: TabBarIndicatorSize.tab,
indicatorHeight: 40.0,
indicatorColor: Colors.white,
),
labelStyle: Styles.tabTextStyle,
labelColor: Colors.black,
unselectedLabelColor: Colors.white,
tabs: <Widget>[
Tab (text:'Brasil'),
Tab (text:'Mundo'),
],
onTap: (index) {},
),
),
),
);
}
}
You're missing TabBarView.
This is how it should be done. Modify this code to achieve your result:
DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
bottom: TabBar(
tabs: [
Tab(icon: Icon(Icons.flight)),
Tab(icon: Icon(Icons.directions_transit)),
Tab(icon: Icon(Icons.directions_car)),
],
),
title: Text('Tabs Demo'),
),
body: TabBarView(
children: [
Icon(Icons.flight, size: 350),
Icon(Icons.directions_transit, size: 350),
Icon(Icons.directions_car, size: 350),
],
),
),
);
For more info you can refer here : https://blog.logrocket.com/flutter-tabbar-a-complete-tutorial-with-examples/
I have a tab bar and i need to put a vertical line as a divider between tabs, how to do that?
this how i used my tabbar:
new TabBar(
unselectedLabelColor: Color.fromRGBO(119, 119, 119, 1),
labelColor: Colors.black,
controller: controller,
tabs: <Tab>[
new Tab(text: "Girls"),
new Tab(text: "Hero"),
new Tab(text: "Open"),
]),
and I need it to be like this:
Finally It worked for me
TabBar(
tabs: [
_individualTab('assets/icons/bottom_nav/Home.png'),
_individualTab('assets/icons/bottom_nav/Guys.png'),
_individualTab('assets/icons/bottom_nav/Notes.png'),
Tab(
icon: ImageIcon(
AssetImage('assets/icons/bottom_nav/Email.png')
),
),
],
labelColor: STColors.PRIMARY_COLOR,
unselectedLabelColor: Colors.grey,
indicatorColor: Colors.white,
indicatorSize: TabBarIndicatorSize.tab,
labelPadding: EdgeInsets.all(0),
indicatorPadding: EdgeInsets.all(0),
),
Individual Tab Function
Widget _individualTab(String imagePath) {
return Container(
height: 50 + MediaQuery
.of(context)
.padding
.bottom,
padding: EdgeInsets.all(0),
width: double.infinity,
decoration: BoxDecoration(border: Border(right: BorderSide(color: STColors.LIGHT_BORDER, width: 1, style: BorderStyle.solid))),
child: Tab(
icon: ImageIcon(AssetImage(imagePath)),
),
);
}
To achieve small size separator you can use this.
Widget _individualTab(String imagePath) {
return Container(
height: 50,
width: double.infinity,
decoration: BoxDecoration(
border: Border(right: BorderSide(color: STColors.LIGHT_BORDER,
width: 0,
style: BorderStyle.solid),
),
),
child: Stack(children: <Widget>[
Center(
child: Tab(
icon: ImageIcon(AssetImage(imagePath)),
),
),
Align(
alignment: Alignment.centerRight,
child: Container(
color: STColors.appBlackMedium,
width: 1,
height: 25,
),
)
],)
);
}
All you need is
indicator: BoxDecoration(
border: Border(
left: BorderSide(color: Colors.grey), // provides to left side
right: BorderSide(color: Colors.grey), // for right side
),
),
Your solution:
new TabBar(
indicator: BoxDecoration(border: Border(right: BorderSide(color: Colors.orange))),
unselectedLabelColor: Color.fromRGBO(119, 119, 119, 1),
labelColor: Colors.black,
controller: controller,
tabs: <Tab>[
new Tab(text: "Girls"),
new Tab(text: "Hero"),
new Tab(text: "Open"),
]),
Here is the Header I want to achieve
Yea, so I added a var called rightDivider which lets you render divider but the last tab on my custom tab widget.
import 'package:flutter/material.dart';
class TabWidget extends StatelessWidget {
final String label;
final bool rightDivider;
TabWidget({
required this.label,
required this.rightDivider,
});
#override
Widget build(BuildContext context) {
return Container(
height: 32 + MediaQuery.of(context).padding.bottom,
width: double.infinity,
padding: EdgeInsets.all(0),
decoration: (rightDivider)
? BoxDecoration(
border: Border(
right: BorderSide(
color: Colors.grey,
width: 1,
style: BorderStyle.solid,
),
),
)
: null,
child: Center(child: Text(label)),
);
}
}
And called this widget like this
TabBar(
controller: _tabController,
tabs: [
TabWidget(
label: 'Today',
rightDivider: true,
),
TabWidget(
label: 'Calendar',
rightDivider: true,
),
TabWidget(
label: 'Report',
rightDivider: false,
),
],
)
That's how I achieve this behavior...
HomeTab({required String title, bool isSeprator = true}) {
return Tab(
child: Container(
width: 100,
height: 20,
child: Center(
child: Text(title),
),
decoration: isSeprator
? BoxDecoration(
border: Border(
left: BorderSide(
color: Colors.grey,
width: 1,
style: BorderStyle.solid,
),
),
)
: null,
),
);
}
isSeparator is to show separator or not ... because on the last index I am not showing...
Following is the list of tabs ...
List<Widget> _tabScroll() => [
HomeTab(title: "Tab1"),
HomeTab(title: "Tab2"),
HomeTab(title: "Tab3", isSeparator: false),
];
and following is the most important TabBar Code...
TabBar(
labelPadding: EdgeInsets.symmetric(horizontal: 0), //removing extra space
controller: _controller,
isScrollable: true,
indicatorColor: Colors.black,
indicatorSize: TabBarIndicatorSize.label,
indicator: UnderlineTabIndicator(
borderSide: BorderSide(width: 3.0), //hight of indicator
insets: EdgeInsets.symmetric(horizontal: 30.0), //give some padding to reduce the size of indicator
),
unselectedLabelColor: Colors.grey,
tabs: _tabScroll(), //list of tabs
),
The end result looks like this...
Working solution is to create border for individual tabs. Tabs parameter actually accept other elements as a child. So you can use custom containers. That way you can control which side border you want to show (left, rigth or any side):
TabBar(
controller: _tabController,
indicator: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.red,
offset: Offset(0, 2.0),
)
],
color: Colors.white,
),
labelColor: Colors.red,
unselectedLabelColor: Colors.grey.shade900,
labelPadding: EdgeInsets.symmetric(horizontal: 1.0),
isScrollable: false,
tabs: [
Container(
child: Center(
child: Text('T1'),
),
decoration: BoxDecoration(
border: Border(
right: BorderSide(
color: Colors.grey,
width: 2,
style: BorderStyle.solid,
),
),
)),
Container(
child: Center(
child: Text('T2'),
),
decoration: BoxDecoration(
border: Border(
right: BorderSide(
color: Colors.grey,
width: 2,
style: BorderStyle.solid,
),
),
)),
Container(
child: Center(
child: Text('T3'),
),
decoration: BoxDecoration(
border: Border(
right: BorderSide(
color: Colors.grey,
width: 2,
style: BorderStyle.solid,
),
),
)),
Tab(
text: 'T4',
),
],
),
Thats how i have done this
I am using screenutils package from pub you can also use it
SizedBox(
width: 342.w,
height: 41.h,
child: Material(
color: const Color(0xffE2E2E2),
borderRadius: BorderRadius.circular(5),
child: Padding(
padding: EdgeInsets.all(2),
child: TabBar(
padding: EdgeInsets.zero,
indicatorPadding: EdgeInsets.zero,
labelPadding: EdgeInsets.zero,
indicator: BoxDecoration(
color: Color(0xffFFFFFF),
// borderRadius: BorderRadius.circular(5.0)
),
labelColor: Colors.black,
unselectedLabelColor: Color(0xff999BA2),
tabs: [
Container(
width: 1.sw,
decoration: BoxDecoration(
// color: Colors.black,
border: Border(right: BorderSide(color: Colors.white))
),
child: Tab(
text: 'Call',
),
),
Container(
width: 1.sw,
decoration: BoxDecoration(
border: Border(right: BorderSide(color: Colors.white))
),
child: Tab(
text: 'Missed Call',
),
// child: Tab(text: 'Phone',),
),
Container(
child: Tab(
text: 'No Call',
),
// child: Tab(text: 'No Call'),
),
],
),
),
),
)
enter image description here