gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 12:33:50 27 4
gpt4 key购买 nike

如何在不改变AppBar的情况下改变TabBar的背景颜色?TabBar 没有 background 属性,有解决方法吗?

最佳答案

你可以通过改变 Theme primaryColor 来改变 TabBar 的颜色:

return MaterialApp(
theme: ThemeData(
brightness: Brightness.light,
// add tabBarTheme
tabBarTheme: const TabBarTheme(
labelColor: Colors.pink[800],
labelStyle: TextStyle(color: Colors.pink[800]), // color for text
indicator: UnderlineTabIndicator( // color for indicator (underline)
borderSide: BorderSide(color: ConstColor.primary))),
primaryColor: Colors.pink[800], // outdated and has no effect to Tabbar
accentColor: Colors.cyan[600] // deprecated,
),
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),
],
),
),
),
);

如果您不在 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),
],
),
),
],
),
),
),
);
}
}

关于flutter - 如何在不改变 AppBar 的情况下更改 TabBar 的背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50566868/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com