isLoading 没有 setState?颤动(CircularProgressIndicator)

isLoading without setState? Flutter (CircularProgressIndicator)

我试过了

setState(() {isLoading = true})
//do something
setState(() {isLoading = false})

在 onTab() 中{}

但是使用 setState 时,pdfFile 和 pdfController 等数据会被覆盖,应用程序不会推送到新路由。

好吧,我的问题是:如何向用户显示加载动画,如果加载完成,将下载的数据推送到屏幕?也许我需要一些没有 setState 的东西?我们有什么可能性?

body: StreamBuilder(
        stream: firebase.base
            .collection('xxx')
            .doc(firebase.auth.currentUser!.uid)
            .collection("xxx")
            .snapshots(),
        builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
          if (snapshot.hasData) {

            final liste = snapshot.data!.docs;
               return Column(
              children: [
                Container(
                  height: 400,
                  child: ListView.builder(
                    padding: const EdgeInsets.all(8),
                    itemCount: liste.length,
                    itemBuilder: (BuildContext context, int index) {
                      Map data = snapshot.data!.docs[index].data() as Map;
                     String fileName = data['fileName'];
                     String url = data['url'];
                      return GestureDetector(
                        onTap: () async {
                          http.Response pdfFile =
                              await http.get(Uri.parse(url));

                          final pdfController = PdfController(
                              document:
                                  PdfDocument.openData(pdfFile.bodyBytes));

                          Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) =>
                                      PdfViewScreen(pdfController, fileName)));
                        },

您可以在大括号后使用 else 表达式。或者在 if

之后只使用 return Center(child: CircularProgressIndicator());

也许以下逻辑适合您:

onTap 仅将用户重定向到 PdfViewScreen。在 PdfViewScreen 中,您使用 FutureBuilder 来解析 http.get(Uri.parse(url)),之后 FutureBuilder 将 return 您使用的 PDFviewer。在 FutureBuilder 中,您可以显示一个 progressIndicator




body: StreamBuilder(
        stream: firebase.base
            .collection('xxx')
            .doc(firebase.auth.currentUser!.uid)
            .collection("xxx")
            .snapshots(),
        builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
          if (snapshot.hasData) {
            final liste = snapshot.data!.docs;
            return Column(
              children: [
                Container(
                  height: 400,
                  child: ListView.builder(
                    padding: const EdgeInsets.all(8),
                    itemCount: liste.length,
                    itemBuilder: (BuildContext context, int index) {
                      Map data = snapshot.data!.docs[index].data() as Map;
                      String fileName = data['fileName'];
                      String url = data['url'];
                      return GestureDetector(
                        onTap: () async {
                          Future<http.Response> pdfFile =
                              http.get(Uri.parse(url));
                          showDialog(
                              context: context,
                              builder: (context) {
                                return FutureBuilder<http.Response>(
                                    future: http.get(Uri.parse(url)),
                                    builder: (context, snapshot) {
                                      if (snapshot.hasData) {
                                        final pdfController = PdfController(
                                            document: PdfDocument.openData(
                                                snapshot.data!.bodyBytes));

                                        return PdfViewScreen(
                                            pdfController, fileName);
                                      }
                                      return const Center(
                                          child: CircularProgressIndicator());
                                    });
                              });
                        },