Флаттер: выбранный значок на нижней панели навигации не изменяется при нажатии - PullRequest
0 голосов
/ 20 июня 2019

Я работаю над флаттером и создаю приложение с 5 вкладками, используя BottomNavigationBar, которые изменяют отображаемый в данный момент контент.Когда я нажимаю на вкладку, содержимое обновляется до нового содержимого, но значок вкладок не меняется.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,
      ),
    );
  }
}

Вот HomePageВиджет:

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 Ответ

1 голос
/ 20 июня 2019

В вашем коде

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

Это должно быть

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