Skip to main content ppwriters

#DartProjects

Project 8

Building a Countdown Timer in Dart

Hello again, Today, we’re diving into day 8 of our “30 Days of Dart Projects” series by building a “Countdown Timer” in Dart. Countdown Timer that will count down from a specified number of seconds. Imagine it as your personal digital hourglass but instead of sand, it uses seconds. You tell it when to start and it ticks away one second at a time, giving you updates until it reaches zero.

Complete code

Here’s the complete code of our Countdown Timer:

/// Countdown timer
/// 
import 'dart:async';
import 'dart:io';

void main() {
  print("Enter the countdown time in seconds:");
  int countdown = int.parse(stdin.readLineSync()!);

  print("Starting countdown...");
  
  Timer.periodic(Duration(seconds: 1), (Timer timer) {
    countdown--;
    
    if (countdown == 0) {
      print("Time's up!");
      timer.cancel();
    } else {
      print(countdown);
    }
  });
}

Code Breakdown

Importing Dart Libraries

import 'dart:async';
import 'dart:io';

First, we import the dart:async library to use Dart’s built-in Timer class. It’s like getting access to a stopwatch for our code. The dart:io library allows us to interact with the terminal for input/output operations - it’s like our gateway to communicate with the user.

Getting User Input

void main() {
  print("Enter the countdown time in seconds:");
  int countdown = int.parse(stdin.readLineSync()!);

In the main() function, we ask the user to enter the countdown time in seconds. Think of this as setting the time on your kitchen timer. The stdin.readLineSync() function waits for user input from the terminal. This input is a string, so we use int.parse() to convert this string to an integer.

Setting Up The Timer

print("Starting countdown...");
  
Timer.periodic(Duration(seconds: 1), (Timer timer) {
  countdown--;

With the countdown time set, we’re ready to start the timer. We create a periodic Timer that triggers a function every second. The countdown decreases (or “ticks”) by one second each time this function is called.

Running the Countdown

if (countdown == 0) {
  print("Time's up!");
  timer.cancel();
} else {
  print(countdown);
}

The timer continues ticking and updating the countdown. Imagine it like a second hand on a clock, moving one step every second. When the countdown reaches zero, the timer stops (we “cancel” the timer), and we print a “Time’s up!” message.

Wrapping Up

And that’s it! You’ve successfully created a countdown timer in Dart. This simple but powerful tool can be used in a variety of applications, from creating time-management tools to building more complex games. Keep practicing, and soon you’ll be building more advanced projects in no time!

Join us tomorrow as we embark on another exciting Dart project. Stay tuned and happy coding!

Enjoying? Tell your friends.

#DartProjects

Learn the basics of Dart by building projects. Learn to build various projects step by step using Dart along the way brush up your Dart programming skills.

back to Dart projects

Join our community on Discord to connect with fellow learners, share your progress, and get help with any questions you may have throughout the #DartProject challenge. Join now and get started on your journey to mastering Dart!

join now