flame_riverpod 2.0.0 copy "flame_riverpod: ^2.0.0" to clipboard
flame_riverpod: ^2.0.0 copied to clipboard

Widgets, providers, and an example project for using Riverpod in conjunction with Flame.

Helpers for using Riverpod in conjunction with Flame - to share state from the game into other parts of your application, or from other parts of your application into your game.

Getting started #

Check out the example package to see a FlameGame with a custom Component being updated alongside a comparable Flutter widget. Both depend on a StreamProvider.

Usage #

RiverpodGameWidget is a simple ConsumerStatefulWidget wrapper around a GameWidget.

ComponentRef should be passed from your RiverpodAwareGame to any Components interested in updates from a Provider. It exposes a subset of the functionality users of Riverpod will be familiar with - this is because Components are not Widgets!

In Riverpod with Flame, you should use listen to subscribe to updates from a provider. Alternatively, you could use ref.read as you would elsewhere in Flutter.

/// An excerpt from the Example. Check it out!
class RiverpodAwareTextComponent extends PositionComponent with RiverpodComponentMixin {
  // ComponentRef is a wrapper around WidgetRef and exposes
  // a subset of its API.
  RiverpodAwareTextComponent(ComponentRef ref) {
    this.ref = ref;
  }

  late TextComponent textComponent;
  int currentValue = 0;

  /// [onMount] should be used over [onLoad], as subscriptions are cancelled
  /// inside [onRemove], which is only called if the [Component] was mounted.
  @override
  void onMount() {
    super.onMount();
    add(textComponent = TextComponent(position: position + Vector2(0, 27)));

    listen(countingStreamProvider, (p0, p1) {
      if (p1.hasValue) {
        currentValue = p1.value!;
        textComponent.text = '$currentValue';
      }
    });
  }
}
27
likes
150
pub points
84%
popularity

Publisher

verified publisherflame-engine.org

Widgets, providers, and an example project for using Riverpod in conjunction with Flame.

Homepage

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

flame, flutter, flutter_riverpod

More

Packages that depend on flame_riverpod