【发布时间】:2020-01-09 22:58:30
【问题描述】:
我尝试了 WillPopScope,但它根本不起作用。然后我尝试了 back_button_interceptor,它确实拦截了后退按钮。但是 showDialog 会引发错误。请帮忙?
class LoginState extends State<Login> {
static String id = "Please scan ID card";
static String user;
int sc=0;
@override
initState() {
super.initState(); BackButtonInterceptor.add(myInterceptor);
}
@override
void dispose() {
BackButtonInterceptor.remove(myInterceptor);
super.dispose();
}
bool myInterceptor(bool stopDefaultButtonEvent) {
print("BACK BUTTON!"); // Do some stuff.
showDialog(
context: context,
child: new AlertDialog(
title: new Text('Are you sure?'),
content: new Text('App will exit'),
actions: <Widget>[
new FlatButton(
onPressed: () => true,
child: new Text('No'),
),
new FlatButton(
onPressed: () => false,
child: new Text('Yes'),
),
],
),
);
return true;
}
@override
Widget build(BuildContext context) {---}
}
【问题讨论】: