在Flutter中,禁止侧滑的方法

在Flutter中,如果你想禁用侧滑返回功能,你可以使用WillPopScope小部件,并在onWillPop回调中返回false来阻止用户通过侧滑返回到上一个页面。

dart 复制代码
class DisableSwipePop extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async => false,
      child: Scaffold(
        appBar: AppBar(
          title: Text('禁用侧滑返回'),
        ),
        body: Center(
          child: Text('点击按钮返回'),
        ),
      ),
    );
  }
}

但是 WillPopScope方法已经过时,现在PopScope 代替具体使用方法

dart 复制代码
class DisableSwipePop extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return PopScope(
      canPop: false,
      child: Scaffold(
        appBar: AppBar(
          title: Text('禁用侧滑返回'),
        ),
        body: Center(
          child: Text('点击按钮返回'),
        ),
      ),
    );
  }
}
相关推荐
蒜香拿铁3 分钟前
Angular【起步】
前端·javascript·angular.js
护国神蛙3 分钟前
HTTP 重定向踩坑实录:307、301、308 问题排查全指南
前端·网络协议
初心丨哈士奇4 分钟前
前端Vibe Coding探索:Cursor+MCP打造沉浸式开发流(使用MCP与Cursor Rules让Vibe Coding更快速与精准)
前端·人工智能
Hilaku13 分钟前
前端开发,真的有必要学Docker吗?
前端·javascript·docker
安迪西嵌入式15 分钟前
数据平滑处理算法03——中心移动平均
java·前端·算法
掘金安东尼19 分钟前
🧭 前端周刊第428期(2025年10月28日–11月3日)
前端·github
一枚前端小能手43 分钟前
🗂️ 文件系统API深度解析 - 让Web应用拥有本地文件操作能力的现代API实战指南
前端·javascript
陈随易1 小时前
编程语言MoonBit:在前端开发中的妙用
前端·后端·程序员
一枚前端小能手1 小时前
「周更第10期」实用JS库推荐:VueUse
前端·javascript·vue.js
前端摸鱼匠1 小时前
Vue 3 事件修饰符全解析:从 .stop 到 .passive,彻底掌握前端交互的艺术
前端·vue.js·node.js·vue·交互