Flutter Listview在使用FutureBuilder加载缩略图时会出现卡顿的情况。

3
我是一名帮助翻译的助手。
我正在使用Flutter创建一个文件管理器来学习。由于我刚开始学习Flutter,因此对它并不了解。
我的问题是想要在文件夹中为图像显示缩略图,我通过在ListView中使用FutureBuilder调用了一个写在Android本地代码中的函数。虽然缩略图是异步调用的,但每当我打开有很多图像的目录时,它会冻结UI一段时间,我无法滚动页面。
以下是我的ListView代码:
@override
Widget build(BuildContext context) {
    return new ListView.builder(
      itemBuilder: (itemContext, i) {
        if (_filesList != null && i < _filesList.length) {
          String currDir = _filesList[i]['path'];
          return new ListTile(
            title: new Text(_filesList[i]['name']),
            leading: (_filesList[i]['isDir'] == "false")
                ? new FutureBuilder<Uint8List>(
                    future: _getThumbnail(currDir),
                    builder: (BuildContext context,
                        AsyncSnapshot<Uint8List> snapshot) {
                      switch (snapshot.connectionState) {
                        case ConnectionState.waiting:
                          return new Container(
                            decoration: new BoxDecoration(
                              color: Colors.brown,
                              borderRadius: new BorderRadius.circular(20.0),
                            ),
                            width: 30.0,
                            height: 30.0,
                          );
                        default:
                          if (snapshot.hasError)
                            return new Container(
                              decoration: new BoxDecoration(
                                color: Colors.brown,
                                borderRadius: new BorderRadius.circular(20.0),
                              ),
                              width: 30.0,
                              height: 30.0,
                            );
                          else {
                            return new Image.memory(snapshot.data);
                          }
                      }
                    },
                  )
                : new Container(
                    decoration: new BoxDecoration(
                      color: (_filesList[i]['isDir'] == "true")
                          ? Colors.yellow
                          : Colors.brown,
                      borderRadius: new BorderRadius.circular(20.0),
                    ),
                    width: 30.0,
                    height: 30.0),
            onTap: () {
              setState(() {
                _currentDir = currDir;
              });
              _getFilesList(currDir);
            },
          );
        } else {
          return null;
        }
      },
    );
  }

_getThumbnail()的代码:

Future <Uint8List> _getThumbnail(path) async {
    try {
      String thumbnailString =
          await channel.invokeMethod("getThumbnail", {"path": path});
      thumbnailString = thumbnailString.replaceAll('\n', '');
      Uint8List bytes = BASE64.decode(thumbnailString);
      return bytes;
    } on PlatformException catch (e) {
      print("Files Not Found $e{e.message}");
      return null;
    }
  }

遇到了同样的问题。你解决了吗?能给我一个思路吗? - undefined
这个链接可能对缩略图有所帮助:https://dev59.com/m1QJ5IYBdhLWcg3w66Q5#59093984。 - undefined
1个回答

0
尽管在Flutter中调用是异步的,但处理getThumbnail的Android代码可能会阻塞UI线程,导致加载卡顿或滚动不流畅。解决方案是使用AsyncTaskdoInBackground中构建/获取缩略图,并在onPostExecute中返回结果。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接