You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中禁用侧边滑动打开Drawer,仅通过按钮开启

禁用侧边滑动打开EndDrawer,仅保留按钮触发方式

要禁用通过侧边滑动打开EndDrawer的手势,只需要在你的Scaffold组件中设置endDrawerEnableOpenDragGesture: false即可(如果同时使用左侧Drawer,也可设置drawerEnableOpenDragGesture: false禁用左侧滑动)。

修改后的完整示例代码如下:

Scaffold(
  // 禁用右侧EndDrawer的滑动打开手势
  endDrawerEnableOpenDragGesture: false,
  // 若有左侧Drawer,可按需禁用其滑动手势
  // drawerEnableOpenDragGesture: false,
  endDrawer: YourEndDrawerWidget(), // 替换为你的EndDrawer组件
  appBar: AppBar(
    actions: [
      Builder(
        builder: (context){
          return IconButton(
            onPressed: (){
              Scaffold.of(context).openEndDrawer();
            },
            icon: Icon(Icons.menu, color: Colors.blue.shade700,),
          );
        },
      ),
    ],
  ),
  body: YourMainContentWidget(), // 替换为你的主内容组件
)

说明

  • endDrawerEnableOpenDragGesture属性专门控制右侧EndDrawer的滑动打开逻辑,设为false后,用户无法通过从屏幕右侧向左滑动的方式打开EndDrawer。
  • 你原有的IconButton触发openEndDrawer()的逻辑不受影响,依然可以正常打开EndDrawer。

内容的提问来源于stack exchange,提问作者Irfan Ganatra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 10:35:39