flutter - Flutter,如何将开关盒用于底部导航栏?
问题描述
我是flutter的初学者,我想使用switch case来路由到另一个页面,我确实尝试过但它不起作用,谁能告诉我我做错了什么???
希望有人可以解决我的问题 希望有人可以解决我的问题 希望有人可以解决我的问题 希望有人可以解决我的问题 希望有人可以解决我的问题 希望有人可以解决我的问题
class BottomNavigationEzyMember extends StatefulWidget {
const BottomNavigationEzyMember({Key key}) : super(key: key);
@override
_BottomNavigationEzyMemberState createState() =>
_BottomNavigationEzyMemberState();
}
class _BottomNavigationEzyMemberState extends State<BottomNavigationEzyMember> {
int _selectedIndex = 0;
var bottomTextStyle =
GoogleFonts.inter(fontSize: 12, fontWeight: FontWeight.w500);
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
@override
Widget build(BuildContext context) {
(index) {
switch (_selectedIndex) {
case 0:
Navigator.of(context).pushNamed('/homepage');
break;
case 1:
Navigator.of(context).pushNamed('/cardpage');
break;
}
};
return Container(
height: 64,
decoration: BoxDecoration(
color: mFillColor,
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.3),
spreadRadius: 2,
blurRadius: 15,
offset: const Offset(0, 5))
],
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(24),
topRight: Radius.circular(24),
),
),
child: BottomNavigationBar(
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: _selectedIndex == 0
? SvgPicture.asset('assets/icons/home_colored.svg')
: SvgPicture.asset('assets/icons/home.svg'),
title: Text('Home', style: bottomTextStyle),
),
BottomNavigationBarItem(
icon: _selectedIndex == 1
? SvgPicture.asset('assets/icons/order_colored.svg')
: SvgPicture.asset('assets/icons/order.svg'),
title: Text(
'My Card',
style: bottomTextStyle,
),
),
BottomNavigationBarItem(
icon: _selectedIndex == 2
? SvgPicture.asset('assets/icons/watch_colored.svg')
: SvgPicture.asset('assets/icons/watch.svg'),
title: Text(
'Watch List',
style: bottomTextStyle,
),
),
BottomNavigationBarItem(
icon: _selectedIndex == 3
? SvgPicture.asset('assets/icons/account_colored.svg')
: SvgPicture.asset('assets/icons/account.svg'),
title: Text(
'Account',
style: bottomTextStyle,
),
),
],
currentIndex: _selectedIndex,
selectedItemColor: mBlueColor,
unselectedItemColor: mSubtitleColor,
onTap: _onItemTapped,
backgroundColor: Colors.transparent,
type: BottomNavigationBarType.fixed,
selectedFontSize: 12,
showUnselectedLabels: true,
elevation: 0,
),
);
}
}
解决方案
从下面删除 switch 语句buildContext
;您将在_onItemTapped
.
void _onItemTapped(int index) {
setState(() {
print("working");
_selectedIndex = index;
switch (_selectedIndex) {
case 0:
Navigator.of(context).pushNamed('/homepage');
break;
case 1:
Navigator.of(context).pushNamed('/cardpage');
break;
}
});
}
推荐阅读
- symfony - Symfony 表单构建器 - 如何将表单标题放在 h2 选择器中?
- oracle-data-integrator - 如果我们有 PL/SQL 语言,为什么要使用 odi
- r - geom_line() 在使用分组数据的 ggplot 中不起作用
- angular - 在另一个提供者中使用一个提供者,反之亦然
- c# - c# 布尔序列化问题
- druid - 如何使用 Tranquility 核心 API 向 druid 发送数据?
- hive - 多表插入 Hive 中的单个表
- mysql - 在mysql中的最后一个连接上从同一个表中获取信息
- jquery - 为单独的图片srcset和img src分配属性
- php - 使用超过 10.000 个数据使数据表加载更快