どうも、ちょげ(@chogetarou)です。
BottomNavigationBarItemのラベルの背景色を設定する方法を紹介します。
方法
BottomNavigationBarItemのラベルの背景色を設定するには、BottomNavigationBarの次の2つの引数を使います。
- selectedLabelStyle : 選択中のラベルのスタイルを設定
- unselectedLabelStyle:非選択中のラベルのスタイルを設定
まず、これらの引数にTextStyleを指定します。
そして、TextStyleの引数「backgroundColor」に背景色を指定します。
BottomNavigationBar(
selectedLabelStyle: TextStyle(
backgroundColor: /*選択中のラベルの背景色*/,
),
unselectedLabelStyle: TextStyle(
backgroundColor: /*非選択中のラベルの背景色*/,
),
items: [
・・・
],
currentIndex: _currentIndex,
onTap: _onTap,
),
これでBottomNavigationBarItemの背景色が設定されます。
使用例
以下は、使用例です。
@override
Widget build(BuildContext context) {
return Scaffold(
body: _pages[_currentIndex],
bottomNavigationBar: BottomNavigationBar(
selectedLabelStyle: TextStyle(
backgroundColor: Colors.yellow,
),
unselectedLabelStyle: TextStyle(
backgroundColor: Colors.orange,
),
items: [
BottomNavigationBarItem(
icon: Icon(Icons.settings),
label: 'Settings',
),
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.favorite),
label: 'Favorite',
),
],
currentIndex: _currentIndex,
onTap: _onTap,
),
);
}
まとめ
BottomNavigationBarItemのラベルの背景色を設定するには、 BottomNavigationBarの引数を使います。
[Flutter]BottomNavigationBarを固定表示するには?
BottomNavigationBarを固定表示する方法を紹介します。
[Flutter]BottomNavigationBarが4つ以上のタブで非表示になるのを防ぐには?
BottomNavigationBarは、引数「items」に4つ以上のアイテムを指定すると非表示になります。これを防ぐにはどうしたらいいのでしょうか?
[Flutter]IndexedStackを使ってBottomNavigationBarを実装するには?
IndexedStackを使って、BottomNavigationBarを実装する方法を紹介します。
コメント