从数据库中提取数据后,如何创建AlertDialog



从数据库中获取数据后,如何在flutter中创建AlertDialog。我可以创建AlertDialog,但我想根据从数据库中获取的数据更改其子容器的颜色。我为颜色参数设置了一个条件,但它不会改变容器的颜色,因为该条件被延迟了。

我是否可以创建延迟AlertDialog或其Childrens的创建?下面的小部件在AlertDialog中,我想在从数据库中获取它的状态后更改它的颜色。

Expanded(
child: Container(
height: size.height * 0.05,
width: size.width * 0.16,
decoration: BoxDecoration(
color:  setCompletedTasksColor(selectedSimName, index) == true ? Colors.green : kSelectedMenuColorLavender,
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(10.0),
bottomRight: Radius.circular(10.0),
)
),
child: Center(child: Text(getQtgMonths(index),textAlign : TextAlign.center))),
),

下面是从数据库获取数据的函数

Future setCompletedTasksColor(String selectedTask, int index) async {
var response = await fetchSelectedTasks(selectedTask, getQuarter(index));
if(response.runtimeType != String){
var taskStatus = response['taskCompleted'];
print(taskStatus);
if(taskStatus == 'true'){
return true;
}else{
return ;
}
}else {
return false;
}

}

当您得到响应时,只需调用此函数

Future<void> _showMyDialog() async {
return showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: Text('AlertDialog Title'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('This is a demo alert dialog.'),
Text('Would you like to approve of this message?'),
],
),
),
actions: <Widget>[
TextButton(
child: Text('Approve'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}

像这个

Future setCompletedTasksColor(String selectedTask, int index) async {
var response = await fetchSelectedTasks(selectedTask, getQuarter(index));
if(response.runtimeType != String){
var taskStatus = response['taskCompleted'];
print(taskStatus);
if(taskStatus == 'true'){
_showMyDialog();//This wil open an alert when response of your request will be true
return true;
}else{
return ;
}
}else {
return false;
}

最新更新