如何在颤振中禁用手机回按

kar*_*i j 8 dart flutter

我是 Flutter 开发的新手。谁能分享我如何禁用后按flutter

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

@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();
}
Run Code Online (Sandbox Code Playgroud)

flutter如何可能呢?

die*_*per 12

将您的小部件包裹在里面WillPopScopeFutureonWillPop属性中返回 false

    @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"),
          ),
        ),
      );
    }
Run Code Online (Sandbox Code Playgroud)

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

  • 有没有一种简单的方法来禁用整个应用程序?或者我必须用 WillPopScope 包裹每个屏幕 (2认同)