我有一个搜索对话框,其中显示找到的项目的列表。 当用户单击一个项目时,我想弹出搜索对话框然后读取并显示数据,但是读取是一项繁重的任务。
(Navigator.pop()是同步的,不是未来的)
如果我的代码是这样的:
machine
这将减慢过渡动画的速度并使视图变慢。
我应该这样做,这当然是肮脏的代码:
Navigator.pop(context);
readSelectedItem();
答案 0 :(得分:0)
Dartpad Ex:https://dartpad.dartlang.org/flutter
bool isRead = false;
@override
void initState() {
super.initState();
readData();
}
void readData() async {
//your long running task
//once data is read, change bool isRead to true
isRead = true;
//if you want to rebuild widgets change it in setState method
}
@override
Widget build(BuildContext context) {
return Container(
color: Colors.white,
alignment: Alignment.center,
child: RaisedButton(
onPressed: () {
show();
},
child: Text('Show Dialog', style: TextStyle(color: Colors.white)),
color: Colors.black,
));
}
void show() {
showDialog(
context: context,
builder: (con) => WillPopScope(
onWillPop:()=>Future.value(false),
child: AlertDialog(
title: Text('Title'),
content: Text('Your Search widgets...'),
actions: [
FlatButton(
onPressed: () {
if (isRead)
Navigator.of(context).pop();
else {
//Disply toast or something !
}
},
child: Text('Close'))
]),
));
}