Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何在Flatter中使用键以编程方式打开和关闭抽屉?_Flutter_Dart_Drawer - Fatal编程技术网

Flutter 如何在Flatter中使用键以编程方式打开和关闭抽屉?

Flutter 如何在Flatter中使用键以编程方式打开和关闭抽屉?,flutter,dart,drawer,Flutter,Dart,Drawer,当我按下图标时,我希望以编程方式打开和关闭抽屉。我试着用钥匙,但运气不太好。我找到了这篇文章-,但我有问题 我有一列图标(见截图),我把它们放在一个文件中,我的抽屉放在另一个文件中,这两个图标显然都放在第三个文件中(这是一个全屏谷歌地图)。我正在考虑使用全局密钥,但我不确定将所有内容放在何处 在我的地图页面中,我有 class _TheMapState extends State<TheMap> with WidgetsBindingObserver { GlobalKey&l

当我按下图标时,我希望以编程方式打开和关闭抽屉。我试着用钥匙,但运气不太好。我找到了这篇文章-,但我有问题

我有一列图标(见截图),我把它们放在一个文件中,我的抽屉放在另一个文件中,这两个图标显然都放在第三个文件中(这是一个全屏谷歌地图)。我正在考虑使用全局密钥,但我不确定将所有内容放在何处

在我的地图页面中,我有

class _TheMapState extends State<TheMap> with WidgetsBindingObserver {
  GlobalKey<ScaffoldState> _drawerKey = GlobalKey();
  ...
  ...
这是我的图标代码,它在一个单独的文件中

Widget _buildShowHideDrawerIcon() {
    return Container(
      child: Opacity(
        opacity: 0.60,
        child: Container(
          padding: EdgeInsets.all(7.0),
          decoration: BoxDecoration(
            color: Colors.white,
            //border: Border.all(color: Colors.black12, width: 1.0),
            boxShadow: [
              BoxShadow(
                color: Colors.black,
                offset: Offset(0.0, 0.0),
                blurRadius: 3.0,
              ),
            ],
          ),
          child: InkWell(
            onTap: () {
              print("SHOW/HIDE DRAWER ICON PRESSED");
              setState(() {
              _drawerKey.currentState.openDrawer();
              });
            },
            child: Icon(
              Icons.swap_horiz,
              color: Colors.black87,
              size: 24.0,
              semanticLabel: 'Show ,or hide sliding drawer',
            ),
          ),
        ),
      ),
    );
  }

如果您能在这方面提供帮助,我将不胜感激。

将钥匙分配给
脚手架
,而不是
抽屉

  Scaffold(
    key: _drawerKey,
    drawer: Drawer(),


您应该将钥匙分配给
脚手架
,而不是
抽屉

Scaffold(
   key: _drawerKey,
并替换该行:

_drawerKey.currentState.openDrawer(); 


谢谢-我现在有一个错误,方法“openDrawer”被调用为null。
_drawerKey.currentState.openDrawer(); 
_drawerKey.currentState?.openDrawer();