Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change background color of TabBar without changing the AppBar in flutter?

How to change background color of TabBar without changing the AppBar? The TabBar does not have a background property, is there a workaround?

like image 219
amater Avatar asked May 28 '18 12:05

amater


People also ask

How do you change the background color of a TabBar in flutter?

To change tab bar background color in Flutter, first, create a getter to return the TabBar widget and then wrap the TabBar widget inside the PreferredSize -> Material widget. Inside the Material add the color property and set the color of your choice.

How do you change the color of the AppBar in flutter?

Step 1: Locate the MaterialApp widget. Step 2: Inside the MaterialApp, add the theme parameter with ThemeData class assigned. Step 3: Inside the ThemeData add the appBarTheme parameter and then assign the AppBarTheme class. Step 4: Inside the AppBarTheme , specify the color parameter and set the color.


1 Answers

You can change the color of the TabBar by changing the Theme primaryColor like that:

return MaterialApp(       theme: ThemeData(         brightness: Brightness.light,       // add tabBarTheme        tabBarTheme: const TabBarTheme(         labelColor: Colors.pink[800],         labelStyle: TextStyle(color: Colors.pink[800]), // color for text         indicator: UnderlineTabIndicator( // color for indicator (underline)           borderSide: BorderSide(color: ConstColor.primary))),         primaryColor: Colors.pink[800], // outdated and has no effect to Tabbar         accentColor: Colors.cyan[600] // deprecated,       ),       home: DefaultTabController(         length: 3,         child: Scaffold(           appBar: AppBar(             bottom: TabBar(               indicatorColor: Colors.lime,               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),             ],           ),         ),       ),     ); 

If you are not using it in an AppBar you could wrap the TabBar in a Material widget and set the color attribute, like that:

class TabBarDemo extends StatelessWidget {   @override   Widget build(BuildContext context) {     return MaterialApp(       home: Scaffold(         appBar: AppBar(           title: Text('Tabs Demo'),         ),         body: DefaultTabController(           length: 3,           child: Column(             children: <Widget>[               Container(                 constraints: BoxConstraints(maxHeight: 150.0),                 child: Material(                   color: Colors.indigo,                   child: TabBar(                     tabs: [                       Tab(icon: Icon(Icons.directions_car)),                       Tab(icon: Icon(Icons.directions_transit)),                       Tab(icon: Icon(Icons.directions_bike)),                     ],                   ),                 ),               ),               Expanded(                 child: TabBarView(                   children: [                     Icon(Icons.directions_car),                     Icon(Icons.directions_transit),                     Icon(Icons.directions_bike),                   ],                 ),               ),             ],           ),         ),       ),     );   } } 
like image 152
Antonino Avatar answered Sep 19 '22 15:09

Antonino