官术网_书友最值得收藏!

Timing the presentation

The presenter will want to keep to their allotted time slot. We will include a timer in the editor to aid in rehearsal.

Introducing the Stopwatch class

The Stopwatch class (from dart:core) provides much of the functionality needed for this feature, as shown in this small command-line application:

main() {
  Stopwatch sw = new Stopwatch();
  sw.start();
  print(sw.elapsed);
  sw.stop();
  print(sw.elapsed);
}

The elapsed property can be checked at any time to give the current duration. This is a very useful class as, for example, it can be used to compare different functions to see which is the fastest.

Implementing the presentation timer

The clock will be stopped and started with a single button handled by the toggleTimer method. A recurring timer will update the duration text on the screen, as follows:

Implementing the presentation timer

If the timer is running, the update Timer and the Stopwatch in the field slidesTime is stopped. No update to the display is required as the user will need to see the final time:

void toggleTimer(Event event) {
  if (slidesTime.isRunning) {
    slidesTime.stop();
    updateTimer.cancel();
  } else {
    updateTimer = new Timer.periodic(new Duration(seconds: 1), (timer) {
      String seconds = (slidesTime.elapsed.inSeconds % 60).toString();
      seconds = seconds.padLeft(2, "0");
      timerDisplay.text = "${slidesTime.elapsed.inMinutes}:$seconds";
    });

    slidesTime
    ..reset()
    ..start();
  }
}

The Stopwatch class provides properties for retrieving the elapsed time in minutes and seconds. To format this to minutes and seconds, the seconds portion is determined with the modular division operator % and padded with the string function padLeft.

Dart's string interpolation feature is used to build the final string, and as the elapsed and inMinutes properties are being accessed, the {} brackets are required so that the single value is returned.

主站蜘蛛池模板: 麦盖提县| 辽源市| 天长市| 那坡县| 奉贤区| 巴青县| 玉田县| 阿坝县| 裕民县| 梅河口市| 临泽县| 诸暨市| 平陆县| 荆门市| 庄河市| 屏东市| 边坝县| 阳西县| 望奎县| 大田县| 漠河县| 嘉峪关市| 岢岚县| 成都市| 雅安市| 济南市| 本溪市| 定日县| 舒城县| 平远县| 岑溪市| 东阿县| 嵊州市| 汶川县| 吉隆县| 公主岭市| 邵东县| 东阳市| 古蔺县| 炉霍县| 太原市|