如何禁用手机后按功能 [英] how to disable phone back press in flutter

查看:70
本文介绍了如何禁用手机后按功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Flutter开发的新手。有人可以分享我如何在 flutter 中禁用后按吗?

I am new to Flutter development. Can anyone share me how to disable the back press in flutter?

在Android中,我们可以使用 onbackpressed 方法。

In Android, we can use onbackpressed method.

@Override
public void onBackPressed() {
  // super.onBackPressed(); commented this line in order to disable back press
  //Write your code here
  Toast.makeText(getApplicationContext(), "Back press disabled!", Toast.LENGTH_SHORT).show();
}

flutter

推荐答案

将小部件包装在 WillPopScope 中并返回 onWillPop 属性中错误的未来

Wrap your widget inside WillPopScope and return a false Future in the onWillPop property

    @override
    Widget build(BuildContext context) {
      return WillPopScope(
        onWillPop: () => Future.value(false),
        child: Scaffold(
          appBar: AppBar(
            title: const Text("Home Page"),
          ),
          body: Center(
            child: const Text("Home Page"),
          ),
        ),
      );
    }

请参阅此文档: https://api.flutter.dev/flutter/widgets/WillPopScope-class.html

这篇关于如何禁用手机后按功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆