简体   繁体   English

在加载页面之前显示加载微调器 (Flutter)

[英]Show loading spinner before loading an page (Flutter)

I have a page that takes to long to load ( I don't know why, maybe a question for another thread).我有一个页面需要很长时间才能加载(我不知道为什么,也许是另一个线程的问题)。 So I want to let the user know that the page is loading.所以我想让用户知道页面正在加载。 But for some reason, I'm only able to make the spinner show indefinitely or not show at all.但出于某种原因,我只能让微调器无限期地显示或根本不显示。 The page takes 10 seconds to load in debug mode.在调试模式下加载页面需要 10 秒。

 onPressed: () async {
                        showDialog(
                            context: context,
                            builder: (context) {
                              return Center(
                                child: CircularProgressIndicator(
                                    color: Colors.white),
                              );
                            });

                        await Navigator.push(context,
                            MaterialPageRoute(builder: (context) {
                          return FormIncident();
                        }));

                        Navigator.of(context).pop();
                      }

I also tried using showModal instead Navigator, and I still face the same problem我也尝试使用 showModal 而不是 Navigator,但我仍然面临同样的问题

You are pushing new page before data loading and your spinner doesn't showing.您在加载数据之前推送新页面,而您的微调器未显示。 In your case, you need to add loader (spinner) to page where are you loading data.在您的情况下,您需要将加载器(微调器)添加到加载数据的页面。 Without additional information I can't make example of your case, but if you are using Future , your FormIncident can be similar like this:如果没有其他信息,我无法举例说明您的情况,但如果您使用的是Future ,您的FormIncident可能类似于这样:

Widget build(BuildContext context) {
  return FutureBuilder<TypeOfYourFuture>(
    future: yourFuture,
    builder: (context, snapshot) {
      // If your data is not loading show loader
      if (!snapshot.hasData) {
        return LoaderWidget();
      // If your data is not loading show loader
      } else if (snapshot.hasError) {
         return ErrorWidget();
      }
      // In other cases you have a valid data and you can shot content
      return ContentWidget();
    }
  );
}

Add more info and we will try to find solution that specific to you.添加更多信息,我们将尝试找到适合您的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM