svga / SVGAPlayer-Flutter

The SVGAPlayer implementation of Flutter using CustomPainter.
Other
245 stars 102 forks source link

有一个bug,同一个svga 控件 切换不同的资源路径时,会切换不了 #43

Closed messnoTrace closed 4 years ago

messnoTrace commented 4 years ago

如题,在如下场景,有一个头像装饰列表,里面都是svga,一个头像控件,点击头像装饰列表中的item时,将对应的item中资源加载到头像上,会切换不成功, 原因是在源码 SVGASimpleImage 的initState 中,直接通过 如下代码解析资源: Future decode; if (widget.resUrl != null) { decode = SVGAParser.shared.decodeFromURL(widget.resUrl); } else if (widget.assetsName != null) { decode = SVGAParser.shared.decodeFromAssets(widget.assetsName); } else if (widget.file != null) { decode = widget.file.readAsBytes().then((bytes) { return SVGAParser.shared.decodeFromBuffer(bytes); }); } decode.then((videoItem) { if (mounted) { this.animationController ..videoItem = videoItem ..repeat(); } }); 但是在点击列表切换的时候,这个initState并不会执行,而是会执行到 build 方法中,在 build方法中 初始化SVGAImage 时用的animationController 并没有变,不重新解析的话,animationController.videoItem 并不会改变,最终导致资源路径没有变,

目前的做法是把解析这段代码在提出来,在build中在解析一遍,不知道有没有别的更好的方法

PonyCui commented 4 years ago

SVGASimpleImage({Key key, this.resUrl, this.assetsName, this.file}) : super(key: key);

Key <<<

messnoTrace commented 4 years ago

SVGASimpleImage({Key key, this.resUrl, this.assetsName, this.file}) : super(key: key);

Key <<<

已解决 在使用控件的构造中,传入一个 key: UniqueKey()