在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('点击按钮返回'),
        ),
      ),
    );
  }
}
相关推荐
@大迁世界10 小时前
Firefox 本地模式:localhost 再见了 ?
前端·架构
好的佩奇11 小时前
Dart 之类修饰符
android·flutter·dart
米米商聊12 小时前
同名附件为什么被 Map 覆盖?重复键与一对多索引
前端·javascript
echome88813 小时前
Python 异常处理:try、else、finally 的正确搭配
开发语言·前端·python·异常处理·编程技巧
时间的情敌13 小时前
OpenLayers 实战|Vue3 + OpenLayers 完整工程案例(加载天地图、要素、弹窗、绘制、WFS)
前端·openlayers
陈随易13 小时前
下载神器!Motrix 停更三年后重写回归
前端·后端·程序员
前端的日常14 小时前
一个人+AI做情侣食谱小程序,0 费用的服务器
前端·后端
计算机魔术师14 小时前
谷歌搜索广告踩刹车,AI广告成为救命稻草
前端
总要学点什么14 小时前
第81篇:数据可视化-PM2.5分布——让圆点的胖瘦替每座城市报出浓度
前端·信息可视化·echarts·cesium·空间分析
IT_陈寒14 小时前
Vue的v-for里用了个临时变量,页面直接挂了
前端·人工智能·后端