Workout Timer
Loading preview
Starting the Flutter app
A showcase of Flutter's workout timer functionality
import 'dart:async';
import 'package:flutter/material.dart';
class WorkoutTimer extends StatefulWidget {
const WorkoutTimer({super.key});
@override
State<WorkoutTimer> createState() => _WorkoutTimerState();
}
class _WorkoutTimerState extends State<WorkoutTimer> {
static const _totalSeconds = 300;
int _remaining = _totalSeconds;
bool _isRunning = false;
Timer? _timer;
String get _display {
final m = _remaining ~/ 60;
final s = _remaining % 60;
return '${m.toString().padLeft(2, '0')}:${s.toString().padLeft(2, '0')}';
}
void _startTimer() {
_timer?.cancel();
_timer = Timer.periodic(const Duration(seconds: 1), (_) {
if (_remaining > 0) {
setState(() => _remaining--);
} else {
_timer?.cancel();
setState(() => _isRunning = false);
}
});
}
void _stopTimer() {
_timer?.cancel();
}
@override
void dispose() {
_timer?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Workout Timer')),
body: Padding(
padding: const EdgeInsets.all(24),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('HIIT Session', style: Theme.of(context).textTheme.titleLarge),
const SizedBox(height: 32),
SizedBox(
width: 200,
height: 200,
child: CustomPaint(
painter: _TimerRingPainter(
progress: 1 - (_remaining / _totalSeconds),
),
child: Center(
child: Text(_display,
style: Theme.of(context).textTheme.displayMedium),
),
),
),
const SizedBox(height: 32),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton.filled(
tooltip: _isRunning ? 'Pause workout' : 'Start workout',
onPressed: () {
setState(() {
if (_isRunning) {
_isRunning = false;
_stopTimer();
} else {
_isRunning = true;
_startTimer();
}
});
},
icon: Icon(_isRunning ? Icons.pause : Icons.play_arrow),
),
const SizedBox(width: 16),
IconButton.outlined(
tooltip: 'Reset timer',
onPressed: () {
_stopTimer();
setState(() {
_remaining = _totalSeconds;
_isRunning = false;
});
},
icon: const Icon(Icons.replay),
),
],
),
],
),
),
);
}
}
class _TimerRingPainter extends CustomPainter {
final double progress;
_TimerRingPainter({required this.progress});
@override
void paint(Canvas canvas, Size size) {
final center = Offset(size.width / 2, size.height / 2);
final radius = size.width / 2 - 8;
final rect = Rect.fromCircle(center: center, radius: radius);
canvas.drawCircle(
center,
radius,
Paint()
..color = Colors.grey.withValues(alpha: 0.2)
..style = PaintingStyle.stroke
..strokeWidth = 8,
);
canvas.drawArc(
rect,
-1.5708,
2 * 3.14159 * progress,
false,
Paint()
..color = Colors.green
..style = PaintingStyle.stroke
..strokeWidth = 8
..strokeCap = StrokeCap.round,
);
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
}
Add it to your project
Copies the Dart source into your app, so you own it. Flutter SDK only, no extra packages.
First time? Run dart pub global activate fluttertemplates_dev, or read the CLI guide.
How it works
The countdown here is real. Timer.periodic decrements _remaining from _totalSeconds, _display formats it as mm:ss, and _TimerRingPainter fills the ring from elapsed time. _startTimer cancels any earlier timer and dispose cleans up. Despite the HIIT Session label there are no intervals, just one five-minute countdown.
Adapting it
- Change
_totalSeconds, or pass it through theWorkoutTimerconstructor. - For intervals, keep a list of work and rest durations and advance to the next when
_remaininghits zero in_startTimer. - Counting ticks can drift, so store an end
DateTimeand derive_remainingfrom it if accuracy matters.