From 7fbd5af935211264822f89bc1beb4062d3efdf7a Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Mon, 22 Jan 2024 10:38:40 +0100 Subject: [PATCH] feat: Make `Component.key` public (#2988) Since the user might want to use the `ComponentKey` after it has been assigned it should be made public. It also updates `Consumer.key` to `Consumer.widgetKey` in flame_riverpod to avoid a name clash. (Slightly breaking) ### Migration instructions If you are using `consumer.key` from flame_riverpod you have to now use `consumer.widgetKey` instead. ## Related Issues Closes #2985 --- .../flame/lib/src/components/core/component.dart | 15 +++++++-------- packages/flame_riverpod/lib/src/consumer.dart | 6 +++--- packages/flame_riverpod/lib/src/widget.dart | 4 ++-- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/packages/flame/lib/src/components/core/component.dart b/packages/flame/lib/src/components/core/component.dart index 2f00ed46e99..b595ef45cd6 100644 --- a/packages/flame/lib/src/components/core/component.dart +++ b/packages/flame/lib/src/components/core/component.dart @@ -69,9 +69,8 @@ class Component { Component({ Iterable? children, int? priority, - ComponentKey? key, - }) : _priority = priority ?? 0, - _key = key { + this.key, + }) : _priority = priority ?? 0 { if (children != null) { addAll(children); } @@ -897,10 +896,10 @@ class Component { _parent!.onChildrenChanged(this, ChildrenChangeType.added); _clearMountingBit(); - if (_key != null) { + if (key != null) { final currentGame = findGame(); if (currentGame is FlameGame) { - currentGame.registerKey(_key!, this); + currentGame.registerKey(key!, this); } } } @@ -958,10 +957,10 @@ class Component { } void _unregisterKey() { - if (_key != null) { + if (key != null) { final game = findGame(); if (game is FlameGame) { - game.unregisterKey(_key!); + game.unregisterKey(key!); } } } @@ -989,7 +988,7 @@ class Component { /// A key that can be used to identify this component in the tree. /// /// It can be used to retrieve this component from anywhere in the tree. - final ComponentKey? _key; + final ComponentKey? key; /// The color that the debug output should be rendered with. Color debugColor = const Color(0xFFFF00FF); diff --git a/packages/flame_riverpod/lib/src/consumer.dart b/packages/flame_riverpod/lib/src/consumer.dart index b346275cc4d..5f3185008eb 100644 --- a/packages/flame_riverpod/lib/src/consumer.dart +++ b/packages/flame_riverpod/lib/src/consumer.dart @@ -14,7 +14,7 @@ class ComponentRef implements WidgetRef { BuildContext get context => game!.buildContext!; RiverpodAwareGameWidgetState? get _container { - return game?.key?.currentState; + return game?.widgetKey?.currentState; } @override @@ -127,7 +127,7 @@ mixin RiverpodComponentMixin on Component { void rebuildGameWidget() { assert(ref.game!.isMounted == true); if (ref.game!.isMounted) { - ref.game!.key!.currentState!.forceBuild(); + ref.game!.widgetKey!.currentState!.forceBuild(); } } } @@ -137,7 +137,7 @@ mixin RiverpodGameMixin on FlameGame { /// was provided to. /// /// Used to facilitate [Component] access to the [ProviderContainer]. - GlobalKey? key; + GlobalKey? widgetKey; final List _onBuildCallbacks = []; diff --git a/packages/flame_riverpod/lib/src/widget.dart b/packages/flame_riverpod/lib/src/widget.dart index cab90cb0ce8..c1303ca68f4 100644 --- a/packages/flame_riverpod/lib/src/widget.dart +++ b/packages/flame_riverpod/lib/src/widget.dart @@ -65,7 +65,7 @@ class RiverpodAwareGameWidgetState extends GameWidgetState @override void initState() { super.initState(); - game.key = (widget as RiverpodAwareGameWidget).key; + game.widgetKey = (widget as RiverpodAwareGameWidget).key; WidgetsBinding.instance.addPersistentFrameCallback((_) { _isForceBuilding = false; @@ -80,7 +80,7 @@ class RiverpodAwareGameWidgetState extends GameWidgetState @override void didUpdateWidget(covariant GameWidget oldWidget) { super.didUpdateWidget(oldWidget); - game.key = (widget as RiverpodAwareGameWidget).key; + game.widgetKey = (widget as RiverpodAwareGameWidget).key; } @override