Sleep Tracker

Loading preview

Starting the Flutter app

A showcase of Flutter's sleep tracking functionality

Get the source

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

maxHours and avgHours are derived from _sleepData in build, and each weekly bar is a FractionallySizedBox scaled by h / (maxHours + 1) to leave headroom. Last Night passes the final _sleepData entry through _formatHours, so it reads 8h 30m. A SingleChildScrollView keeps everything reachable on short screens. The Set sleep goal tile has no onTap.

Adapting it

  • Feed _sleepData from HealthKit or Health Connect, and handle an empty list, because reduce throws on one.
  • _sleepData.last assumes the list runs oldest to newest, so sort it before rendering.