【发布时间】:2021-11-24 04:07:29
【问题描述】:
我为退出弹出服务创建了一个WillPopScope 函数。当用户点击后退按钮时,它会显示一个退出权限对话框。
它在 Flutter 版本 1 上完美运行。现在对于 null 问题,这是一个问题。
这是我的代码:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class ExitPopUp extends StatelessWidget {
final page;
ExitPopUp(this.page);
@override
Widget build(BuildContext context) {
Future<bool?> showExitPopUp() {
return showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20.0))),
backgroundColor: Color.fromRGBO(0, 68, 69, .5),
title: Text("Confirm", style: TextStyle(color: Colors.white)),
content: Text("Do you want to Exit ?",
style: TextStyle(color: Colors.white)),
actions: <Widget>[
ElevatedButton(
child: Text(
"No",
style: TextStyle(color: Colors.yellow[100]),
),
onPressed: () {
Navigator.pop(context, false);
}),
ElevatedButton(
child: Text("Yes",
style: TextStyle(color: Colors.yellow[100])),
onPressed: () {
SystemNavigator.pop();
})
],
);
});
}
return WillPopScope(child: page, onWillPop: showExitPopUp);
}
}
但这是关于 onWillPop 的一个问题。
我该如何解决这个问题?
【问题讨论】:
-
您说它在可空更改之前有效。是什么让您在
Future<bool?> showExitPopUp() {行中添加了?? -
您可以关注此 SO 帖子。 stackoverflow.com/questions/68297278/…
-
我这次添加 Future
标签: flutter popup exit nullable