Skip to content

Commit

Permalink
feat: Make Component.key public (#2988)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
spydon authored Jan 22, 2024
1 parent a6fe62a commit 7fbd5af
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
15 changes: 7 additions & 8 deletions packages/flame/lib/src/components/core/component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ class Component {
Component({
Iterable<Component>? children,
int? priority,
ComponentKey? key,
}) : _priority = priority ?? 0,
_key = key {
this.key,
}) : _priority = priority ?? 0 {
if (children != null) {
addAll(children);
}
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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!);
}
}
}
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions packages/flame_riverpod/lib/src/consumer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
}
}
Expand All @@ -137,7 +137,7 @@ mixin RiverpodGameMixin<W extends World> on FlameGame<W> {
/// was provided to.
///
/// Used to facilitate [Component] access to the [ProviderContainer].
GlobalKey<RiverpodAwareGameWidgetState>? key;
GlobalKey<RiverpodAwareGameWidgetState>? widgetKey;

final List<void Function()> _onBuildCallbacks = [];

Expand Down
4 changes: 2 additions & 2 deletions packages/flame_riverpod/lib/src/widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class RiverpodAwareGameWidgetState<T extends Game> extends GameWidgetState<T>
@override
void initState() {
super.initState();
game.key = (widget as RiverpodAwareGameWidget<T>).key;
game.widgetKey = (widget as RiverpodAwareGameWidget<T>).key;

WidgetsBinding.instance.addPersistentFrameCallback((_) {
_isForceBuilding = false;
Expand All @@ -80,7 +80,7 @@ class RiverpodAwareGameWidgetState<T extends Game> extends GameWidgetState<T>
@override
void didUpdateWidget(covariant GameWidget<T> oldWidget) {
super.didUpdateWidget(oldWidget);
game.key = (widget as RiverpodAwareGameWidget<T>).key;
game.widgetKey = (widget as RiverpodAwareGameWidget<T>).key;
}

@override
Expand Down

0 comments on commit 7fbd5af

Please sign in to comment.