mirror of
https://github.com/flame-engine/flame.git
synced 2025-10-30 00:17:20 +08:00
I was playing around with the rule avoid-global-state While I don't think we should enable it, because we do have several totally legitimate cases of what the rule considers global state, it did help me find any current cases where the things just should be final constants. So this PR will mark semantically final variables as final (or const) proper, exclusively on examples (no violations on actual src code are legit).
106 lines
2.6 KiB
Dart
106 lines
2.6 KiB
Dart
import 'package:flame/components.dart';
|
|
import 'package:flame/events.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 const 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)));
|
|
}
|
|
}
|