如何在不更改 AppBar 的情况下更改 TabBar 的背景颜色?
How to change background color of TabBar without changing the AppBar in flutter?
如何在不改变 AppBar
的情况下改变 TabBar
的背景颜色?
TabBar
没有 background
属性,有解决方法吗?
您可以通过像这样更改 Theme primaryColor 来更改 TabBar 的颜色:
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),
],
),
),
),
);
如果您不在 AppBar 中使用它,您可以将 TabBar 包装在 Material 小部件中并设置颜色属性,如下所示:
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),
],
),
),
],
),
),
),
);
}
}
为此创建一个简单的Widget,再简单不过了:
class ColoredTabBar extends Container implements PreferredSizeWidget {
ColoredTabBar(this.color, this.tabBar);
final Color color;
final TabBar tabBar;
@override
Size get preferredSize => tabBar.preferredSize;
@override
Widget build(BuildContext context) => Container(
color: color,
child: tabBar,
);
}
在 Flutter 中更改 TabBar 的背景颜色..
只需在 Body of Scaffold 中使用 TabBar,然后用 Column Widget 将其包裹起来,这样您就可以毫无问题地使用两者。使用 Container 小部件包裹 TabBar 以更改选项卡颜色。
通过这种方式,您可以更改 FLutter 中 Tab 栏的颜色。
这是示例代码...
Scaffold(
appBar: AppBar(
backgroundColor: const Color(0xFF3baee7),
title: Text("Jobs"),
),
body: Column( // Column
children: <Widget>[
Container(
color: Colors.deepOrangeAccent, // Tab Bar color change
child: TabBar( // TabBar
controller: tabController,
unselectedLabelColor: Colors.lightBlue[100],
labelColor: const Color(0xFF3baee7),
indicatorWeight: 2,
indicatorColor: Colors.blue[100],
tabs: <Widget>[
Tab(
text: "All Jobs",
),
Tab(
text: "Most Recent",
),
Tab(
text: "Saved Jobs",
)
],
),
),
Expanded(
flex: 3,
child: TabBarView( // Tab Bar View
physics: BouncingScrollPhysics(),
controller: tabController,
children: <Widget>[AllJobScreen(), AllJobScreen(), AllJobScreen()],
),
),
],
),
);
截图:
代码:
TabBar get _tabBar => TabBar(
tabs: [
Tab(icon: Icon(Icons.call)),
Tab(icon: Icon(Icons.message)),
],
);
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 2,
child: Scaffold(
appBar: AppBar(
title: Text('AppBar'),
bottom: PreferredSize(
preferredSize: _tabBar.preferredSize,
child: ColoredBox(
color: Colors.red,
child: _tabBar,
),
),
),
),
);
}
简洁明了。
class ColoredTabBar extends ColoredBox implements PreferredSizeWidget {
ColoredTabBar({this.color, this.tabBar}) : super(color: color, child: tabBar);
final Color color;
final TabBar tabBar;
@override
Size get preferredSize => tabBar.preferredSize;
}
与 SliverPersistentHeader 一起使用(e.t.c 在嵌套滚动视图中与 sliverappbar 一起折叠)
class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
_SliverAppBarDelegate(this._tabBar);
final TabBar _tabBar;
@override
double get minExtent => _tabBar.preferredSize.height;
@override
double get maxExtent => _tabBar.preferredSize.height;
@override
Widget build(
BuildContext context, double shrinkOffset, bool overlapsContent) {
return new Container(
child: _tabBar,
color: Colors.red,
);
}
@override
bool shouldRebuild(_SliverAppBarDelegate oldDelegate) {
return false;
}
}
因此,如果您正在寻找更改特定选项卡颜色的方法,那么您可以尝试以下代码:
创建颜色变量:
颜色 tabColor=Colors.green;
在TabBar内创建onTap方法和在标签栏内创建setState()
方法代码如下:
onTap: (index) {
setState(() {
if(index==0) { tabColor = Colors.lightGreen;}
if(index==1) {tabColor = Colors.yellow;}
if(index==2) {tabColor = Colors.green;}
if(index==3) {tabColor = Colors.red;}
if(index==4) {tabColor = Colors.deepPurple;}
});
print(index);
},
创建一个指标并根据您的要求提供一些半径并更改给定的颜色代码:
indicator: BoxDecoration(
borderRadius: BorderRadius.only(topLeft:
Radius.circular(10),topRight: Radius.circular(10)),
color: tabColor
),
当 Tabbar 位于 Material 小部件内时,您可以更改 Tabbar 背景颜色。
Material(
color: Colors.white, //Tabbar background-color
child: TabBar(
isScrollable: true,
labelStyle: Theme.of(context).tabBarTheme.labelStyle,
unselectedLabelStyle:
Theme.of(context).tabBarTheme.unselectedLabelStyle,
labelColor: Theme.of(context).tabBarTheme.labelColor,
unselectedLabelColor:
Theme.of(context).tabBarTheme.unselectedLabelColor,
indicatorColor: Theme.of(context).primaryColor,
tabs: [
Tab(text: 'tab 1'),
Tab(text: 'tab 2'),
Tab(text: 'tab 3'),
Tab(text: 'tab 4'),
],
),
),
有点小技巧
bottom: TabBar(
labelPadding: EdgeInsets.zero, // this one make 0
tabs: [
for (final t in [1, 2, 3])
Container(
width: double.infinity,
color: Colors.blue, // color same as background
child: Tab(
text: "tab $t",
),
)
],
),
so the trick is, wrap the tab with a container and make the width of the container the same and then change the color of the container according to your wishes
如何在不改变 AppBar
的情况下改变 TabBar
的背景颜色?
TabBar
没有 background
属性,有解决方法吗?
您可以通过像这样更改 Theme primaryColor 来更改 TabBar 的颜色:
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),
],
),
),
),
);
如果您不在 AppBar 中使用它,您可以将 TabBar 包装在 Material 小部件中并设置颜色属性,如下所示:
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),
],
),
),
],
),
),
),
);
}
}
为此创建一个简单的Widget,再简单不过了:
class ColoredTabBar extends Container implements PreferredSizeWidget {
ColoredTabBar(this.color, this.tabBar);
final Color color;
final TabBar tabBar;
@override
Size get preferredSize => tabBar.preferredSize;
@override
Widget build(BuildContext context) => Container(
color: color,
child: tabBar,
);
}
在 Flutter 中更改 TabBar 的背景颜色..
只需在 Body of Scaffold 中使用 TabBar,然后用 Column Widget 将其包裹起来,这样您就可以毫无问题地使用两者。使用 Container 小部件包裹 TabBar 以更改选项卡颜色。 通过这种方式,您可以更改 FLutter 中 Tab 栏的颜色。
这是示例代码...
Scaffold(
appBar: AppBar(
backgroundColor: const Color(0xFF3baee7),
title: Text("Jobs"),
),
body: Column( // Column
children: <Widget>[
Container(
color: Colors.deepOrangeAccent, // Tab Bar color change
child: TabBar( // TabBar
controller: tabController,
unselectedLabelColor: Colors.lightBlue[100],
labelColor: const Color(0xFF3baee7),
indicatorWeight: 2,
indicatorColor: Colors.blue[100],
tabs: <Widget>[
Tab(
text: "All Jobs",
),
Tab(
text: "Most Recent",
),
Tab(
text: "Saved Jobs",
)
],
),
),
Expanded(
flex: 3,
child: TabBarView( // Tab Bar View
physics: BouncingScrollPhysics(),
controller: tabController,
children: <Widget>[AllJobScreen(), AllJobScreen(), AllJobScreen()],
),
),
],
),
);
截图:
代码:
TabBar get _tabBar => TabBar(
tabs: [
Tab(icon: Icon(Icons.call)),
Tab(icon: Icon(Icons.message)),
],
);
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 2,
child: Scaffold(
appBar: AppBar(
title: Text('AppBar'),
bottom: PreferredSize(
preferredSize: _tabBar.preferredSize,
child: ColoredBox(
color: Colors.red,
child: _tabBar,
),
),
),
),
);
}
简洁明了。
class ColoredTabBar extends ColoredBox implements PreferredSizeWidget {
ColoredTabBar({this.color, this.tabBar}) : super(color: color, child: tabBar);
final Color color;
final TabBar tabBar;
@override
Size get preferredSize => tabBar.preferredSize;
}
与 SliverPersistentHeader 一起使用(e.t.c 在嵌套滚动视图中与 sliverappbar 一起折叠)
class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
_SliverAppBarDelegate(this._tabBar);
final TabBar _tabBar;
@override
double get minExtent => _tabBar.preferredSize.height;
@override
double get maxExtent => _tabBar.preferredSize.height;
@override
Widget build(
BuildContext context, double shrinkOffset, bool overlapsContent) {
return new Container(
child: _tabBar,
color: Colors.red,
);
}
@override
bool shouldRebuild(_SliverAppBarDelegate oldDelegate) {
return false;
}
}
因此,如果您正在寻找更改特定选项卡颜色的方法,那么您可以尝试以下代码:
创建颜色变量:
颜色 tabColor=Colors.green;在TabBar内创建onTap方法和在标签栏内创建
setState()
方法代码如下:onTap: (index) { setState(() { if(index==0) { tabColor = Colors.lightGreen;} if(index==1) {tabColor = Colors.yellow;} if(index==2) {tabColor = Colors.green;} if(index==3) {tabColor = Colors.red;} if(index==4) {tabColor = Colors.deepPurple;} }); print(index); },
创建一个指标并根据您的要求提供一些半径并更改给定的颜色代码:
indicator: BoxDecoration( borderRadius: BorderRadius.only(topLeft: Radius.circular(10),topRight: Radius.circular(10)), color: tabColor ),
当 Tabbar 位于 Material 小部件内时,您可以更改 Tabbar 背景颜色。
Material(
color: Colors.white, //Tabbar background-color
child: TabBar(
isScrollable: true,
labelStyle: Theme.of(context).tabBarTheme.labelStyle,
unselectedLabelStyle:
Theme.of(context).tabBarTheme.unselectedLabelStyle,
labelColor: Theme.of(context).tabBarTheme.labelColor,
unselectedLabelColor:
Theme.of(context).tabBarTheme.unselectedLabelColor,
indicatorColor: Theme.of(context).primaryColor,
tabs: [
Tab(text: 'tab 1'),
Tab(text: 'tab 2'),
Tab(text: 'tab 3'),
Tab(text: 'tab 4'),
],
),
),
有点小技巧
bottom: TabBar(
labelPadding: EdgeInsets.zero, // this one make 0
tabs: [
for (final t in [1, 2, 3])
Container(
width: double.infinity,
color: Colors.blue, // color same as background
child: Tab(
text: "tab $t",
),
)
],
),
so the trick is, wrap the tab with a container and make the width of the container the same and then change the color of the container according to your wishes