0

我正在尝试从 nodejs 服务器下载文件。但是当我在 gridview 上实现 onTap 方法时,它会在启动时自动运行 onTap 函数,并且每个文件都开始下载。我无法弄清楚我做错了什么。这是我的代码的预览。我已经实现了 streambuilder 来获取数据。

GridView.builder(
                padding: EdgeInsets.all(10),
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 2,
                    crossAxisSpacing: 10,
                    mainAxisSpacing: 10,
                    childAspectRatio: 0.9),
                itemCount: snapshot.data.data.length,
                itemBuilder: (context, i) {
                  return Container(
                    padding: EdgeInsets.all(10),
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(20),
                        color: Colors.blue.withOpacity(0.3)),
                    child: Column(
                      children: <Widget>[
                        Container(
                          height: 50,
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(20),
                              image: DecorationImage(
                                  image: AssetImage('assets/pdf_icon.png'))),
                        ),
                        Divider(),
                        for (var items in snapshot.data.data[i].attachments)
                          ResourceDetail(
                            name: snapshot.data.data[i].title,
                            author: snapshot.data.data[i].user.name,
                            onTap: downloadFile(
                                'url/api/${items.source}',
                                snapshot.data.data[i].title,
                                items.extension),
                          ),
                      ],
                    ),
                  );
                },
              );

这里是下载文件的方法。

Future<void> downloadFile(
      String url, String fileName, String extension) async {
    var dio = new Dio();
    var dir = await getExternalStorageDirectory();
    var downloadDir =
        await new io.Directory('${dir.path}/downloads').create(recursive: true);
    io.File('${downloadDir.path}/$fileName.$extension').exists().then((a) async {
      print(a);
      if (a) {
        print("Opening file");
        showDialog(
            context: context,
            builder: (_) {
              return AlertDialog(
                backgroundColor: Color(0xff5b8c85),
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10)),
                title: Text('File is already downloaded'),
                actions: <Widget>[
                  FlatButton(
                      child: Text(
                        'Open',
                        style: TextStyle(color: Colors.white),
                      ),
                      onPressed: () {
                        OpenFile.open('${downloadDir.path}/$fileName.$extension');
                        Navigator.pop(context);
                      })
                ],
              );
            });
        return;
      } else {
        print("Downloading file");
        openDialog();
        await dio.download(url, '${downloadDir.path}/$fileName.$extension',
            onReceiveProgress: (rec, total) {
          if (mounted) {
            setState(() {
              progressValue = (rec / total);
              progressString = ((rec / total) * 100).toStringAsFixed(0) + "%";
              myDialogState.setState(() {
                myDialogState.progressData = progressString;
                myDialogState.progressValue = progressValue;
              });
            });
          }
        });
        if (mounted) {
          setState(() {
            print('${downloadDir.path}');
          });
        }
        print("Download completed");
      }
    });
  }
4

1 回答 1

0

GridView.builder 中的 ResourceDetails,尝试将 onTap 替换为此

onTap: ()=>downloadFile(
    'url/api/${items.source}',
    snapshot.data.data[i].title,
    items.extension)
于 2020-02-19T10:25:45.450 回答