颤动:轻按

时间:2019-06-20 13:27:06

标签: flutter selecteditem bottomnavigationview

我使用的是Flutter,我使用BottomNavigationBar制作了5个标签的应用,这些标签更改了当前显示的内容。 当我点击选项卡时,内容会更新为新内容,但选项卡图标不会更改。 Home page screen Train page screen

我试图更改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'),
    );
  }
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

在您的代码中

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

应该是

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