7

I am new to DART. I read the language overview and checked example code in DART editor. So far I could not find how to handle events in DART. For e.g. onclick="call_dart_method()".

How can we handle events in DART?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
18bytes
  • 5,951
  • 7
  • 42
  • 69

2 Answers2

5

That's not how you do it on Dart Check here, under the section Events: http://www.dartlang.org/articles/improving-the-dom/

elem.onClick.listen(
    (event) => print('click!'));
Fox32
  • 13,126
  • 9
  • 50
  • 71
chenglou
  • 3,640
  • 2
  • 25
  • 33
4

Also, you might find that being able to optionally declare our variable types makes working with events in Dart bliss.

import 'dart:html';
import 'dart:math';

class MyApplication {
  MyApplication() {
    CanvasElement screenCanvas;
    CanvasRenderingContext2D screen;
    final int WIDTH = 400, HEIGHT = 300;

    Random rand = new Random();
    screenCanvas = new CanvasElement();
    screenCanvas
      ..width = WIDTH
      ..height = HEIGHT
      ..style.border = 'solid black 1px';

    screen = screenCanvas.getContext('2d');
    document.body.nodes.add(screenCanvas);
    screenCanvas.onClick.listen((MouseEvent me) {
      int
          r = rand.nextInt(256),
          g = rand.nextInt(256),
          b = rand.nextInt(256);
      double a = rand.nextDouble();
      screen
        ..save()
        ..translate(me.offsetX, me.offsetY)
        ..rotate(rand.nextDouble() * PI)
        ..fillStyle = 'rgba($r,$g,$b,$a)'
        ..fillRect(-25, -25, 50, 50)
        ..restore();
    });
  }
}

void main() {
  new MyApplication();
}
Richard Ambler
  • 4,816
  • 2
  • 21
  • 38