From 0ba9504472ff86f13d842bdb59d1e81d09ad08cd Mon Sep 17 00:00:00 2001 From: Munsterlander Date: Fri, 18 Aug 2023 17:17:11 -0700 Subject: [PATCH 1/6] Initial Commit --- packages/flame/lib/src/game/game.dart | 4 ++++ packages/flame/lib/src/game/game_widget/game_widget.dart | 7 +++++-- .../test/game/game_widget/game_widget_lifecycle_test.dart | 6 ++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/flame/lib/src/game/game.dart b/packages/flame/lib/src/game/game.dart index 46e183301c9..7163488c639 100644 --- a/packages/flame/lib/src/game/game.dart +++ b/packages/flame/lib/src/game/game.dart @@ -213,6 +213,10 @@ abstract mixin class Game { /// do cleanups to avoid memory leaks. void onRemove() {} + /// Called when the game has been removed from the Flutter widget tree when + /// it was disposed by Flutter. + void onDispose() {} + /// Called after the game has left the widget tree. /// This can be overridden to add logic that requires the game /// not be on the flutter widget tree anymore. diff --git a/packages/flame/lib/src/game/game_widget/game_widget.dart b/packages/flame/lib/src/game/game_widget/game_widget.dart index 61a5da0e619..163380d84dd 100644 --- a/packages/flame/lib/src/game/game_widget/game_widget.dart +++ b/packages/flame/lib/src/game/game_widget/game_widget.dart @@ -254,8 +254,11 @@ class GameWidgetState extends State> { _loaderFuture = null; } - void disposeCurrentGame() { + void disposeCurrentGame({bool isDispose = false}) { currentGame.removeGameStateListener(_onGameStateChange); + if (isDispose) { + currentGame.onDispose(); + } } @override @@ -281,7 +284,7 @@ class GameWidgetState extends State> { @override void dispose() { super.dispose(); - disposeCurrentGame(); + disposeCurrentGame(isDispose: true); // If we received a focus node from the user, they are responsible // for disposing it if (widget.focusNode == null) { diff --git a/packages/flame/test/game/game_widget/game_widget_lifecycle_test.dart b/packages/flame/test/game/game_widget/game_widget_lifecycle_test.dart index 16914e64725..df625d6f899 100644 --- a/packages/flame/test/game/game_widget/game_widget_lifecycle_test.dart +++ b/packages/flame/test/game/game_widget/game_widget_lifecycle_test.dart @@ -30,6 +30,12 @@ class _MyGame extends FlameGame { super.onRemove(); events.add('onRemove'); } + + @override + void onDispose() { + super.onDispose(); + events.add('onDispose'); + } } class _TitlePage extends StatelessWidget { From 0855dabd3b7000b31bf49156f4f4f98c0e289cca Mon Sep 17 00:00:00 2001 From: Munsterlander Date: Fri, 18 Aug 2023 20:15:01 -0700 Subject: [PATCH 2/6] Updated tests --- .../test/game/game_widget/game_widget_lifecycle_test.dart | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/flame/test/game/game_widget/game_widget_lifecycle_test.dart b/packages/flame/test/game/game_widget/game_widget_lifecycle_test.dart index df625d6f899..568b5218472 100644 --- a/packages/flame/test/game/game_widget/game_widget_lifecycle_test.dart +++ b/packages/flame/test/game/game_widget/game_widget_lifecycle_test.dart @@ -191,6 +191,11 @@ void main() { true, reason: 'onRemove was not called', ); + expect( + events.contains('onDispose'), + true, + reason: 'onDispose was not called', + ); }); testWidgets('on resize, parents are kept', (tester) async { @@ -235,6 +240,7 @@ void main() { 'onLoad', 'onMount', 'onRemove', + 'onDispose', 'onGameResize', 'onMount', ], From a16af1699ce29d9193311c690031791f76fc0673 Mon Sep 17 00:00:00 2001 From: Munsterlander Date: Sun, 20 Aug 2023 08:47:12 -0700 Subject: [PATCH 3/6] Clarifications --- packages/flame/lib/src/game/game.dart | 4 ++-- packages/flame/lib/src/game/game_widget/game_widget.dart | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/flame/lib/src/game/game.dart b/packages/flame/lib/src/game/game.dart index 7163488c639..c14c71ed678 100644 --- a/packages/flame/lib/src/game/game.dart +++ b/packages/flame/lib/src/game/game.dart @@ -213,8 +213,8 @@ abstract mixin class Game { /// do cleanups to avoid memory leaks. void onRemove() {} - /// Called when the game has been removed from the Flutter widget tree when - /// it was disposed by Flutter. + /// Called when the game has been removed from the Flutter widget tree via + /// the Game Widget's Flutter dispose event. void onDispose() {} /// Called after the game has left the widget tree. diff --git a/packages/flame/lib/src/game/game_widget/game_widget.dart b/packages/flame/lib/src/game/game_widget/game_widget.dart index 163380d84dd..6a794c8f463 100644 --- a/packages/flame/lib/src/game/game_widget/game_widget.dart +++ b/packages/flame/lib/src/game/game_widget/game_widget.dart @@ -254,9 +254,9 @@ class GameWidgetState extends State> { _loaderFuture = null; } - void disposeCurrentGame({bool isDispose = false}) { + void disposeCurrentGame({bool callGameOnDispose = false}) { currentGame.removeGameStateListener(_onGameStateChange); - if (isDispose) { + if (callGameOnDispose) { currentGame.onDispose(); } } @@ -284,7 +284,7 @@ class GameWidgetState extends State> { @override void dispose() { super.dispose(); - disposeCurrentGame(isDispose: true); + disposeCurrentGame(callGameOnDispose: true); // If we received a focus node from the user, they are responsible // for disposing it if (widget.focusNode == null) { From 60db04741dcc3407673bcdb2f72ac11f33fd6bee Mon Sep 17 00:00:00 2001 From: Munsterlander Date: Sun, 20 Aug 2023 09:11:47 -0700 Subject: [PATCH 4/6] Updated per Spydon recommendation --- packages/flame/lib/src/game/game.dart | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/flame/lib/src/game/game.dart b/packages/flame/lib/src/game/game.dart index c14c71ed678..53f2e2e88bb 100644 --- a/packages/flame/lib/src/game/game.dart +++ b/packages/flame/lib/src/game/game.dart @@ -213,8 +213,7 @@ abstract mixin class Game { /// do cleanups to avoid memory leaks. void onRemove() {} - /// Called when the game has been removed from the Flutter widget tree via - /// the Game Widget's Flutter dispose event. + /// Called when the GameWidget is disposed by Flutter. void onDispose() {} /// Called after the game has left the widget tree. From 4a3e9ef59ec2227c505567678b41795120c8bbe9 Mon Sep 17 00:00:00 2001 From: Munsterlander Date: Sun, 20 Aug 2023 09:15:31 -0700 Subject: [PATCH 5/6] Updated doc --- packages/flame/lib/src/game/game_widget/game_widget.dart | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/flame/lib/src/game/game_widget/game_widget.dart b/packages/flame/lib/src/game/game_widget/game_widget.dart index 6a794c8f463..dd44f296e68 100644 --- a/packages/flame/lib/src/game/game_widget/game_widget.dart +++ b/packages/flame/lib/src/game/game_widget/game_widget.dart @@ -254,6 +254,9 @@ class GameWidgetState extends State> { _loaderFuture = null; } + /// [disposeCurrentGame] is called by two flutter events - `didUpdateWidget` + /// and `dispose`. When the parameter `callGameOnDispose` = true, the + /// `currentGame`'s `onDispose` method will be called; otherwise, it will not. void disposeCurrentGame({bool callGameOnDispose = false}) { currentGame.removeGameStateListener(_onGameStateChange); if (callGameOnDispose) { From ea6463e626ec84f65cae5f61407f427ffc60326a Mon Sep 17 00:00:00 2001 From: Munsterlander Date: Sun, 20 Aug 2023 09:19:54 -0700 Subject: [PATCH 6/6] Update packages/flame/lib/src/game/game_widget/game_widget.dart Co-authored-by: Lukas Klingsbo --- packages/flame/lib/src/game/game_widget/game_widget.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/flame/lib/src/game/game_widget/game_widget.dart b/packages/flame/lib/src/game/game_widget/game_widget.dart index dd44f296e68..fdf734c3bac 100644 --- a/packages/flame/lib/src/game/game_widget/game_widget.dart +++ b/packages/flame/lib/src/game/game_widget/game_widget.dart @@ -255,7 +255,7 @@ class GameWidgetState extends State> { } /// [disposeCurrentGame] is called by two flutter events - `didUpdateWidget` - /// and `dispose`. When the parameter `callGameOnDispose` = true, the + /// and `dispose`. When the parameter [callGameOnDispose] is true, the /// `currentGame`'s `onDispose` method will be called; otherwise, it will not. void disposeCurrentGame({bool callGameOnDispose = false}) { currentGame.removeGameStateListener(_onGameStateChange);