flutter - 即使在对 4 个元素使用 setState 后,设置状态在底部导航栏项目中也不起作用
问题描述
当我在底部导航栏中使用 4 个项目时,即使 _currentIndex 像往常一样更改,也只有菜单显示为选定项目(索引 0)。现在该怎么办,是不是因为在类别屏幕中使用了构造函数?
@override
Widget build(BuildContext context) {
User user = FirebaseAuth.instance.currentUser;
int _currentIndex = 0;
void onTabTapped(int index) {
setState(() {
_currentIndex = index;
});
}
final List<Widget> _children = [
CategoryScreen(email: user.email),
TabBarOrder(),
PayInScreen(),
ProfileScreen(),
];
return Scaffold(
body: _children[_currentIndex],
bottomNavigationBar: BottomNavigationBar(
showSelectedLabels: true,
onTap: onTabTapped,
currentIndex: _currentIndex,
type: BottomNavigationBarType.fixed,
iconSize: 0,
items: [
BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Menu'),
BottomNavigationBarItem(icon: Icon(Icons.mail), label: 'Order'),
BottomNavigationBarItem(
icon: Icon(Icons.play_circle_filled), label: 'Pay-in'),
BottomNavigationBarItem(
icon: Icon(Icons.play_circle_filled), label: 'Profile'),
],
),
);
}
请帮忙
解决方案
int _currentIndex = 0;
void onTabTapped(int index) {
setState(() {
_currentIndex = index;
});
}
@override
Widget build(BuildContext context) {
User user = FirebaseAuth.instance.currentUser;
final List<Widget> _children = [
CategoryScreen(email: user.email),
TabBarOrder(),
PayInScreen(),
ProfileScreen(),
];
return Scaffold(
body: _children.elementAt(_currentIndex),
bottomNavigationBar: BottomNavigationBar(
showSelectedLabels: true,
onTap: onTabTapped,
currentIndex: _currentIndex,
type: BottomNavigationBarType.fixed,
iconSize: 0,
items: [
BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Menu'),
BottomNavigationBarItem(icon: Icon(Icons.mail), label: 'Order'),
BottomNavigationBarItem(
icon: Icon(Icons.play_circle_filled), label: 'Pay-in'),
BottomNavigationBarItem(
icon: Icon(Icons.play_circle_filled), label: 'Profile'),
],
),
);
}
推荐阅读
- python - 匹配两个数据框中的某些列以获得相似度分数
- google-cloud-platform - 谷歌 GCP 云运行 redis 客户端失去与实例的连接
- numpy - 如何获取张量中的特定元素(pytorch)
- javascript - 如何使用省略号在 JSX 中显示最后 6 个字符?
- html - 如何增加一个人的高度与行中的其他人对齐?
- asp.net - 使用会话变量查询 SQL Server
- node.js - Nodemailer错误:连接ECONNREFUSED 127.0.0.1:587(errno:-61),但我指定端口:465,安全:true
- visual-studio-2017 - VSIX 项目 - 属性工具窗口扩展
- sql - 为什么我的 BigQuery 保留与 Firebase 不一致?
- sql - 在 SQL 中从 Count() 中选择 MAX 值时出错