在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('点击按钮返回'),
        ),
      ),
    );
  }
}
相关推荐
2301_773643624 分钟前
ceph镜像
前端·javascript·ceph
程序员黑豆25 分钟前
AI全栈开发之Java:什么是JDK
前端·后端·ai编程
To_OC26 分钟前
万字解析《JS语言精粹》之第四章:函数15大核心精髓(JS灵魂核心)
前端·javascript·代码规范
宋拾壹29 分钟前
同时添加多个类目
android·开发语言·javascript
●VON31 分钟前
AtomGit Flutter鸿蒙客户端:用户资料
flutter·华为·架构·跨平台·harmonyos·鸿蒙
悟空瞎说31 分钟前
Flutter 三大主流本地存储全解:SharedPreferences、Hive、SQLite 实战指南
flutter
IT知识分享34 分钟前
从零开发在线简繁转换工具:OpenCC 实战、避坑经验与方案选型
javascript·python
mqcode36 分钟前
Vue3 + Element Plus + Vite 企业级后台框架搭建全流程
前端
SL-staff38 分钟前
Web 白板技术架构深度解析:从渲染到协作的选型哲学
前端·架构
悟空瞎说39 分钟前
Flutter Isolate 与 compute 全方位实战指南:后台任务优化,保障 UI 60 帧流畅
flutter