Flutter:点击时 BottomNavigationBar 上的选定图标不会改变

Flutter: selected Icon on BottomNavigationBar doesn't change when tap

我从事 flutter 工作,我制作了一个有 5 个标签的应用程序,使用 BottomNavigationBar,它改变了当前显示的内容。 当我点击一个选项卡时,内容会更新为新内容,但选项卡图标不会改变。

我尝试更改 BottomNavigationBarType,但没有任何改变...

这是基本页面小部件:

class Home extends StatefulWidget {
  Home({Key key}) : super(key: key);

  @override
  _Home createState() => _Home();
}

class _Home extends State<Home> {
  int _selectedIndex = 0;

  static List<Widget> _widgetOptions = <Widget>[
    HomePage(),
    CreateTrain(),
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }
  @override
  Widget build(BuildContext context) {
    print(_selectedIndex);
    return Scaffold(
      body: _widgetOptions.elementAt(_selectedIndex),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: 0, // this will be set when a new tab is tapped
        items: [
          BottomNavigationBarItem(
            icon: new Icon(Icons.home),
            title: new Text('Home'),
          ),
          BottomNavigationBarItem(
            icon: new Icon(Icons.assignment),
            title: new Text('Training'),
          ),
          BottomNavigationBarItem(
            icon: new Icon(Icons.play_arrow),
            title: new Text('start'),
          ),
          BottomNavigationBarItem(
            icon: new Icon(Icons.insert_chart),
            title: new Text('Stats'),
          ),
          BottomNavigationBarItem(
              icon: Icon(Icons.person), title: Text('Profile'))
        ],
        selectedFontSize: 12,
        unselectedFontSize: 12,
        selectedItemColor: Colors.amber[800],
        unselectedItemColor: Colors.grey[500],
        showUnselectedLabels: true,
        type: BottomNavigationBarType.fixed,
        onTap: _onItemTapped,
      ),
    );
  }
}

这是主页小部件:

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: true,
        title: const Text(
          'Home',
          style: TextStyle(
            color: Colors.black,
            fontSize: 30,
          ),
        ),
        backgroundColor: _bgColor,
      ),
      body: Text('data'),
    );
  }
}

感谢您的帮助。

在你的代码中

  currentIndex: 0, // this will be set when a new tab is tapped

这应该是

  currentIndex: _selectedIndex, // this will be set when a new tab is tapped