flutter - 带有加载微调器和确认的颤振 showDialog
问题描述
在颤振中,我有一个带有取消和确认按钮的 showDialog() 。确认按钮将触发对 API 的调用。我需要的是在单击后在 showDialog 窗口中显示“正在加载...”,一旦 API 调用完成以显示成功或失败。我该如何管理?或者我应该关闭窗口,等待回复并弹出一个成功或错误的新对话窗口?感谢您提供任何帮助或更好的建议。这是我到目前为止所拥有的:
void _showAlert(String textLabel, String action, String linkId) {
showDialog(
context: context,
//barrierDismissible: false, // on external click
builder: (_) => new AlertDialog(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.0)),
title: new Text(
'Please confirm:',
style: TextStyle(color: Colors.deepOrange, fontWeight: FontWeight.bold),
textAlign: TextAlign.center,
),
content: new Text(
textLabel,
style: new TextStyle(fontSize: 20.0),
),
actions: <Widget>[
new FlatButton(
onPressed: () {
Navigator.pop(context);
},
child: new Text('CANCEL')),
new FlatButton(
onPressed: () {
_postAction(action, linkId).then((_) => setState(() {}));
Navigator.pop(context);
},
child: new Text('I CONFIRM')),
],
));
}
解决方案
你可以试试这个,这只是想法..
class _SampleState extends State<Sample> {
bool isSuccessFromApi = false;
bool isLoading = false;
Widget popup() {
showDialog(context: context, builder: (builder) {
return AlertDialog(
content: !isSuccessFromApi ? Container(
child: Text('Are you Sure???'),
) : Container( child: isLoading ? CircularProgressIndicator() : Text('Success'),),
actions: <Widget>[
Text('Cancel'),
InkWell(child: Text('OK'), onTap: apiCall,)
],
);
});
}
void apiCall(){
setState(() {
isLoading = true;
});
//call the api
//after success or failure
setState(() {
isLoading = false;
isSuccessFromApi = true;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: popup(),
);
}
}
推荐阅读
- c++ - 无法将值转换为mysql
- python - 来自 usnews.com 的 python 请求超时其他网站工作正常
- html - Bootstrap4 Accordion - 在不使用 JavaScript 的情况下全部折叠
- typescript - 在 TypeScript 中,如何确保文字类型读/写对应?
- azure - 不能使用从 cosmos db 到 mongoexport 的连接字符串?
- jquery - 在 Jquery 中使用正则表达式替换短语中的任何单词
- datatables - 呈现为html元素时如何更新行
- jsp - 如何使用 JSP 在登录中创建 base64_encode
- xamarin.forms - 在 Xamarin 表单中将 CollectionView 显示为 InfiniteView
- python - 如何继承 UserDict 和元类