flutter_prayer_time_calculator 0.0.2
flutter_prayer_time_calculator: ^0.0.2 copied to clipboard
A Flutter package to calculate Islamic prayer times with customizable methods and location/timezone support.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter_prayer_time_calculator/flutter_prayer_time_calculator.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Prayer Times Example',
theme: ThemeData(primarySwatch: Colors.blue),
home: PrayerTimesExample(),
);
}
}
class PrayerTimesExample extends StatefulWidget {
@override
_PrayerTimesExampleState createState() => _PrayerTimesExampleState();
}
class _PrayerTimesExampleState extends State<PrayerTimesExample> {
late PrayerTimes prayerTimes;
Map<PrayerTime, String> times = {};
@override
void initState() {
super.initState();
prayerTimes = PrayerTimes(method: CalculationMethod.isna);
calculatePrayerTimes();
}
void calculatePrayerTimes() {
// Example: Calculate prayer times for Finland, Helsinki
times = prayerTimes.getTimes(
date: DateTime.now(),
latitude: 60.1699,
longitude: 24.9384,
timezone: 3,
format: TimeFormat.twelveHour,
method: CalculationMethod.mwl,
asrMethod: AsrMethod.standard,
highLatitudeMethod: HighLatitudeMethod.angleBased,
offsets: {
PrayerTime.fajr: 2, // 2 minutes offset for Fajr
PrayerTime.isha: -1, // 1 minute earlier for Isha
},
);
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Prayer Times'),
backgroundColor: Colors.green,
),
body: Container(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Prayer Times for Today',
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
SizedBox(height: 20),
...PrayerTime.values.map((prayerTime) {
if (times.containsKey(prayerTime)) {
return Card(
margin: EdgeInsets.symmetric(vertical: 4),
child: ListTile(
leading: Icon(
_getPrayerIcon(prayerTime),
color: Colors.green,
),
title: Text(
prayerTime.displayName,
style: TextStyle(fontWeight: FontWeight.w600),
),
trailing: Text(
times[prayerTime] ?? '',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Colors.blue[800],
),
),
),
);
}
return Container();
}).toList(),
SizedBox(height: 30),
Text(
'Calculation Method: ${prayerTimes.getMethod().name}',
style: TextStyle(fontSize: 16, fontStyle: FontStyle.italic),
),
],
),
),
);
}
IconData _getPrayerIcon(PrayerTime prayerTime) {
switch (prayerTime) {
case PrayerTime.fajr:
return Icons.wb_twilight;
case PrayerTime.sunrise:
return Icons.wb_sunny;
case PrayerTime.dhuhr:
return Icons.brightness_high;
case PrayerTime.asr:
return Icons.brightness_medium;
case PrayerTime.maghrib:
return Icons.brightness_low;
case PrayerTime.isha:
return Icons.brightness_2;
case PrayerTime.midnight:
return Icons.bedtime;
default:
return Icons.access_time;
}
}
}
// Simple usage examples:
void simpleUsageExamples() {
// Basic usage
PrayerTimes pt = PrayerTimes();
// Get prayer times for current location and date
Map<PrayerTime, String> times = pt.getTimes(
latitude: 43.6532, // Toronto latitude
longitude: -79.3832,
timezone: -4, // Toronto timezone
// Toronto longitude
);
print('Fajr: ${times[PrayerTime.fajr]}');
print('Dhuhr: ${times[PrayerTime.dhuhr]}');
print('Asr: ${times[PrayerTime.asr]}');
print('Maghrib: ${times[PrayerTime.maghrib]}');
print('Isha: ${times[PrayerTime.isha]}');
// Using specific method and parameters
Map<PrayerTime, String> customTimes = pt.getTimes(
date: DateTime(2024, 6, 15),
latitude: 21.4225, // Makkah latitude
longitude: 39.8262, // Makkah longitude
elevation: 277, // Makkah elevation
timezone: 3, // Saudi Arabia timezone
format: TimeFormat.twelveHour,
method: CalculationMethod.makkah,
asrMethod: AsrMethod.hanafi,
highLatitudeMethod: HighLatitudeMethod.angleBased,
offsets: {
PrayerTime.fajr: 5, // 5 minutes later
PrayerTime.isha: -10, // 10 minutes earlier
},
);
// Advanced usage with custom parameters
pt.adjust({'fajr': 18.5, 'isha': 17.0, 'maghrib': '5 min'});
// Set time offsets
pt.tune({PrayerTime.fajr: 2, PrayerTime.dhuhr: 1, PrayerTime.asr: -1});
// Get formatted time
String formattedTime = pt.getFormattedTime(12.5, TimeFormat.twelveHour, [
'AM',
'PM',
]);
print('Formatted time: $formattedTime'); // Output: 12:30 PM
}