Search code examples
animationgifflutter

flutter: gif animation listener in Image


I'm still new to use flutter, it is an interesting language. Just need to know.

@override
Widget build(BuildContext context){
  return Center(
     child : Image.asset(
          "animated.gif"
     )
  );
}

If I build an animated image widget with Image.asset, is that possible to know when the animation finish, jump to the specific frame of the image just custom, and add a listener, or is there another way to achieve that?


Solution

  • The Image widget does not expose the progress of the animated asset. However, you can use the lower level APIs to get more control.

    If all you need is visibility into when frames are scheduled, you can work directly with the MultiFrameImageStreamCompleter. Look at the implementation of the Image widget for an example of how to get an image stream completer and use it.

    Jumping to a specific animation frame is more tricky as the animation formats encodes deltas from previous frame and thus does not support random seek. One way to achieve that would be to decode and cache all frames, you can use the ui.Codec API to decode frames yourself and cache them. Note that doing this will consume a potentially large amount of memory.