Search⌘ K

Solution: Build a Stopwatch Using RxDart

Explore how to build a functional stopwatch app using the RxDart library within the Flutter BLoC architecture. Understand managing streams for time and running state, controlling the timer through start, pause, and stop functions, and integrating the StopwatchBloc with the user interface for real-time updates.

Let’s look at a possible solution to the stopwatch project using RxDart.

Creating the StopwatchBloc

The first step is to create the logic of the StopwatchBloc.

Starting the stopwatch

To start the stopwatch, you have to:

  1. Start a timer that calls the tick() function every second.

  2. Update the isRunning stream that the UI is listening to reflect that the stopwatch is now running.

Dart
void start() {
_timer = Timer.periodic(const Duration(seconds: 1), (_) => tick());
_isRunning.add(true);
}

Pausin

...