简体   繁体   English

在颤动中更改标签栏的背景颜色

[英]Change background color of Tab bar in flutter

I am trying to change the background color of the tab bar in flutter, I have tried the following ( which was accepted as an answer on this forum ) but it didnt work:我正在尝试更改颤振中选项卡栏的背景颜色,我尝试了以下方法(在本论坛上被接受为答案),但没有奏效:

here is the code这是代码

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

EDIT :编辑 :

When I change the theme data colors the background color doesnt change.当我更改主题数据颜色时,背景颜色不会改变。 Im trying to create a horizontal scrolling sub menu underneath the app bar and it was suggested I use a Tab bar.我试图在应用栏下方创建一个水平滚动子菜单,有人建议我使用 Tab 栏。

Here is the entire dart file:这是整个 dart 文件:

  import 'package:flutter/material.dart';
  import 'package:font_awesome_flutter/font_awesome_flutter.dart';

  class Index extends StatelessWidget {

//final User user;

  // HomeScreen({Key key, @required this.user}) : super(key: key);

  @override
   Widget build(BuildContext context) {
     // TODO: implement build
    // String emoji = emojify(":cool:");
   return new MaterialApp(
     theme: new ThemeData(
      brightness: Brightness.light,
       primaryColor: Colors.white, //Changing this will change the color of     the TabBar
      accentColor: Colors.cyan[600],
     ),

    home: DefaultTabController(
    length: 2,
  child: Scaffold(
  appBar: new AppBar(
     backgroundColor: const Color(0xFF0099a9),
     title: new Image.asset('images/lb_appbar_small.png', fit: BoxFit.none,),
     bottom: TabBar(
          tabs: [
            Tab( text: "Tab 1",),
            Tab(text: "Tab 2"),
             ],       
     ),
  ),
    body: Column(children: <Widget>[
          Row(
            //ROW 1
            children: [
              Container(
                margin: EdgeInsets.all(25.0),
                child: IconButton(
                     icon: new Icon(FontAwesomeIcons.checkSquare,),
                     iconSize: 60.0,
                     color: const Color(0xFF0099a9),
                      onPressed: () { print("Pressed"); }
                ),
              ),
              Container(
                margin: EdgeInsets.all(25.0),
                  child: IconButton(
                     icon: new Icon(FontAwesomeIcons.glasses,),
                     iconSize: 60.0,
                     color: const Color(0xFF0099a9),
                      onPressed: () { print("Pressed"); }
                  )

              ),
            Container(
                margin: EdgeInsets.all(25.0),
                  child: IconButton(
                     icon: new Icon(FontAwesomeIcons.moon,),
                     iconSize: 60.0,
                     color: const Color(0xFF0099a9),
                      onPressed: () { print("Pressed"); 
                      Text("Check Out");
                      }
                  )

              ),
            ]
          ),
          Row(//ROW 2
              children: [
            Container(
                margin: EdgeInsets.all(25.0),
                  child: IconButton(
                     icon: new Icon(FontAwesomeIcons.users,),
                     iconSize: 60.0,
                     color: const Color(0xFF0099a9),
                      onPressed: () { print("Pressed"); }
                  )

              ),
            Container(
                margin: EdgeInsets.all(25.0),
                  child: IconButton(
                     icon: new Icon(FontAwesomeIcons.trophy,),
                     iconSize: 60.0,
                     color: const Color(0xFF0099a9),
                      onPressed: () { print("Pressed"); }
                  )

              ),
             Container(
                margin: EdgeInsets.all(25.0),
                  child: IconButton(
                     icon: new Icon(FontAwesomeIcons.calendar,),
                     iconSize: 60.0,
                     color: const Color(0xFF0099a9),
                      onPressed: () { print("Pressed"); }
                  )

              )
          ]),
          Row(// ROW 3
              children: [
            Container(
                margin: EdgeInsets.all(25.0),
                  child: IconButton(
                     icon: new Icon(FontAwesomeIcons.fileExcel,),
                     iconSize: 60.0,
                     color: const Color(0xFF0099a9),
                      onPressed: () { print("Pressed"); }
                  )

              ),
            Container(
                margin: EdgeInsets.all(25.0),
                  child: IconButton(
                     icon: new Icon(FontAwesomeIcons.shoppingCart,),
                     iconSize: 60.0,
                     color: const Color(0xFF0099a9),
                      onPressed: () { print("Pressed"); }
                  )

              ),
             Container(
                margin: EdgeInsets.all(25.0),
                  child: IconButton(
                     icon: new Icon(FontAwesomeIcons.list,),
                     iconSize: 60.0,
                     color: const Color(0xFF0099a9),
                      onPressed: () { print("Pressed"); }
                  )

              ),
          ]),
        ],
        ),


 bottomNavigationBar: new BottomNavigationBar(
    items: [
      new BottomNavigationBarItem(
          icon: new Icon(Icons.feedback, color: const Color(0xFF0099a9),),
          title: new Text("feedback")
      ),
      new BottomNavigationBarItem(
          icon: new Icon(Icons.help, color: const Color(0xFF0099a9),),
          title: new Text("help")
      ),
      new BottomNavigationBarItem(
          icon: new Icon(Icons.people, color: const Color(0xFF0099a9),),
          title: new Text("community",)
       )
     ]
    )







         )
   )
 );






  }
}

You have the TabBar inside your AppBar for that reason it take the same color, just move the TabBar outside the Appbar由于这个原因,您的AppBar内有TabBar ,因此它采用相同的颜色,只需将TabBar移到Appbar之外

    Scaffold(
                    appBar: new AppBar(
                      backgroundColor: const Color(0xFF0099a9),
                      title: new Image.asset(
                        'images/lb_appbar_small.png',
                        fit: BoxFit.none,
                      ),
                    ),
                    body: Column(
                      children: <Widget>[
                        TabBar(
                          tabs: [
                            Tab(
                              text: "Tab 1",
                            ),
                            Tab(text: "Tab 2"),
                          ],
                        ),
                        Row(
                            //ROW 1
                         ....

Hi you are getting same color since you have added tabbar in appbar,If you are looking for different color for both TabBar and Appbar .嗨,因为您在 appbar 中添加了 tabbar,所以您得到了相同的颜色,如果您正在为TabBarAppbar寻找不同的颜色。 Here is what you have to do.这是你必须做的。
*Add TabBar in body of Scafold . *在Scafold body 中添加 TabBar。
Sample Code:示例代码:

    class _SwapOfferPageState extends State<SwapOfferPage> with SingleTickerProviderStateMixin{
  TabController _tabController;

  @override
  void initState() {
    _tabController = new TabController(length: 2, vsync: this);
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Swap or Offer shift"),
        centerTitle: true,
      ),
      body: Column(      // Column
        children: <Widget>[
          Container(
            color: Color(0xffF5F5F5),        // Tab Bar color change
            child: TabBar(           // TabBar
              controller: _tabController,
              labelColor: const Color(0xff959595),
              indicatorWeight: 2,
              indicatorColor: Color(0xff4961F6),
              tabs: <Widget>[
                Tab(
                  text: "SWAP MY SHIFT",
                ),
                Tab(
                  text: "OFFER MY SHIFT",
                ),
              ],
            ),
          ),
          Expanded(
            flex: 3,
            child: TabBarView(         // Tab Bar View
              physics: BouncingScrollPhysics(),
              controller: _tabController,
              children: <Widget>[
                Text('Tab1'),
                Text('Tab2'),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

For anyone who is still looking for How to separate a TabBar from Appbar , please check the below code-对于仍在寻找如何将TabBarAppbar分开的Appbar ,请查看以下代码-

appBar: new AppBar(
    title: new Text("some title"),
    body: new Column(
        children: [
            /// this is will not colored with theme data
            new TabBar(...),
            Expanded(
                new TabBarView(...)
            )
        ]
    )
)

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'),
                    ],
                  ),
                ),

或者您甚至可以简单地将 TabBar 包装在 Container 小部件中,然后应用您想要的“颜色”。

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

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