Search code examples
dartdart-polymer

Dart language: build a clock (with date and time)


I would like to know how can I build a clock (date and current time) using Dart. I'll be binding the resulting observable string (so it refreshes automatically) to an HTML Polymer element.

I thought about using a timer to refresh the string using DateTime.now(), but this seems to be a workaround, not the best solution.

I just need to know how to code the dart file, since the HTML and Polymer elements are already configured.


Solution

  • You might take a look at this example
    https://github.com/bwu-dart/polymer_ui_elements/blob/master/example/polymer_ui_clock.html
    https://github.com/bwu-dart/polymer_ui_elements/blob/master/lib/polymer_ui_clock/polymer_ui_clock.dart It needs a little tweaking to work with the current Polymer version.

    var oneSecond = new Duration(seconds: 1);
    _timer = new Timer.periodic(oneSecond, updateTime);
    

    updateTime is a method called every second.