Handle events in DART

南笙酒味 提交于 2019-12-05 07:21:29
chenglou

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!'));

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();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!