如何通过点击BottomNavigationBarItem以编程方式打开抽屉?

Mic*_*l D 5 dart flutter

我正在制作一个Flutter应用程序,我需要能够通过点击BottomNavigationBarItem来打开抽屉。有什么办法吗?

UX设计人员将抽屉菜单图标放在底部导航栏中的索引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),
        ],
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

而不是显示抽屉,我得到以下错误消息:

在不包含Scaffold的上下文中调用Scaffold.of()。

Tri*_*Pct 8

这是因为在onTabTapped您使用的上下文中不包含您创建的支架。

您实例化了脚手架,build但是在onTabTapped当前上下文(_HomeStatecontext)中寻找父脚手架。

您可以Builder在脚手架内部使用以获得正确的上下文,也可以GlobalKey在脚手架上使用。

有关更多详细信息,请参见此答案

编辑: 在您的情况下,GlobalKey更容易实现。

您可以执行以下操作:

class _HomeState extends State<Home> {
  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>(); // ADD THIS LINE
  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) {
        _scaffoldKey.currentState.openDrawer(); // CHANGE THIS LINE
      }
      else {
        _currentIndex = index;
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey, // ADD THIS LINE
      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),
        ],
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)