如何更改TabBar的背景颜色而不更改flutter中的AppBar?

ama*_*ter 12 appbar tabbar dart flutter

如何更改TabBar的背景颜色而不更改flutter中的AppBar?TabBar没有BG proprety,有解决方法吗?

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)

  • 它改变了整个 AppBar 不仅是 TabBar,我只需要改变 TabBar (11认同)

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)

  • @最白目请将上面的“tabBar”字段包装在“Material”小部件中,并将类型设置为“MaterialType.transparency” (4认同)

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)

  • 完美的答案。应标记为正确答案。 (5认同)
  • 最简单的答案,谢谢 (3认同)
  • 应将其标记为正确答案! (3认同)
  • 这应该被标记为正确答案。这是整个线程中最简单、最直接的答案,不需要您创建自定义类。 (2认同)
  • 这么简单又简短,谢谢兄弟!!! (2认同)

Sat*_*was 5

简单、简洁。

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)