timings_of_deen 0.0.1 copy "timings_of_deen: ^0.0.1" to clipboard
timings_of_deen: ^0.0.1 copied to clipboard

Your comprehensive guide for daily Muslim practices. Access accurate prayer times, Qibla direction,and essential Islamic resources, all in one place..

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:timings_of_deen/timings_of_deen.dart'; // This imports your library
import 'package:timezone/data/latest.dart' as tz; // Using tz alias for timezone
import 'data/zilla_locations.dart'; // Make sure this file exists and contains data

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  tz.initializeTimeZones(); // Initialize timezone data
  runApp(const PrayerTimesApp());
}

class PrayerTimesApp extends StatelessWidget {
  const PrayerTimesApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Prayer Times',
      theme: ThemeData(primarySwatch: Colors.teal),
      home: const PrayerTimesPage(),
    );
  }
}

class PrayerTimesPage extends StatefulWidget {
  const PrayerTimesPage({super.key});

  @override
  PrayerTimesPageState createState() => PrayerTimesPageState();
}

class PrayerTimesPageState extends State<PrayerTimesPage> {
  late PrayerTimes _prayerTimes;
  late Coordinates _coordinates;
  late double _qiblaDirection;
  late double _madinaDirection;
  bool _loading = true;

  String _selectedDistrict = 'Dhaka'; // Initial selection
  String _selectedAsrMethod =
      'Shafi'; // Default Asr method to Standard for clarity
  late CalculationMethod
  _selectedCalculationMethod; // State variable for Calculation Method

  @override
  void initState() {
    super.initState();
    // Initialize _selectedCalculationMethod based on default for selected district
    _selectedCalculationMethod =
        countryDefaultMethods[_selectedDistrict] ??
        CalculationMethod.muslimWorldLeague;
    _calculatePrayerTimes();
  }

  void _calculatePrayerTimes() {
    // Find the coordinates for the selected district
    final selected = zillaLocations.firstWhere(
      (z) => z['name'] == _selectedDistrict,
      orElse: () => {
        'name': 'Dhaka',
        'latitude': 23.8103,
        'longitude': 90.4125,
      }, // Fallback to Dhaka
    );
    _coordinates = Coordinates(
      latitude: selected['latitude'],
      longitude: selected['longitude'],
    );

    final now = DateTime.now();

    final times = getPrayerTimesByCountry(
      country:
          _selectedDistrict, // Use selected district to potentially get correct timezone if your map is extensive
      latitude: _coordinates.latitude,
      longitude: _coordinates.longitude,
      date: now,
      useHanafiAsr: _selectedAsrMethod == 'Hanafi',
      method: _selectedCalculationMethod, // Pass the user-selected method
    );
    debugPrint("🌍 Calculating for $_selectedDistrict");

    debugPrint("🕓 Fajr: ${times.fajr}");
    debugPrint("🕓 Sunrise: ${times.sunrise}");
    debugPrint("🕓 Dhuhr: ${times.dhuhr}");
    debugPrint("🕓 Asr: ${times.asr}");
    debugPrint("🕓 Maghrib: ${times.maghrib}");
    debugPrint("🕓 Isha: ${times.isha}");

    setState(() {
      _prayerTimes = times;
      _qiblaDirection = qiblaDirection(_coordinates);
      _madinaDirection = madinaDirection(_coordinates);
      _loading = false;
    });
  }

  Widget _buildPrayerTimeRow(String name, DateTime time) {
    final formattedTime = DateFormat('hh:mm a').format(time);
    return ListTile(
      title: Text(name),
      trailing: Text(
        formattedTime,
        style: const TextStyle(fontWeight: FontWeight.bold),
      ),
    );
  }

  Widget _buildInfoCard(String title, String value, {IconData? icon}) {
    return Card(
      margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 6),
      child: ListTile(
        leading: Icon(icon ?? Icons.info_outline),
        title: Text(title),
        trailing: Text(value),
      ),
    );
  }

  BoxDecoration _dropdownDecoration() {
    return BoxDecoration(
      color: Colors.white,
      borderRadius: BorderRadius.circular(12.0),
      boxShadow: [
        BoxShadow(
          color: Colors.grey.withAlpha(20),
          spreadRadius: 1,
          blurRadius: 5,
          offset: const Offset(0, 3),
        ),
      ],
      border: Border.all(color: Colors.blueAccent, width: 1.5),
    );
  }

  @override
  Widget build(BuildContext context) {
    if (_loading) {
      return const Scaffold(body: Center(child: CircularProgressIndicator()));
    }

    final tahajjud = _prayerTimes.isha.subtract(const Duration(hours: 2));
    final sehri = _prayerTimes.fajr.subtract(const Duration(minutes: 10));
    final beforeFajr = _prayerTimes.fajr.subtract(const Duration(minutes: 5));
    final beforeMaghrib = _prayerTimes.maghrib.subtract(
      const Duration(minutes: 5),
    );

    return Scaffold(
      appBar: AppBar(
        title: Text('Prayer Times - $_selectedDistrict'),
        centerTitle: true,
      ),
      body: ListView(
        padding: const EdgeInsets.all(8),
        children: [
          // District Dropdown
          Padding(
            padding: const EdgeInsets.all(16.0),
            child: Container(
              decoration: _dropdownDecoration(),
              child: DropdownButtonHideUnderline(
                child: DropdownButton<String>(
                  isExpanded: true,
                  value: _selectedDistrict,
                  icon: const Icon(
                    Icons.arrow_drop_down_circle,
                    color: Colors.blueAccent,
                  ),
                  iconSize: 28,
                  elevation: 16,
                  style: const TextStyle(
                    color: Colors.deepPurple,
                    fontSize: 16,
                  ),
                  onChanged: (value) {
                    if (value != null) {
                      setState(() {
                        _selectedDistrict = value;
                        // Update calculation method default for new district if applicable, or let user override
                        _selectedCalculationMethod =
                            countryDefaultMethods[_selectedDistrict] ??
                            CalculationMethod.muslimWorldLeague;
                        _loading = true;
                      });
                      _calculatePrayerTimes();
                    }
                  },
                  items: zillaLocations.map<DropdownMenuItem<String>>((z) {
                    return DropdownMenuItem<String>(
                      value: z['name'],
                      child: Padding(
                        padding: const EdgeInsets.symmetric(
                          horizontal: 12.0,
                          vertical: 8.0,
                        ),
                        child: Text(
                          z['name'],
                          style: const TextStyle(color: Colors.black87),
                        ),
                      ),
                    );
                  }).toList(),
                  dropdownColor: Colors.blue.shade50,
                ),
              ),
            ),
          ),
          // Asr Method Dropdown
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 16.0),
            child: Container(
              decoration: _dropdownDecoration(),
              child: DropdownButtonHideUnderline(
                child: DropdownButton<String>(
                  isExpanded: true,
                  value: _selectedAsrMethod,
                  icon: const Icon(
                    Icons.access_time_rounded,
                    color: Colors.teal,
                  ),
                  iconSize: 26,
                  elevation: 10,
                  style: const TextStyle(color: Colors.teal, fontSize: 15),
                  onChanged: (value) {
                    if (value != null) {
                      setState(() {
                        _selectedAsrMethod = value;
                        _loading = true;
                      });
                      _calculatePrayerTimes();
                    }
                  },
                  items: ['Hanafi', 'Shafi'].map((method) {
                    return DropdownMenuItem<String>(
                      value: method,
                      child: Padding(
                        padding: const EdgeInsets.symmetric(
                          horizontal: 12.0,
                          vertical: 8.0,
                        ),
                        child: Text(method),
                      ),
                    );
                  }).toList(),
                  dropdownColor: Colors.teal.shade50,
                ),
              ),
            ),
          ),
          // Calculation Method Dropdown
          Padding(
            padding: const EdgeInsets.symmetric(
              horizontal: 16.0,
              vertical: 8.0,
            ),
            child: Container(
              decoration: _dropdownDecoration(), // Reusing the same decoration
              child: DropdownButtonHideUnderline(
                child: DropdownButton<CalculationMethod>(
                  // CORRECTED: Changed DropadownButton to DropdownButton
                  isExpanded: true,
                  value: _selectedCalculationMethod,
                  icon: const Icon(
                    Icons.menu_book,
                    color: Colors.deepOrange,
                  ), // New icon
                  iconSize: 26,
                  elevation: 10,
                  style: const TextStyle(
                    color: Colors.deepOrange,
                    fontSize: 15,
                  ),
                  onChanged: (value) {
                    if (value != null) {
                      setState(() {
                        _selectedCalculationMethod = value;
                        _loading = true;
                      });
                      _calculatePrayerTimes();
                    }
                  },
                  items: CalculationMethod.values.map((method) {
                    // Iterate through all enum values
                    return DropdownMenuItem<CalculationMethod>(
                      value: method,
                      child: Padding(
                        padding: const EdgeInsets.symmetric(
                          horizontal: 12.0,
                          vertical: 8.0,
                        ),
                        child: Text(
                          calculationMethodLabels[method]!,
                        ), // Use the labels map
                      ),
                    );
                  }).toList(),
                  dropdownColor:
                      Colors.deepOrange.shade50, // New dropdown color
                ),
              ),
            ),
          ),

          // Info Cards
          _buildInfoCard(
            "Date",
            DateFormat.yMMMMEEEEd().format(_prayerTimes.date),
            icon: Icons.calendar_today,
          ),
          _buildInfoCard(
            "Location",
            "$_selectedDistrict, Bangladesh",
            icon: Icons.location_on,
          ),
          _buildInfoCard(
            "Qibla Direction",
            "${_qiblaDirection.toStringAsFixed(2)}°",
            icon: Icons.explore,
          ),
          _buildInfoCard(
            "Madina Direction",
            "${_madinaDirection.toStringAsFixed(2)}°",
            icon: Icons.location_city,
          ),
          // Update the Method Info Card to reflect the user's selection
          _buildInfoCard(
            "Method",
            calculationMethodLabels[_selectedCalculationMethod]!,
            icon: Icons.calculate,
          ),
          const Divider(),

          // Prayer Times Rows
          _buildPrayerTimeRow("Tahajjud", tahajjud),
          _buildPrayerTimeRow("Sehri Ends", sehri),
          _buildPrayerTimeRow("Fajr", _prayerTimes.fajr),
          _buildPrayerTimeRow("Sunrise", _prayerTimes.sunrise),
          _buildPrayerTimeRow("Dhuhr", _prayerTimes.dhuhr),
          _buildPrayerTimeRow("Asr", _prayerTimes.asr),
          _buildPrayerTimeRow("Maghrib", _prayerTimes.maghrib),
          _buildPrayerTimeRow("Isha", _prayerTimes.isha),
          const Divider(),
          _buildPrayerTimeRow("Before Fajr (Sunnah)", beforeFajr),
          _buildPrayerTimeRow("Before Maghrib (Sunnah)", beforeMaghrib),
        ],
      ),
    );
  }
}
4
likes
150
points
20
downloads

Publisher

verified publisherrahulreza.com

Weekly Downloads

Your comprehensive guide for daily Muslim practices. Access accurate prayer times, Qibla direction,and essential Islamic resources, all in one place..

Homepage

Documentation

API reference

License

MIT (license)

Dependencies

flutter, intl, timezone

More

Packages that depend on timings_of_deen