[英]How do I show a Dialog Box on my home screen as soon as it loads in Flutter?
I want to show a dialog box as soon as my home screen loads up with the dialog box containing 4 clickable images in a grid view.我想在我的主屏幕加载时立即显示一个对话框,该对话框在网格视图中包含 4 个可单击的图像。 The images will be routed to individual screens.图像将被传送到各个屏幕。 I am unaware of a way to implement this.我不知道实现这个的方法。 Could use some help.需要一些帮助。 Thanks.谢谢。
You need to use stateful widget to show a dialog when screen loads so do it like this,您需要使用有状态小部件在屏幕加载时显示对话框,所以这样做,
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
void initState() {
super.initState();
SchedulerBinding.instance?.addPostFrameCallback((_) {
showDialog(); // your dialong goes here
}
}
}
Note when you are using inistate
you don't have access to context so you required use a postFrameCallBack
so that whenever context is available the function will get executed.请注意,当您使用inistate
时,您无权访问上下文,因此您需要使用postFrameCallBack
,以便在上下文可用时执行 function。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.