我是 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
中,并在 onWillPop
属性中返回一个错误的 Future
@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
关于dart - 如何在 flutter 中禁用手机后退,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51834053/