[Flutter]BottomNavigationBarItemのラベルの背景色を設定するには?

Flutter

どうも、ちょげ(@chogetarou)です。

BottomNavigationBarItemのラベルの背景色を設定する方法を紹介します。

スポンサーリンク

方法

インターフェース, インターネット, プログラム, ブラウザ, Www

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の引数を使います。

コメント

タイトルとURLをコピーしました