I'am trying to program some animations but I am having troubles to understand how the Futures work on Dart.
The main problem is that the animation itself is an asynchronous process and, if I try to chain several points, they pile up and the sprite moves directly to the last point. All the examples I've seen work when the function returns a value but this is not the case when dealing with animations.
I'd like to be able to detect then the animation is finished, in order to trigger the next movement, but I've not been really successful so far.
Here is my code:
class MovingThing {
String name;
DivElement sprite = new DivElement();
MovingThing(this.name){
sprite.id = name;
sprite.text = name;
sprite.style..width = "30px"
..height = "30px"
..border = "1px solid black"
..position = "absolute"
..transition = "all 2000ms ease-in-out";
querySelector("body").children.add(sprite);
}
Future move(Point p) {
sprite.style..top = p.y.toString() + "px"
..left = p.x.toString() + "px";
return Future.wait([sprite.onTransitionEnd.first]);
}
}
main () {
List<Point> points = [
new Point(20, 20)
, new Point(200, 20)
, new Point(20, 200)
, new Point(100, 100)
];
MovingThing mt = new MovingThing("MT");
Future.forEach(points, (Point p) => mt.move(p));
}