我是Flutter开发的新手。谁能和我分享如何禁用后按功能flutter?
flutter
在Android中,我们可以使用onbackpressed方法。
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
WillPopScope
Future
onWillPop
@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