Flutter showDialog拦截返回键

showDialog(
      context: context,
      barrierDismissible: false,//点击外部遮罩区域是否可以关闭dialog
      builder: (context) {
        return WillPopScope(
          onWillPop: () async => false,//关键代码
          child: Dialog(
            backgroundColor: Colors.transparent,
            insetPadding: EdgeInsets.zero,
            child: child,
          ),
        );
      },
    );

在Dialog的外面再嵌套一层WillPopScope,并将onWillPop设为false即可。

THE END
喜欢就支持一下吧
点赞6赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容