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

后端 未结 3 1067
我在风中等你
我在风中等你 2020-12-24 12:41

How to change background color of TabBar without changing the AppBar? The TabBar does not have a background property, is

3条回答
  •  情话喂你
    2020-12-24 13:34

    You can change the color of the TabBar by changing the Theme primaryColor like that:

    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:

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

提交回复
热议问题