Flutter 如何使警报对话框在颤振中工作
我正在使用下面的代码注册用户,有一个选项可以上传图像,如果它为空,用户将不会被注册,这是准确的工作,我面临的唯一问题是,如果图像为Flutter 如何使警报对话框在颤振中工作,flutter,dart,flutter-alertdialog,Flutter,Dart,Flutter Alertdialog,我正在使用下面的代码注册用户,有一个选项可以上传图像,如果它为空,用户将不会被注册,这是准确的工作,我面临的唯一问题是,如果图像为null它应该向用户发送警报对话框,但警报对话框根本不工作。 我应该如何实现警报对话框 if (avatarImageFile != null ) { FirebaseAuth.instance .createUserWithEmailAndPassword(
null
它应该向用户发送警报对话框,但警报对话框根本不工作。
我应该如何实现警报对话框
if (avatarImageFile != null ) {
FirebaseAuth.instance
.createUserWithEmailAndPassword(
email: emailInputController.text,
password: pwdInputController.text)
.then((currentUser) =>
Firestore.instance
.collection("users")
.document(currentUser.user.uid)
.setData({
"username": userNameInputController.text,
"email": emailInputController.text,
})
.then((result) =>
{
uploadFile(currentUser.user.uid),
print(currentUser.user.getIdToken()),
currentUser.user.sendEmailVerification(),
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (context) =>
MainScreen(
)),
(_) => false),
})
.catchError((err) => print(err)))
.catchError((err) => print(err));
}
else {
print("Please Upload the Image");
AlertDialog(
title: Text("Image Required"),
content: Text("Please upload the image"),
actions: <Widget>[
FlatButton(
child: Text("Close"),
onPressed: () {
Navigator.of(context).pop();
},
)
],
);
}
if(avatarImageFile!=null){
FirebaseAuth.instance
.createUserWithEmailAndPassword(
电子邮件:emailInputController.text,
密码:pwdInputController.text)
.然后((当前用户)=>
Firestore.instance
.收集(“用户”)
.document(currentUser.user.uid)
.setData({
“用户名”:userNameInputController.text,
“email”:emailInputController.text,
})
。然后((结果)=>
{
上载文件(currentUser.user.uid),
打印(currentUser.user.getIdToken()),
currentUser.user.sendEmailVerification(),
Navigator.pushandremove直到(
上下文
材料路线(
生成器:(上下文)=>
主屏幕(
)),
(41;=>假),
})
.catchError((err)=>打印(err)))
.catchError((err)=>打印(err));
}
否则{
打印(“请上传图片”);
警报对话框(
标题:文本(“需要图像”),
内容:文本(“请上传图片”),
行动:[
扁平按钮(
子项:文本(“关闭”),
已按下:(){
Navigator.of(context.pop();
},
)
],
);
}
您需要使用函数showDialog
,这样对话框就会出现:
else {
print("Please Upload the Image");
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Image Required"),
content: Text("Please upload the image"),
actions: <Widget>[
FlatButton(
child: Text("Close"),
onPressed: () {
Navigator.of(context).pop();
},
)
],
);
};
);
}
else{
打印(“请上传图片”);
显示对话框(
上下文:上下文,
生成器:(BuildContext上下文){
返回警报对话框(
标题:文本(“需要图像”),
内容:文本(“请上传图片”),
行动:[
扁平按钮(
子项:文本(“关闭”),
已按下:(){
Navigator.of(context.pop();
},
)
],
);
};
);
}
Peter Haddad的答案解决了这个问题,但我建议将AlertDialog放在一个小部件中,这样就可以很容易地再次重用AlertDialog。以下是我为我的项目所做的:
Dialogs.dart:
import 'package:flutter/material.dart';
enum alertDialogAction { cancel, save }
class Dialogs {
static Future<alertDialogAction> alertDialog(
BuildContext context,
String title,
String body,
String cancel,
String save,
) {
Future<alertDialogAction> action = showDialog(
context: context,
barrierDismissible: true,
builder: (BuildContext context) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
title: Text(title),
content: Text(body),
actions: <Widget>[
FlatButton(
onPressed: () =>
Navigator.pop(context, alertDialogAction.cancel),
child: Text(cancel)),
RaisedButton(
color: Colors.orange,
onPressed: () =>
Navigator.of(context).pop(alertDialogAction.save),
child: Text(
save,
style: TextStyle(color: Colors.white),
)),
],
);
});
return (action != null) ? action : alertDialogAction.cancel;
}
您还可以使用scaffold显示对话框,例如
Scaffold.of(context).showSnackBar(SnackBar(content: AlertDialog(content: Text('Alert!!!'))));
但您必须记住,上下文应该是当前脚手架的上下文。
因此,您可能希望将body属性(在Scaffold中)包装到一个Builder小部件中,然后当您在该Builder小部件中使用上下文时,该上下文将是当前Scaffold的上下文。showDialog(
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Your Title!!!"),
content: Text("Your Content!!!"),
actions: <Widget>[
FlatButton(
child: Text("Close"),
onPressed: () {
Navigator.of(context).pop();
},
)
],
);
};
);
上下文:上下文,
生成器:(BuildContext上下文){
返回警报对话框(
标题:文本(“你的标题!!!”,
内容:文本(“您的内容!!!”,
行动:[
扁平按钮(
子项:文本(“关闭”),
已按下:(){
Navigator.of(context.pop();
},
)
],
);
};
);
您可以为警报对话框创建单独的dart类,然后在整个应用程序中使用该类。你可以看看这篇文章
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Your Title!!!"),
content: Text("Your Content!!!"),
actions: <Widget>[
FlatButton(
child: Text("Close"),
onPressed: () {
Navigator.of(context).pop();
},
)
],
);
};
);