【问题标题】:Flutter display gif from Uint8List来自 Uint8List 的 Flutter 显示 gif
【发布时间】:2022-01-11 05:28:32
【问题描述】:

我需要在我的 Flutter 应用程序中显示 gif。从后端,我从响应中获取 gif 作为 Uint8List 列表。你能帮我看看如何在屏幕上显示这个吗?

我的代码在这里:

widget.session
        .get('/api/caff/getCaff/' + widget.gifId.toString())
        .then((response) async {
      if (response.statusCode == 200) {
        Uint8List bytes = response.bodyBytes;
        _gifFile = File.fromRawPath(bytes); // tried this but didn't work
      } else {
        CaffToast.showError(
            'Something went wrong! Please check your network connection!');
      }
    });

我尝试将其显示为文件图像,但它不起作用:

@override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          _gifFile == null ? Container() : Container(
            decoration: BoxDecoration(
                image: DecorationImage(
                    image: FileImage(_gifFile!))),
          ),
        ],
      ),
    );
  }

您有什么建议可以解决这个问题吗?

【问题讨论】:

    标签: flutter gif mobile-development uint8list


    【解决方案1】:

    无需将数据写入文件。图像数据已经在内存中,所以只需显示它。只需改用MemoryImage 图像提供程序

    我不确定您是如何从网络调用中获取数据到您的 build 方法的,所以我使用占位符,但就像您在使用文件时所做的一样。

    _bytes = response.bodyBytes;
    
    @override
      Widget build(BuildContext context) {
        return Container(
          child: Column(
            children: [
              _gifFile == null ? Container() : Container(
                decoration: BoxDecoration(
                    image: DecorationImage(
                        image: MemoryImage(_bytes!))),
              ),
            ],
          ),
        );
      }
    

    【讨论】:

    • 谢谢,它使显示更简单,但问题是容器没有大小,默认为 0 高度。我编辑了容器大小,现在它可以工作了。
    • @BenceKovács 无论如何,这种方法效率更高,因为您不再进行文件 IO。
    猜你喜欢
    • 2020-10-26
    • 2012-03-20
    • 2019-08-03
    • 2021-10-13
    • 2022-01-13
    • 2015-09-13
    • 2020-07-09
    • 2023-01-02
    • 1970-01-01
    相关资源
    最近更新 更多