Skip to content

Commit

Permalink
fix: SpriteAnimationWidget was resetting the ticker even when the pla…
Browse files Browse the repository at this point in the history
…ying didn't changed (#2891)

`SpriteAnimationWidget` was incorrectly reseting its `animationTicker`
everytime it had a change, even if the playing attribute did not change.
This PR fixes that.
  • Loading branch information
erickzanardo authored Dec 1, 2023
1 parent 0c74560 commit 9aed8b4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
13 changes: 9 additions & 4 deletions packages/flame/lib/src/widgets/animation_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,17 @@ class _InternalSpriteAnimationWidgetState
if (oldWidget.animation != widget.animation) {
oldWidget.animationTicker.onComplete = null;
_setupController();
if (widget.playing) {
_initAnimation();
}
}

if (widget.playing) {
_initAnimation();
} else {
_pauseAnimation();
if (widget.playing != oldWidget.playing) {
if (widget.playing) {
_initAnimation();
} else {
_pauseAnimation();
}
}
}

Expand Down
44 changes: 44 additions & 0 deletions packages/flame/test/widgets/sprite_animation_widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,50 @@ Future<void> main() async {
},
);

testWidgets(
'does not resets the ticker when has a new animation',
(tester) async {
final frames = List.generate(5, (_) => Sprite(image));
final animation = SpriteAnimation.spriteList(
frames,
stepTime: 0.1,
loop: false,
);
final animationTicker = SpriteAnimationTicker(animation);

animationTicker.setToLast();

await tester.pumpWidget(
MaterialApp(
home: StatefulBuilder(
builder: (context, setState) {
return Column(
children: [
SpriteAnimationWidget(
animation: animation,
animationTicker: animationTicker,
),
ElevatedButton(
onPressed: () {
setState(() {});
},
child: const Text('Change animations'),
),
],
);
},
),
),
);
animationTicker.setToLast();

await tester.tap(find.byType(ElevatedButton));
await tester.pump();

expect(animationTicker.isLastFrame, isTrue);
},
);

testWidgets(
'onComplete callback is called when the animation is finished',
(tester) async {
Expand Down
2 changes: 1 addition & 1 deletion packages/flame_riverpod/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Showcasing the flame_riverpod functionality.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1
environment:
sdk: '>=2.18.5 <3.0.0'
sdk: ">=3.0.0 <4.0.0"
dependencies:
flame: ^1.10.1
flame_riverpod: ^5.0.0
Expand Down
2 changes: 1 addition & 1 deletion packages/flame_riverpod/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: Helpers for using Riverpod - a reactive caching and data-binding fr
version: 5.0.0
homepage: https://github.com/flame-engine/flame/tree/main/packages/flame_riverpod
environment:
sdk: '>=2.18.5 <4.0.0'
sdk: ">=3.0.0 <4.0.0"
flutter: ">=1.17.0"
dependencies:
flame: ^1.10.1
Expand Down

0 comments on commit 9aed8b4

Please sign in to comment.