我在Flutter项目中使用了AwesomeDialog。当这个AwesomeDialog显示时,我想停止下一行的执行,直到Awesome对话框解除。
这是我为AwesomeDialog Show 编写的代码
import 'package:awesome_dialog/awesome_dialog.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Page(),
);
}
}
class Page extends StatefulWidget {
@override
_PageState createState() => _PageState();
}
class _PageState extends State<Page> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () {
print("aaaaaaaaaaaaaaaaaaaa");
AwesomeDialog(
context: context,
dialogType: DialogType.INFO,
animType: AnimType.BOTTOMSLIDE,
title: 'Register Status',
desc: "Register Successfull",
btnOkOnPress: () {
print("bbbbbbbbbbbbbbbbb");
},
width: 400,
)..show();
print("cccccccccccccccccccc");
},
child: Text("Show Dialog"),
),
),
);
}
}
调试控制台:
aaaaaaaaaaaaaaaaaaaa
cccccccccccccccccccc
bbbbbbbbbbbbbbbbb
在我的警报解除之前,我不想打印ccccccc。
提前感谢
我们可以使用await
关键字来停止执行,直到Future
得到解决:
await AwesomeDialog(
context: context,
dialogType: DialogType.INFO,
animType: AnimType.BOTTOMSLIDE,
title: 'Register Status',
desc: "Register Successfull",
btnOkOnPress: () {
print("bbbbbbbbbbbbbbbbb");
},
width: 400,
)..show();
为了做到这一点,我们需要将onPressed()
函数声明为async
:
onPressed: () async {
print('aaa');
await AwesomeDialog(
context: context,
dialogType: DialogType.INFO,
animType: AnimType.BOTTOMSLIDE,
title: 'Register Status',
desc: "Register Successfull",
btnOkOnPress: () {
print("bbbbbbbbbbbbbbbbb");
},
width: 400,
)..show();
print('cccc');
}
import 'package:awesome_dialog/awesome_dialog.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Page(),
);
}
}
class Page extends StatefulWidget {
@override
_PageState createState() => _PageState();
}
class _PageState extends State<Page> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () {
print("aaaaaaaaaaaaaaaaaaaa");
AwesomeDialog(
context: context,
dialogType: DialogType.INFO,
animType: AnimType.BOTTOMSLIDE,
title: 'Register Status',
desc: "Register Successfull",
btnOkOnPress: () {
print("bbbbbbbbbbbbbbbbb");
},
width: 400,
)..show().then((value) {
print("cccccccccccccccccccc");
});
},
child: Text("Show Dialog"),
),
),
);
}
}