user3505901
user3505901

Reputation: 408

Mouse movement, while mouse down not working in Dart?

How do you get mouse movements but only when you're have clicked.

I know this is how you track whether or not the mouse is clicked:

canvas.onMouseDown

and this is how you track mouse movement canvas.onMouseMove

so I tried doing this:

//when the mouse is clicked
canvas.onMouseDown.listen((onData){
print("Mouse is down!" + "(" + onData.client.x.toString() + ", " + onData.client.y.toString() + ")");

//check where it's being dragged
canvas.onMouseMove.listen((onData){
  mouseMovementsDown(onData.client.x, onData.client.y);
});

});

But the problem is it doesn't stop once I stop clicking. I want to track the movement so long as the mouse is down and once the mouse is no longer down, I want to stop tracking the mouse's movement. Can anyone help me? For some reason this thing continues reporting canvas.onMouseMove even after I've put the mouse up.

I also tried this:

if(canvas.onMouseDown.listen(onData) == true) 

But apparently that's not how things work in dart >.>;

I read more about streams on the dart api docs, can someone explain to me how close is used? I think it said that's how you stop getting input from a mouse event, but how is that done I don't really understand how the code would be written to stop the canvas.onMouseMove.listen stream when canvas.onMouseUp happens.

Upvotes: 0

Views: 329

Answers (1)

Pixel Elephant
Pixel Elephant

Reputation: 21383

You can store a reference to the mouseMove StreamSubscription and cancel it on mouseUp:

canvas.onMouseDown.listen((onData) {

  StreamSubscription mouseMoveStream = canvas.onMouseMove.listen((onData) {
    // Do things...
  });

  canvas.onMouseUp.listen((onData) => mouseMoveStream.cancel());

});

Upvotes: 1

Related Questions