Build Offline Prayer Times in Flutter with Adhan
ملخص
Pixel shares a guide to building offline prayer times in Flutter using the pure Dart adhan package. It covers solar calculations from location and date, community-specific calculation methods, a live countdown to the next prayer, and on-time Adhan notifications—useful features for reliable Islamic apps, even without internet access.
DEV Community
Build Prayer Times in Flutter
When I built Bayaan, an Islamic lifestyle app with prayer times, Adhan notifications, daily reminders and prophetic routines, the prayer times screen became the screen users opened every day. Bayaan runs on iOS and Android, is built with Flutter and Dart, and pairs Firebase and REST APIs with local notifications — but the prayer times themselves never touch a server. You can see the finished app in the Bayaan case study. This guide shows how to build the same feature in your own Flutter app: calculate the five daily prayer times on-device, let the user pick the calculation method their community follows, show a live countdown to the next prayer, and schedule Adhan notifications that fire on time. What are prayer times and how are they calculated? Prayer times are defined by the position of the sun, not by a timetable you download. Fajr begins at dawn, when the sun is a set number of degrees below the horizon; Dhuhr begins just after the sun passes its highest point; Asr begins when the shadow of an object reaches a set length; Maghrib begins at sunset; and Isha begins when the twilight has faded. Because every one of those moments is a solar event, the times can be computed from three inputs — latitude, longitude and date — using high-precision astronomical equations. The adhan package is a pure Dart library that does exactly this, with no dependencies and no network calls, and its equations come from the book Astronomical Algorithms by Jean Meeus. That makes prayer times a perfect offline feature: the app works in airplane mode, in a basement masjid with no signal, and on a phone that has never had an account. How do you calculate prayer times with the adhan package? You calculate prayer times with the adhan package by giving it three things: where the user is, what day it is, and which calculation method to use. Coordinates takes latitude first and longitude second, and the calculation parameters come from a named method instead of being built by hand. The PrayerTimes object then holds Fajr, Sunrise, Dhuhr, Asr, Maghrib and Isha as DateTime values, plus sunset and the wraparound helpers ishaBefore and fajrAfter. Pass the device UTC offset so the returned times are local wall-clock times, and use PrayerTimes.today as a shortcut when you only need the current day. The madhab setting matters too: it changes the Asr shadow length, with Hanafi Asr later than Shafi Asr, so expose it in settings alongside the method. import 'package:adhan/adhan.dart';
PrayerTimes todaysPrayerTimes({
required double latitude,
required double longitude,
required Duration utcOffset,
}) {
final coordinates = Coordinates(latitude, longitude);
final params = CalculationMethod.karachi.getParameters();
params.madhab = Madhab.shafi;
return PrayerTimes(
coordinates,
DateComponents.from(DateTime.now()),
params,
utcOffset: utcOffset,
);
}
One detail trips up almost every first implementation: the DateComponents must be the local calendar date, not a UTC date, or the times land a day out near midnight. Build it from DateTime.now() in local time and you are safe. If a user reports times that are exactly an hour off, the cause is almost always the UTC offset — daylight saving changed and the stored offset did not — so read the offset fresh from the device instead of caching it. Which calculation method should you use? There is no single correct calculation method, and that is the most important product decision in a prayer times app. Each method is a preset of Fajr and Isha angles and minute adjustments published by a religious authority, and different communities follow different ones. The table below lists the common presets from the adhan package documentation — use it to offer the choice, not to pick for the user. Method Fajr angle Isha angle Commonly used in Muslim World League 18 degrees 17 degrees Europe, Far East University of Islamic Sciences, Karachi 18 degrees 18 degrees Pakistan, Bangladesh, Afghanistan Islamic S...

Powered by Readybot.io