【问题标题】:How to open drawer programatically on press in flutter如何在颤动中以编程方式打开抽屉
【发布时间】:2021-04-10 00:52:15
【问题描述】:

我需要在单击浮动按钮时打开应用程序抽屉,但它没有打开。请建议是否有除抽屉以外的任何其他东西我正在寻找构建过滤器部分

这是我的部分代码

 final GlobalKey<ScaffoldState> _scaffoldKey =
      new GlobalKey<ScaffoldState>(); // 
 drawer: Drawer(
        // Add a ListView to the drawer. This ensures the user can scroll
        // through the options in the drawer if there isn't enough vertical
        // space to fit everything.
        child: ListView(
          // Important: Remove any padding from the ListView.
          padding: EdgeInsets.zero,
          children: <Widget>[
            DrawerHeader(
              child: Text('Drawer Header'),
              decoration: BoxDecoration(
                color: Colors.blue,
              ),
            ),
            ListTile(
              title: Text('Item 1'),
              onTap: () {
                // Update the state of the app
                // ...
                // Then close the drawer
                Navigator.pop(context);
              },
            ),
            ListTile(
              title: Text('Item 2'),
              onTap: () {
                // Update the state of the app
                // ...
                // Then close the drawer
                Navigator.pop(context);
              },
            ),
          ],
        ),
      ),
      
      floatingActionButton: FloatingActionButton.extended(
          onPressed: () {
            // Add your onPressed code here!

            _scaffoldKey.currentState.openDrawer();
           
          },
          label: Text(
            'Filter',
            style:
                TextStyle(decoration: TextDecoration.none, color: Colors.black),
          ),
          icon: Icon(Icons.filter_alt, color: Colors.black),
          backgroundColor: Colors.white),

我遇到错误

引发了另一个异常:NoSuchMethodError: 方法 'openDrawer' 在 null 上被调用。

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您应该将 _scaffoldKeyparamater 传递给您的脚手架。

    Scaffold(key: _scaffoldKey);
    

    【讨论】:

    • 不是Drawer(key: _scaffoldKey);,而是Scaffold(key: _scaffoldKey, ...)
    • 未经测试,但如果您这么说,那一定是真的。谢谢你^^
    • 你说对了,我查了一下。
    【解决方案2】:

    其实你需要在脚手架中设置key属性。

    Scaffold(key:_scaffoldKey)
    

    然后使用与您使用的方法相同的方法。

    【讨论】:

      猜你喜欢
      • 2020-01-04
      • 2019-09-24
      • 2019-04-17
      • 2013-08-06
      • 2019-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多