Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
Pruned the API. Moved RiverpodGameWidget out of the library and into …
Browse files Browse the repository at this point in the history
…the example definition.
  • Loading branch information
markvideon committed Jan 7, 2023
1 parent a1714cb commit 12fddeb
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 73 deletions.
45 changes: 43 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'package:flame/components.dart' hide Timer;
import 'package:flame/game.dart';
import 'package:flame_riverpod/flame_riverpod.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
Expand Down Expand Up @@ -54,8 +55,10 @@ class FlutterCountingComponent extends ConsumerWidget {
}
}

class RefExampleGame extends RiverpodAwareFlameGame {
RefExampleGame(super.ref);
class RefExampleGame extends FlameGame with HasComponentRef {
RefExampleGame(WidgetRef ref) {
this.ref = ComponentRef(ref);
}

@override
onLoad() async {
Expand All @@ -65,6 +68,44 @@ class RefExampleGame extends RiverpodAwareFlameGame {
}
}

class RiverpodGameWidget extends ConsumerStatefulWidget {
const RiverpodGameWidget.readFromProvider({super.key})
: uninitialisedGame = null;
const RiverpodGameWidget.initialiseWithGame(
{super.key, required this.uninitialisedGame});

final HasComponentRef Function(WidgetRef ref)? uninitialisedGame;

@override
ConsumerState<RiverpodGameWidget> createState() => _RiverpodGameWidgetState();
}

class _RiverpodGameWidgetState extends ConsumerState<RiverpodGameWidget> {
@override
void initState() {
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
if (widget.uninitialisedGame is HasComponentRef Function(
WidgetRef ref)) {
ref
.read(riverpodAwareGameProvider.notifier)
.set(widget.uninitialisedGame!(ref));
}
});
super.initState();
}

@override
Widget build(BuildContext context) {
final game = ref.watch(riverpodAwareGameProvider);

if (game is! Game) {
return Container();
}

return GameWidget(game: game!);
}
}

class RiverpodAwareTextComponent extends PositionComponent
with RiverpodComponentMixin {
// ComponentRef is a wrapper around WidgetRef and exposes
Expand Down
2 changes: 0 additions & 2 deletions lib/flame_riverpod.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@ library flame_riverpod;

export 'src/component_ref.dart';
export 'src/game_provider.dart';
export 'src/riverpod_aware_game.dart';
export 'src/riverpod_component_mixin.dart';
export 'src/riverpod_game_widget.dart';
5 changes: 5 additions & 0 deletions lib/src/component_ref.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import 'package:flame/game.dart' show Game;
import 'package:flutter/cupertino.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

mixin HasComponentRef on Game {
late ComponentRef ref;
}

/// A wrapper around [WidgetRef]. Methods that correspond to interactions with
/// the widget tree, such as [WidgetRef.watch] are not exposed.
class ComponentRef {
Expand Down
11 changes: 6 additions & 5 deletions lib/src/game_provider.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import 'package:flame_riverpod/src/riverpod_aware_game.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

/// Simple provider that returns a [RiverpodAwareGame] instance.
import 'component_ref.dart';

/// Simple provider that returns a Riverpod-Aware Game instance.
final riverpodAwareGameProvider =
StateNotifierProvider<RiverpodAwareGameNotifier, HasComponentReference?>(
StateNotifierProvider<RiverpodAwareGameNotifier, HasComponentRef?>(
(ref) {
return RiverpodAwareGameNotifier();
});

/// Simple [StateNotifier] that holds the current [RiverpodAwareGame] instance.
class RiverpodAwareGameNotifier extends StateNotifier<HasComponentReference?> {
class RiverpodAwareGameNotifier extends StateNotifier<HasComponentRef?> {
RiverpodAwareGameNotifier() : super(null);

set(HasComponentReference candidate) {
set(HasComponentRef candidate) {
state = candidate;
}
}
20 changes: 0 additions & 20 deletions lib/src/riverpod_aware_game.dart

This file was deleted.

44 changes: 0 additions & 44 deletions lib/src/riverpod_game_widget.dart

This file was deleted.

0 comments on commit 12fddeb

Please sign in to comment.