扑BottomAppBar选择的颜色

时间:2019-10-31 10:37:42

标签: flutter android-bottomappbar

因此,我目前在底部导航栏中有以下内容

 bottomNavigationBar: BottomAppBar(
        shape: CircularNotchedRectangle(),
        child: Container(
          decoration:
          new BoxDecoration(color: new Color(0xFFFF0000)),
          height: 75,
          child: Row(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              IconButton(
                iconSize: 30.0,
                padding: EdgeInsets.only(left: 28.0),
                icon: Icon(Icons.home),
                onPressed: () {
                  setState(() {
                     onTabTapped(0);
                  });
                },
              ),
              IconButton(
                iconSize: 30.0,
                //padding: EdgeInsets.only(left: 28.0),
                icon: Icon(Icons.view_headline),
                onPressed: () {
                  setState(() {
                    onTabTapped(2);
                  });
                },
              ),
          /*    IconButton(
                iconSize: 30.0,
                padding: EdgeInsets.only(right: 28.0),
                icon: Icon(Icons.search),
                onPressed: () {
                  setState(() {
                    onTabTapped(1);
                  });
                },
              ),
              IconButton(
                iconSize: 30.0,
                padding: EdgeInsets.only(left: 28.0),
                icon: Icon(Icons.notifications),
                onPressed: () {
                  setState(() {
                    onTabTapped(2);
                  });
                },
              ),*/
              IconButton(
                iconSize: 30.0,
                padding: EdgeInsets.only(right: 28.0),
                icon: Icon(Icons.info_outline),
                onPressed: () {
                  setState(() {
                    onTabTapped(1);
                  });
                },
              )
            ],
          ),
        ),
      ),

但是,我注意到它不会使活动页面或选择的颜色不同。例如,我想要的是因为首页是首页,因此图标应为白色而不是黑色。

我想知道我需要添加什么?基本上,如果选择的颜色应该是白色。

我发现的解决方案是将代码更改为以下内容:

bottomNavigationBar: new Theme(
      data: Theme.of(context).copyWith(
    // sets the background color of the `BottomNavigationBar`
    canvasColor: Colors.red,
    // sets the active color of the `BottomNavigationBar` if `Brightness` is light
    primaryColor: Colors.black,
    textTheme: Theme
        .of(context)
        .textTheme
        .copyWith(caption: new TextStyle(color: Colors.black))), // sets the inactive color of the `BottomNavigationBar`
          child: BottomNavigationBar(
              items: const <BottomNavigationBarItem>[
                BottomNavigationBarItem(
                  icon: Icon(Icons.home),
                  title: Text('Home'),
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.view_headline),
                  title: Text('News'),
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.more),
                  title: Text('More'),
                ),
              ],
              currentIndex: _selectedIndex,
              selectedItemColor: Colors.white,
              onTap: onTabTapped,
      ),
    ), 

1 个答案:

答案 0 :(得分:1)

老问题,但没有答案,所以也许这会帮助指导某人

处理此问题的一种方法是使用三元检查索引。基于 OP 的示例:

引用您班级中的 int _selectedIndex

int _selectedIndex = 0;

您的 IconButton 中的 BottomAppBar(我在这里删除了 setState 调用):

IconButton(
 iconSize: 30.0,
 icon: Icon(Icons.view_headline, color: _selectedIndex == 2 ? Colors.white : Colors.black),
 onPressed: () {
  onTabTapped(2);
 },
),

您的 OnTabTapped 方法:

  void onTabTapped(int index) {
    setState(() {
      _selectedIndex = index;
      ///call your PageController.jumpToPage(index) here too, if needed
    });
  }

Now when you tap on an Icon, it will change the _selectedIndex variable within setState, which will then rebuild your Icons and choose their color appropriately (the selected Icon will be white and the rest black ).