如何使用 Image.memory() 或 MemoryImage() flutter 缓存内存图像?

Dar*_* Z. 5 android ios flutter

如何使用Image.memory()MemoryImage()flutter缓存内存图像?

我有数据列表,但图像类型是字节。我正在使用Image.memory()MemoryImage()内部,Gridview.builder但当分页(新数据即将到来)或滚动时,它每次重新渲染网格示例都会闪烁。

感谢帮助

代码:

StreamBuilder<List<Datum>>(
        stream: _provider.promotionsStream,
        builder: (BuildContext context, AsyncSnapshot<List<Datum>> snapshot) {
          return Padding(
            padding: EdgeInsets.only(top: 16.0),
            child: NotificationListener<ScrollEndNotification>(
              onNotification: (scrollEnd) {
                if (scrollEnd.metrics.pixels >=
                    scrollEnd.metrics.maxScrollExtent * .45) {
                  _provider.nextPage();
                }
                return true;
              },
              child: SingleChildScrollView(
                child: Column(
                  children: [
                    GridView.builder(
                      shrinkWrap: true,
                      physics: BouncingScrollPhysics(),
                      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 3,
                      ),
                      itemCount:
                          _provider.dataLentgh > 16 ? _provider.dataLentgh : 18,
                      itemBuilder: (ctx, idx) {
                        Datum? _data = snapshot.data == null
                            ? null
                            : _provider.dataLentgh > idx
                                ? snapshot.data![idx]
                                : null;

                        return AspectRatio(
                          aspectRatio: 1.0,
                          child: Container(
                            padding: EdgeInsets.all(2.0),
                            alignment: Alignment.center,
                            width: 60,
                            decoration: BoxDecoration(
                              color: Colors.white,
                              border:
                                  Border.all(color: Colors.grey, width: 0.5),
                            ),
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Container(
                                  width: 60,
                                  height: 60,
                                  decoration: BoxDecoration(
                                    image: _data == null
                                        ? null
                                        : _data.imageUrl.isEmpty
                                            ? null
                                            : DecorationImage(
                                                fit: BoxFit.contain,
                                                image: Image.memory(
                                                  Uint8List.fromList(
                                                      _data.imageBytes),
                                                ).image,
                                              ),
                                    color: Colors.transparent,
                                  ),
                                ),
                              ],
                            ),
                          ),
                        );
                      },
                    ),
                    
                  ],
                ),
              ),
            ),
          );
        },
      ),
Run Code Online (Sandbox Code Playgroud)

来自API的字节类型数据需要在gridview中显示为图像。但每次数据更新或滚动时问题都会闪烁

Dar*_* Z. 6

终于找到了解决方案,我只是自定义图像提供程序来缓存字节类型的图像。这是代码

https://gist.github.com/darmawan01/9be266df44594ea59f07032e325ffa3b

也许还有另一种方法可以做到这一点,你们想与阅读这篇文章的任何人分享。我会很感激的。

希望这有帮助