fix: RouterComponent should be on top (#3524)

To get the `WorldRoute` and `world` to work well with other components,
the `RouterComponent` needs to be on top.

This also fixes a small deprecation in the index.html file for the
examples.
This commit is contained in:
Lukas Klingsbo
2025-03-13 15:18:13 +13:00
committed by GitHub
parent 835c40fc6b
commit aa52a2a58d
3 changed files with 80 additions and 45 deletions

View File

@ -4,6 +4,7 @@ import 'package:flame/events.dart';
import 'package:flame/game.dart';
import 'package:flame/geometry.dart';
import 'package:flame/rendering.dart';
import 'package:flutter/material.dart' show Colors;
import 'package:flutter/rendering.dart';
class RouterWorldExample extends FlameGame {
@ -328,52 +329,55 @@ class Level2Page extends DecoratedWorld with HasGameReference {
}
}
class Planet extends PositionComponent {
class Planet extends CircleComponent
with TapCallbacks, HasGameReference<RouterWorldExample> {
Planet({
required this.radius,
required this.color,
super.position,
required super.radius,
required Color color,
super.children,
}) : _paint = Paint()..color = color;
final double radius;
final Color color;
final Paint _paint;
}) : super(paint: Paint()..color = color, anchor: Anchor.center);
@override
void render(Canvas canvas) {
canvas.drawCircle(Offset.zero, radius, _paint);
void onTapDown(TapDownEvent event) {
game.router.pushAndWait(YesNoDialog()).then((shouldRemove) {
if (shouldRemove) {
removeFromParent();
}
});
}
}
class Orbit extends PositionComponent {
class Orbit extends CircleComponent {
Orbit({
required this.radius,
required super.radius,
required this.planet,
required this.revolutionPeriod,
double initialAngle = 0,
}) : _paint = Paint()
}) : super(
children: [planet],
anchor: Anchor.center,
paint: Paint()
..style = PaintingStyle.stroke
..color = const Color(0x888888aa),
_angle = initialAngle {
add(planet);
}
);
final double radius;
final double revolutionPeriod;
final Planet planet;
final Paint _paint;
double _angle;
@override
void render(Canvas canvas) {
canvas.drawCircle(Offset.zero, radius, _paint);
Future<void> onLoad() async {
super.onLoad();
if (parent is Planet) {
position = Vector2.all((parent! as Planet).radius);
}
@override
void update(double dt) {
_angle += dt / revolutionPeriod * tau;
planet.position = Vector2(radius, 0)..rotate(_angle);
planet.position.x = size.x;
planet.position.y = size.y / 2;
planet.add(
RotateAroundEffect(
tau,
EffectController(duration: revolutionPeriod, infinite: true),
center: size / 2,
),
);
}
}
@ -441,3 +445,43 @@ class DecoratedWorld extends World with HasTimeScale {
}
}
}
class YesNoDialog extends ValueRoute<bool> {
YesNoDialog() : super(value: false);
@override
Component build() {
final gameSize = findGame()!.size;
const margin = 10.0;
final boxSize = Vector2(350, 100);
return PositionComponent(
position: Vector2(gameSize.x / 2, margin),
size: boxSize,
anchor: Anchor.topCenter,
children: [
RectangleComponent(
size: boxSize,
paint: Paint()..color = const Color(0xFFAA0000),
),
TextComponent(
position: Vector2.all(margin),
text: 'Remove the planet?',
),
RoundedButton(
text: 'Yes',
action: () => completeWith(true),
color: Colors.green,
borderColor: Colors.white,
position: Vector2(boxSize.x / 4, boxSize.y - 30),
),
RoundedButton(
text: 'No',
action: () => completeWith(false),
color: Colors.red,
borderColor: Colors.white,
position: Vector2(boxSize.x * 0.75, boxSize.y - 30),
),
],
);
}
}

View File

@ -19,16 +19,6 @@
<link rel="manifest" href="manifest.json">
</head>
<body>
<!-- This script installs service_worker.js to provide PWA functionality to
application. For more information, see:
https://developers.google.com/web/fundamentals/primers/service-workers -->
<script>
if ('serviceWorker' in navigator) {
window.addEventListener('load', function () {
navigator.serviceWorker.register('flutter_service_worker.js');
});
}
</script>
<script src="main.dart.js" type="application/javascript"></script>
<script src="flutter_bootstrap.js" async></script>
</body>
</html>

View File

@ -34,6 +34,7 @@ class RouterComponent extends Component {
Map<String, RouteFactory>? routeFactories,
this.onUnknownRoute,
super.key,
super.priority = 0x7fffffff,
}) : _routes = routes,
_routeFactories = routeFactories ?? {} {
routes.forEach((name, route) => route.name = name);