fluttercommunity / flutter_blurhash

Compact representation of a placeholder for an image. Encode a blurry image under 30 caracters for instant display like used by Medium. Maintainer: @Solido
https://pub.dev/packages/flutter_blurhash
MIT License
528 stars 66 forks source link

onDecoded is not firing #26

Closed aytunch closed 2 years ago

aytunch commented 3 years ago

We see onDecoded in the packages API however It is not connected to the related action at the moment. There is no instance of call(widget.onDecoded) Could you please add onDecoded on both the nullsafe and nullable versions.

aytunch commented 3 years ago

I was experimenting and added widget.onDecoded?.call(); before Image is being returned. I don't know why but the future builder fires more than once. Maybe it is because the buildBlurHashBackground is a widget function rather than a StatelessWidget. Same with prepareDisplayedImage. Fixing this would increase performance for sure. Another solution might be to use decoded and decoding bool like the ones used in the code:

loaded = false;
loading = false;

And inside of the build method we can do

if(decoded)
    return Image(image: UiImage(snap.data), fit: widget.imageFit);
Widget buildBlurHashBackground() => FutureBuilder<ui.Image>(
        future: _image,
        builder: (ctx, snap) {
          if (snap.hasData) {
            print("onDecoded hash:${widget.hash}");
            widget.onDecoded?.call(); //This gets called for more than once per widget.
            return Image(image: UiImage(snap.data), fit: widget.imageFit);
          } else {
            return Container(color: widget.color);
          }
        },
      );
}
Solido commented 2 years ago

Hi, onDecoded and onDisplayed events have been added. Thx