简体   繁体   English

如何在 Flutter 加载后立即在主屏幕上显示对话框?

[英]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.

相关问题 如何在 Flutter 应用程序加载时显示一些加载屏幕或启动画面 - How can i show some loading screen or splash screen while flutter application loads up 屏幕加载后如何显示对话框(无 onTap)Flutter - How to display a dialog once a screen loads (without onTap) Flutter 如何在 flutter 中显示没有上下文的对话框 - How to show dialog box without context in flutter Flutter:如何使我的对话框可滚动? - Flutter: How to make my dialog box scrollable? 如何确保我的图像在颤动中的 url 不会显示在屏幕上而不是图像上? - How do I ensure that the url of my image in flutter doesn't show up on the screen instead of the image? Flutter-我如何从登录屏幕切换到主页和返回? 后端工作但不手动刷新无法显示屏幕 - Flutter-How do I switch from login screen to home and back? Back-end works but can't show screen without manually refresh Flutter 显示全屏对话框 - Flutter show full screen dialog 我如何在 flutter 中旋转我的小盒子? - How do i rotate my fractionally sized box in flutter? 在 Flutter 中按下导航抽屉中的内容时,如何更新主屏幕的布局? - How to I update the layout of my home screen when something in the navigation drawer is pressed in Flutter? 如何在 Flutter 中设置主屏幕的背景颜色? - How do I set the background color of my main screen in Flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM