jde*_*vp2 10 navigator flutter
我正在将代码从 更改为WillPopScope来PopScope捕获后退按钮的按下情况。我需要在返回上一屏幕之前显示一个对话框。因此,我需要发出Navigator.pop两次,一次关闭对话框,另一次返回上一个屏幕。下面的代码可以正常工作,但是当我将其更改为“since is depreciated”时,会抛出WillPopScope错误 。'!_debugLocked': is not truePopScopeWillPopScope
// return WillPopScope(\nreturn PopScope(\n canPop:true,\n onPopInvoked: (bool didPop) async{\n // onWillPop: () async {\n if (vStatus[1] && !vStatus[2]) {\n _showMaterialDialog(context, 1);\n }\n return Future.value(true);\n },\n child: Scaffold(\n \xe2\x80\xa6\xe2\x80\xa6.\n\n\nvoid _showMaterialDialog(BuildContext context, int opt) {\n showDialog(\n context: context,\n builder: (context) {\n return AlertDialog(\n title: Text(getTextTitle(opt),style:TextStyle(fontFamily: 'DancingScriptBold',color:Colors.blue, fontSize: 20.0)),\n content: Text(getTextContent(opt),\n style: const TextStyle(\n fontFamily: 'DancingScriptBold', color:Colors.blue,fontSize: 20.0),\n ),\n actions: <Widget>[\n TextButton(\n onPressed: () {\n Utils.saveVideo().then((value) async {\n if(value!) {\n vStatus[2] = true;\n }\n });\n \n Navigator.of(context).pop(); //dismiss dialog\n Navigator.of(context).pop(); //go back to previous screen \n \n },\n child: const Text(\n style: TextStyle(\n fontFamily: 'DancingScriptBold', color:Colors.blue,fontSize: 20.0),\n 'Yes')),\n TextButton(\n onPressed: () {\n \n Navigator.of(context).pop(); //dismiss dialog\n Navigator.of(context).pop(); //go back to previous screen \n \n }\n },\n child: const Text(\n style: TextStyle(\n fontFamily: 'DancingScriptBold',color:Colors.blue, fontSize: 20.0),\n 'No'),\n )\n ],\n );\n });\n}\nRun Code Online (Sandbox Code Playgroud)\n感谢任何反馈!
\n小智 11
您可以使用下面的代码
PopScope(
canPop: false,
onPopInvoked: (bool didPop) async {
if (didPop) return;
final NavigatorState navigator = Navigator.of(context);
if (vStatus[1] && !vStatus[2]) {
await _showMaterialDialog(context, 1);
}
navigator.pop();
},
),
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6599 次 |
| 最近记录: |