如何禁用手机后退功能
问题内容:
我是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();
}
在flutter
怎么可能呢?
问题答案:
将小部件包装在其中,WillPopScope
并Future
在onWillPop
属性中返回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"),
),
),
);
}
请参阅此文档:https : //api.flutter.dev/flutter/widgets/WillPopScope-
class.html