Files
Lukas Klingsbo d409193300 chore(release): Publish Flame v1.16.0 et. al (#3044)
```
Package Name           Current Version   Updated Version   Update Reason
flame                  1.15.0            1.16.0            manual versioning
flame_audio            2.1.8             2.10.0            manual versioning
flame_bloc             1.10.10           1.11.0            manual versioning
flame_fire_atlas       1.4.8             1.5.0             manual versioning
flame_rive             1.9.11            1.10.0            manual versioning
flame_riverpod         5.2.0             5.3.0             manual versioning
flame_svg              1.9.0             1.10.0            manual versioning
flame_test             1.15.4            1.16.0            manual versioning
flame_texturepacker    3.0.0             3.1.0             manual versioning
flame_tiled            1.18.4            1.19.0            manual versioning
flame_forge2d          0.16.0+5          0.17.0            updated with major changes
flame_isolate          0.5.1             0.6.0             updated with major changes
flame_lottie           0.3.0+8           0.4.0             updated with major changes
flame_markdown         0.1.1+8           0.2.0             updated with major changes
flame_network_assets   0.2.0+13          0.3.0             updated with major changes
flame_noise            0.2.0             0.3.0             updated with major changes
flame_oxygen           0.1.9+8           0.2.0             updated with major changes
flame_spine            0.1.1+10          0.2.0             updated with major changes
flame_splash_screen    0.2.0             0.3.0             updated with major changes
jenny                  1.2.1             1.3.0             updated with minor changes
```
2024-02-17 11:23:14 +01:00
..
2023-06-20 09:36:15 -03:00
2023-09-12 13:52:56 +00:00
2021-10-04 09:25:22 -03:00
2021-10-04 09:25:22 -03:00

flame

Offers a simple and natural way to use flutter_bloc inside Flame.


flame_bloc 🔥🧱

flame_bloc offers a simple and natural (as in similar to flutter_bloc) way to use blocs and cubits inside a FlameGame.

For a migration guide from the previous API to the current one, check this article.

How to use

Lets assume we have a bloc that handles player inventory, first we need to make it available to our components.

We can do that by using FlameBlocProvider component:

class MyGame extends FlameGame {
  @override
  Future<void> onLoad() async {
    await add(
      FlameBlocProvider<PlayerInventoryBloc, PlayerInventoryState>(
        create: () => PlayerInventoryBloc(),
        children: [
          Player(),
          // ...
        ],
      ),
    );
  }
}

With the above changes, the Player component will now have access to our bloc.

If more than one bloc needs to be provided, FlameMultiBlocProvider can be used in a similar fashion:

class MyGame extends FlameGame {
  @override
  Future<void> onLoad() async {
    await add(
      FlameMultiBlocProvider(
        providers: [
          FlameBlocProvider<PlayerInventoryBloc, PlayerInventoryState>(
            create: () => PlayerInventoryBloc(),
          ),
          FlameBlocProvider<PlayerStatsBloc, PlayerStatsState>(
            create: () => PlayerStatsBloc(),
          ),
        ],
        children: [
          Player(),
          // ...
        ],
      ),
    );
  }
}

Listening to states changes at the component level can be done with two approaches:

By using FlameBlocListener component:

class Player extends PositionComponent {
  @override
  Future<void> onLoad() async {
    await add(
      FlameBlocListener<PlayerInventoryBloc, PlayerInventoryState>(
        listener: (state) {
          updateGear(state);
        },
      ),
    );
  }
}

Or by using FlameBlocListenable mixin:

class Player extends PositionComponent
  with FlameBlocListenable<PlayerInventoryBloc, PlayerInventoryState> {

  @override
  void onNewState(state) {
    updateGear(state);
  }
}

If all your component need is to simply access a bloc, the FlameBlocReader mixin can be applied to a component:

class Player extends PositionComponent
  with FlameBlocReader<PlayerStatsBloc, PlayerStatsState> {

  void takeHit() {
    bloc.add(const PlayerDamaged());
  }
}

Note that one limitation of the mixin is that it can access only a single bloc.

For a full example, check the example folder