Files
flame/doc/examples/gestures/lib/main_draggables.dart
Renan ccee9a466b Move files to src and comply with the dart package layout convention (#621)
* 👌 Use `Offset` type directly in `JoystickAction.update` calculations (#631)

* Move files to src and comply with the dart package layout convention

* Fixing widgets example

Co-authored-by: Serge Matveenko <lig@countzero.co>
Co-authored-by: Erick Zanardo <erickzanardoo@gmail.com>
2021-01-20 09:05:43 -03:00

59 lines
1.4 KiB
Dart

import 'package:flame/components.dart';
import 'package:flame/extensions.dart';
import 'package:flame/gestures.dart';
import 'package:flutter/material.dart' hide Draggable;
import 'package:flame/game.dart';
void main() {
final widget = Container(
padding: const EdgeInsets.all(0),
color: const Color(0xFFA9A9A9),
child: GameWidget(
game: MyGame(),
),
);
runApp(widget);
}
class DraggableSquare extends PositionComponent with Draggable {
@override
bool debugMode = true;
bool _isDragging = false;
DraggableSquare({Vector2 position}) {
size = Vector2.all(100);
this.position = position ?? Vector2.all(100);
}
@override
void update(double dt) {
super.update(dt);
debugColor = _isDragging ? Colors.greenAccent : Colors.purple;
}
Vector2 dragDeltaPosition;
@override
bool onReceiveDrag(DragEvent event) {
event.onUpdate = (DragUpdateDetails details) {
if (!_isDragging) {
_isDragging = true;
dragDeltaPosition =
event.initialPosition.toVector2() - position.clone();
}
position = details.localPosition.toVector2() - dragDeltaPosition;
};
event.onEnd = (DragEndDetails details) {
_isDragging = false;
};
return true;
}
}
class MyGame extends BaseGame with HasDraggableComponents {
MyGame() {
add(DraggableSquare()..anchor = Anchor.topLeft);
add(DraggableSquare()..y = 350);
}
}