简体   繁体   English

如何在不改变 AppBar 的情况下更改 TabBar 的背景颜色?

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

How to change background color of TabBar without changing the AppBar ?如何在不更改AppBar情况下更改TabBar背景颜色? The TabBar does not have a background property, is there a workaround? TabBar没有background属性,有解决方法吗?

Create a simple Widget for this, cannot be simpler:为此创建一个简单的 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,
  );
}

You can change the color of the TabBar by changing the Theme primaryColor like that:您可以通过像这样更改 Theme primaryColor 来更改 TabBar 的颜色:

return MaterialApp(
      theme: ThemeData(
        brightness: Brightness.light,
        primaryColor: Colors.pink[800], //Changing this will change the color of the TabBar
        accentColor: Colors.cyan[600],
      ),
      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:如果您不在 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),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Change Background Color of TabBar in Flutter..在 Flutter 中更改 TabBar 的背景颜色..

Simply use TabBar in Body of Scaffold, wrap it with Column Widget so that, you can use both without any issue.只需在 Scaffold 主体中使用 TabBar,将其与 Column Widget 一起使用,这样您就可以毫无问题地使用两者。 Wrap TabBar with Container widget to change the tab color.用容器小部件包裹 TabBar 以更改选项卡颜色。 In this way you can change the color of Tab bar in FLutter.这样你就可以在 FLutter 中改变 Tab bar 的颜色。

Here's the sample Code...这是示例代码...

   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()],
               ),
            ),
         ],
    ),
  );

Screenshot:截屏:

在此处输入图片说明


Code:代码:

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,
          ),
        ),
      ),
    ),
  );
}

Simple and concise.简单而简洁。

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;
}

for use with SliverPersistenHeader (etc collapsing with sliver appbar in nested scrollview)与 SliverPersistenHeader 一起使用(等在嵌套滚动视图中与 sliver appbar 一起折叠)

 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;
  }
}

So if you are looking for to change the specific tab color and then you can try the following code:因此,如果您正在寻找更改特定选项卡颜色,那么您可以尝试以下代码:

  1. Create a variable of color:创建一个颜色变量:
    Color tabColor=Colors.green;颜色 tabColor=Colors.green;

  2. Create onTap method inside the TabBar and inside the tab bar create setState() method code is given below:在 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); },
  3. Create an indicator and give some radius as per your requirement and change the color code is given:创建一个指示器并根据您的要求给出一些半径,并更改给出的颜色代码:

     indicator: BoxDecoration( borderRadius: BorderRadius.only(topLeft: Radius.circular(10),topRight: Radius.circular(10)), color: tabColor ),

You can change the Tabbar background-color, when Tabbar is inside the Material widget.当 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'),
                ],
              ),
            ),

with a little trick用一个小技巧

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所以诀窍是,用容器包裹标签并使容器的宽度相同,然后根据您的意愿更改容器的颜色

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM