gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 07:18:06 57 4
gpt4 key购买 nike

我在 flutter 上工作,我制作了一个有 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,
),
);
}
}

这是主页小部件:

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

关于Flutter:点击时 BottomNavigationBar 上的选定图标不会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56687052/

57 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com