在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('点击按钮返回'),
        ),
      ),
    );
  }
}
相关推荐
kyriewen1 小时前
产品经理把PRD写成“天书”,我用AI半小时重写了一遍,他当场愣住
前端·ai编程·cursor
humcomm2 小时前
元框架的工作原理详解
前端·前端框架
canonical_entropy2 小时前
Attractor Before Harness: AI 大规模开发的方法论
前端·aigc·ai编程
zhangxingchao2 小时前
多 Agent 架构到底怎么选?从 Claude Agent Teams、Cognition/Devin 到工程落地原则
前端·人工智能·后端
IT_陈寒2 小时前
SpringBoot那个自动配置的坑,害我排查到凌晨三点
前端·人工智能·后端
Honor丶Onlyou2 小时前
VS Code 右键菜单修复记录
前端
卡卡军2 小时前
agmd 1.0 重磅升级——Rust 重写,性能起飞
javascript·rust
PILIPALAPENG3 小时前
Python 语法速成指南:前端开发者视角(JS 类比版)
前端·人工智能·python
Larcher3 小时前
🔥 告别抓瞎:用 Claude Code (cc) 优雅接手与维护已有项目
javascript·机器学习·前端框架
JYeontu3 小时前
轮播图不够惊艳?试下这个立体卡片轮播图
前端·javascript·css