Flutter showDialog给出了颤振中的错误
我试过Willposcope,但根本不起作用。然后我尝试了back_button_拦截器,它确实截获了backbutton。但是showDialog会抛出错误。请帮忙Flutter showDialog给出了颤振中的错误,flutter,dart,Flutter,Dart,我试过Willposcope,但根本不起作用。然后我尝试了back_button_拦截器,它确实截获了backbutton。但是showDialog会抛出错误。请帮忙 class LoginState extends State<Login> { static String id = "Please scan ID card"; static String user; int sc=0; @override initState() { super.ini
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) {---}
}
类LoginState扩展状态{
静态字符串id=“请扫描身份证”;
静态字符串用户;
int-sc=0;
@凌驾
initState(){
super.initState();BackButtonInterceptor.add(myInterceptor);
}
@凌驾
无效处置(){
BackButtonInterceptor.remove(myInterceptor);
super.dispose();
}
布尔myInterceptor(布尔stopDefaultButtonEvent){
打印(“后退按钮!”;//做一些事情。
显示对话框(
上下文:上下文,
子:新建警报对话框(
标题:新文本(“你确定吗?”),
内容:新文本(“应用程序将退出”),
行动:[
新扁平按钮(
onPressed:()=>正确,
子项:新文本(“否”),
),
新扁平按钮(
onPressed:()=>false,
子项:新文本(“是”),
),
],
),
);
返回true;
}
@凌驾
小部件生成(BuildContext上下文){---}
}
如果您试图阻止在区域/对话框外点击时关闭对话框,请在showDialog内添加障碍Ismissible:false,
然后,您可以使用以下示例手动关闭该对话框,将其放置在AlertDialog的操作中
FlatButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text(
'Cancel',
style: TextStyle(
color: Colors.black,
),
),
),
如果您计划在关闭对话框之前执行其他操作,请将()=>Navigator.of(context).pop()更改为()=>(){//do stuff here,然后在此函数末尾添加Navigator.of(context).pop()。我正在尝试显示对话框。当我的拦截器执行时,我只是得到了一堆错误。