当我按下图标时,我正在寻找以编程方式打开和关闭抽屉的方法。我正在尝试使用键,但是运气不佳。我找到了这篇文章-Flutter: How to open Drawer programmatically,但是出现问题。
我有一列图标(请参见屏幕截图),我将其包含在一个文件中,而我的抽屉位于另一个文件中,这两个图标显然都包含在第三个文件中(这是全屏Google地图)。我正在考虑使用全局密钥,但不确定将所有内容放在何处。
在我的地图页面中]
class _TheMapState extends State<TheMap> with WidgetsBindingObserver { GlobalKey<ScaffoldState> _drawerKey = GlobalKey(); ... ...
当我尝试在抽屉页面中添加密钥时,它无法识别它,因此我将GlobalKey行添加到了抽屉页面中。我也必须在我的图标选项页面上执行此操作,否则由于错误,该应用程序将无法构建。这样做时没有错误,但是单击按钮时没有任何反应。
这里是我将钥匙添加到抽屉的位置。
Widget mapDrawer() { return Drawer( key: _drawerKey, ... ...
这是我的图标代码,在单独的文件中。
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', ), ), ), ), ); }
我将不胜感激。
当我按下图标时,我正在寻找以编程方式打开和关闭抽屉的方法。我正在尝试使用键,但是运气不佳。我找到了这篇文章-Flutter:How to open Drawer ...
将密钥分配给Scaffold
,而不是Drawer