我正在制作一个Ffltter应用程序,我需要能够通过点击BottomNavigationBarItem来打开抽屉.有什么办法可以做到这一点吗?
UX Designer的家伙把抽屉菜单图标放在底部导航栏的索引0处.我试图在Flutter 文档中找到答案,但我没有找到任何相关的东西.我实际上找到了一种以编程方式打开它的方法(如下所示),但在我的例子中不是这样工作的.
class _HomeState extends State<Home> {
int _currentIndex = 1; // 0 = menu
final List<Widget> _children = [
PlaceholderWidget(Colors.deepPurple),
PlaceholderWidget(Colors.white),
DiagnosisWidget(),
FindUsWidget(),
];
_navItem(String text, IconData icon) {
return BottomNavigationBarItem(
/* Building Bottom nav item */
);
}
void onTabTapped(int index) {
setState(() {
if(index == 0) {
Scaffold.of(context).openDrawer(); // This is what I've tried
}
else {
_currentIndex = index;
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
drawer: Drawer(
child: MyDrawer(),
),
body: _children[_currentIndex],
bottomNavigationBar: BottomNavigationBar(
onTap: onTabTapped,
currentIndex: _currentIndex,
type: BottomNavigationBarType.fixed, // 4+ items in the bar
items: [
_navItem('MENU', Icons.menu),
_navItem('HOME', Icons.home),
_navItem('DIAGNOSIS', Icons.person),
_navItem('FIND US', Icons.location_on),
],
),
);
}
}
我没有显示抽屉,而是收到以下错误消息:
使用不包含Scaffold的上下文调用了Scaffold.of().