flutter - 按下按钮时未显示警报对话框
问题描述
警报对话框类
class AlertWindow extends StatelessWidget {
final String title;
const AlertWindow({Key key, this.title}) : super(key: key);
@override
Widget build(BuildContext context) {
return Builder(
builder:(BuildContext context) {
return AlertDialog(
title: Text(this.title),
actions: <Widget>[
new FlatButton(
onPressed: (){
Navigator.of(context).pop();
},
child: new Text(
"OK"
)
),
],
);
}
);
}
}
它在这样的aysnc
函数中被调用
Future<ParseUser> SignUP(username, pass, email) async {
var user = ParseUser(username, pass, email); // You can add columns to user object adding "..set(key,value)"
var result = await user.create();
if (result.success) {
setState(() {
_parseUser = user; // Keep the user
});
print(user.objectId);
new AlertWindow(
title: "Signup success " + user.objectId,
);
} else {
print(result.error.message);
new AlertWindow(
title: "Signup error " + result.error.message,
);
}
}
在运行它时,我可以print
在控制台中看到语句,但AlertWindow
没有出现。
我有一种预感,它可能与我创建它时BuildContext
没有传递给父级有关。AlertDialog
解决方案
您需要调用该函数showDialog
才能AlertDialog
出现:
class AlertWindow {
final String title;
final BuildContext context;
const AlertWindow({Key key, this.title, this.context});
void widget(){
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text(this.title),
actions: <Widget>[
new FlatButton(
onPressed: (){
Navigator.of(context).pop();
},
child: new Text(
"OK"
)
),
],
);
}
);
}
}
工作示例:
推荐阅读
- firebase - 如何使用电子邮件(无密码)设置注册并在firebase中显示名称?
- python - OSX 内部麦克风在 Python 中拾取内部扬声器输出
- angular - 如何有效地在离子中加载大列表?
- regex - 将文本拆分为段落
- nuxt.js - 带有 Nuxt2 的 vue2-google-maps(config.externals 未定义)
- xcode - Xcode 本地提交失败并显示“多个工作副本未能提交文件”。错误
- javascript - 每次渲染 vue 组件时避免新的 axios 请求
- python - 我如何在正则表达式中做条件?
- c# - 将队列的一部分复制到另一个队列,甚至从队列复制到数组
- laravel - 如何在刀片中以多种语言将(创建)数据(CRUD)表格插入数据库并使用 LARAVEL MULTILANGUAGE - LOCALIZE 将其读出?