【问题标题】:Handle events in DART在 DART 中处理事件
【发布时间】:2012-04-17 18:36:39
【问题描述】:

我是 DART 新手。我阅读了语言概述并检查了 DART 编辑器中的示例代码。到目前为止,我找不到如何处理 DART 中的事件。例如onclick="call_dart_method()".

我们如何处理 DART 中的事件?

【问题讨论】:

标签: javascript dart onclick dom-events


【解决方案1】:

这不是你在 Dart 上的做法 检查这里,在事件部分下: http://www.dartlang.org/articles/improving-the-dom/

elem.onClick.listen(
    (event) => print('click!'));

【讨论】:

    【解决方案2】:

    此外,您可能会发现能够选择性地声明我们的变量类型使得在 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();
    }
    

    【讨论】:

    • 在哪一行有演员表?
    • 好点 - 我应该说排版。我想我最初想到的是var screen,后来又想到了(screen as CanvasRenderingContext2D)..etc.。
    猜你喜欢
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 2016-12-23
    • 2019-06-26
    • 2020-01-15
    • 2019-02-17
    • 2010-11-01
    • 2014-09-29
    相关资源
    最近更新 更多