Files
flame/examples/lib/stories/components/components_notifier_provider_example.dart
Lukas Klingsbo b5bdf4ec17 feat!: The HasTappableComponents mixin is no longer needed (#2450)
This PR is the second in a series of refactors that aim to simplify event handling in Flame. The approach is as follows:

    Added the MultiTapDispatcher component, which contains the logic that used to be within the HasTappableComponents mixin. This component is internal; it mounts to a FlameGame directly, and ensures that it is a singleton.
    Whenever any TapCallbacks component is added to a game, it automatically adds the MultiTapDispatcher component (unless there is already one), which in turn registers a tap gesture detector with GestureDetectorBuilder and rebuilds the game widget.

The end result is that now in order to make a component tappable you only need to add the TapCallbacks mixin to that component, everything else will be handled by the framework.

Consequently, the HasTappableComponents mixin is now empty and marked as deprecated.
2023-04-02 16:52:57 +00:00

106 lines
2.6 KiB
Dart

import 'package:flame/components.dart';
import 'package:flame/experimental.dart';
import 'package:flame/game.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class ComponentsNotifierProviderExampleWidget extends StatefulWidget {
const ComponentsNotifierProviderExampleWidget({super.key});
static String description = '''
Similar to the Components Notifier example, but uses provider
instead of the built in ComponentsNotifierBuilder widget.
''';
@override
State<ComponentsNotifierProviderExampleWidget> createState() =>
_ComponentsNotifierProviderExampleWidgetState();
}
class _ComponentsNotifierProviderExampleWidgetState
extends State<ComponentsNotifierProviderExampleWidget> {
@override
void initState() {
super.initState();
game = ComponentNotifierExample();
}
late final ComponentNotifierExample game;
@override
Widget build(BuildContext context) {
return Scaffold(
body: MultiProvider(
providers: [
Provider<ComponentNotifierExample>.value(value: game),
ChangeNotifierProvider<ComponentsNotifier<Enemy>>(
create: (_) => game.componentsNotifier<Enemy>(),
),
],
child: Stack(
children: [
Positioned.fill(
child: GameWidget(game: game),
),
const Positioned(
left: 16,
top: 16,
child: GameHud(),
),
],
),
),
);
}
}
class GameHud extends StatelessWidget {
const GameHud({super.key});
@override
Widget build(BuildContext context) {
final enemies = context.watch<ComponentsNotifier<Enemy>>().components;
return Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: enemies.isEmpty
? ElevatedButton(
child: const Text('Play again'),
onPressed: () {
context.read<ComponentNotifierExample>().replay();
},
)
: Text('Remaining enemies: ${enemies.length}'),
),
);
}
}
class Enemy extends CircleComponent with TapCallbacks, Notifier {
Enemy({super.position})
: super(
radius: 20,
paint: Paint()..color = const Color(0xFFFF0000),
);
@override
void onTapUp(_) {
removeFromParent();
}
}
class ComponentNotifierExample extends FlameGame {
@override
Future<void> onLoad() async {
replay();
}
void replay() {
add(Enemy(position: Vector2(100, 100)));
add(Enemy(position: Vector2(200, 100)));
add(Enemy(position: Vector2(300, 100)));
}
}