Ant*_*ace 19
您可以通过更改主题primaryColor来更改TabBar的颜色:
return new MaterialApp(
theme: new ThemeData(
brightness: Brightness.light,
primaryColor: Colors.pink[800], //Changing this will change the color of the TabBar
accentColor: Colors.cyan[600],
),
home: new DefaultTabController(
length: 3,
child: new Scaffold(
appBar: new AppBar(
bottom: new TabBar(
indicatorColor: Colors.lime,
tabs: [
new Tab(icon: new Icon(Icons.directions_car)),
new Tab(icon: new Icon(Icons.directions_transit)),
new Tab(icon: new Icon(Icons.directions_bike)),
],
),
title: new Text('Tabs Demo'),
),
body: new TabBarView(
children: [
new Icon(Icons.directions_car),
new Icon(Icons.directions_transit),
new Icon(Icons.directions_bike),
],
),
),
),
);
Run Code Online (Sandbox Code Playgroud)
如果您没有在AppBar中使用它,您可以将TabBar包装在Material小部件中并设置color属性,如下所示:
class TabBarDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text('Tabs Demo'),
),
body: new DefaultTabController(
length: 3,
child: new Column(
children: <Widget>[
new Container(
constraints: BoxConstraints(maxHeight: 150.0),
child: new Material(
color: Colors.indigo,
child: new TabBar(
tabs: [
new Tab(icon: new Icon(Icons.directions_car)),
new Tab(icon: new Icon(Icons.directions_transit)),
new Tab(icon: new Icon(Icons.directions_bike)),
],
),
),
),
new Expanded(
child: new TabBarView(
children: [
new Icon(Icons.directions_car),
new Icon(Icons.directions_transit),
new Icon(Icons.directions_bike),
],
),
),
],
),
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
dev*_*mit 14
在 Flutter 中更改 TabBar 的背景颜色..
只需在 Scaffold 主体中使用 TabBar,将其与 Column Widget 一起使用,这样您就可以毫无问题地使用两者。用容器小部件包裹 TabBar 以更改选项卡颜色。这样你就可以在 FLutter 中改变 Tab bar 的颜色。
这是示例代码...
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()],
),
),
],
),
);
Run Code Online (Sandbox Code Playgroud)
小智 12
为此,创建一个简单的小部件,再简单不过了:
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,
);
}
Run Code Online (Sandbox Code Playgroud)
Cop*_*oad 10
截屏:
代码:
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,
),
),
),
),
);
}
Run Code Online (Sandbox Code Playgroud)
简单、简洁。
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14744 次 |
| 最近记录: |