hadith_nawawi 0.0.4
hadith_nawawi: ^0.0.4 copied to clipboard
Flutter package for accessing and displaying the forty hadiths of Imam Nawawi with UI components and search functionality.
import 'package:flutter/material.dart';
import 'package:hadith_nawawi/hadith_nawawi.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Hadith Nawawi Example',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.green),
useMaterial3: true,
),
home: const HadithHomePage(),
);
}
}
class HadithHomePage extends StatefulWidget {
const HadithHomePage({super.key});
@override
State<HadithHomePage> createState() => _HadithHomePageState();
}
class _HadithHomePageState extends State<HadithHomePage> {
bool _isLoading = true;
String _errorMessage = '';
List<Hadith> _hadiths = [];
List<Hadith> _filteredHadiths = [];
final TextEditingController _searchController = TextEditingController();
@override
void initState() {
super.initState();
_loadHadiths();
}
@override
void dispose() {
_searchController.dispose();
super.dispose();
}
Future<void> _loadHadiths() async {
try {
setState(() {
_isLoading = true;
_errorMessage = '';
});
await HadithNawawi.loadHadiths();
setState(() {
_hadiths = HadithNawawi.getHadiths();
_filteredHadiths = _hadiths;
_isLoading = false;
});
} catch (e) {
setState(() {
_errorMessage = 'Failed to load hadiths: $e';
_isLoading = false;
});
}
}
void _filterHadiths(String query) {
setState(() {
if (query.isEmpty) {
_filteredHadiths = _hadiths;
} else {
_filteredHadiths = HadithNawawi.searchHadiths(query);
}
});
}
void _clearSearch() {
setState(() {
_searchController.clear();
_filteredHadiths = _hadiths;
});
FocusScope.of(context).unfocus();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Hadith Nawawi Collection'),
backgroundColor: Theme.of(context).colorScheme.primaryContainer,
actions: [
IconButton(
icon: const Icon(Icons.info_outline),
onPressed: () => _showAboutDialog(context),
),
],
),
body: Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
controller: _searchController,
decoration: InputDecoration(
labelText: 'Search Hadiths',
prefixIcon: const Icon(Icons.search),
border: const OutlineInputBorder(),
suffixIcon: IconButton(
icon: const Icon(Icons.clear),
onPressed: _clearSearch,
),
),
onChanged: _filterHadiths,
),
),
Expanded(
child: _buildBody(),
),
],
),
);
}
Widget _buildBody() {
if (_isLoading) {
return const Center(child: CircularProgressIndicator());
}
if (_errorMessage.isNotEmpty) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.error_outline, size: 48, color: Colors.red),
const SizedBox(height: 16),
Text(_errorMessage),
const SizedBox(height: 16),
ElevatedButton(
onPressed: _loadHadiths,
child: const Text('Retry'),
),
],
),
);
}
if (_filteredHadiths.isEmpty) {
return const Center(
child: Text('No hadiths found'),
);
}
return ListView.builder(
itemCount: _filteredHadiths.length,
itemBuilder: (context, index) {
final hadith = _filteredHadiths[index];
return HadithListItem(hadith: hadith);
},
);
}
void _showAboutDialog(BuildContext context) {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('About Hadith Nawawi'),
content: const SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
'Arba\'in Al-Nawawi (الأربعين النووية)',
style: TextStyle(fontWeight: FontWeight.bold),
),
SizedBox(height: 8),
Text(
'A collection of forty hadiths compiled by Imam Yahya ibn Sharaf al-Nawawi. '
'These hadiths cover the core of Islam and are considered essential knowledge for every Muslim.',
),
],
),
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Close'),
),
],
),
);
}
}
class HadithListItem extends StatelessWidget {
final Hadith hadith;
const HadithListItem({super.key, required this.hadith});
@override
Widget build(BuildContext context) {
return Card(
margin: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
child: InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => HadithDetailPage(hadith: hadith),
),
);
},
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.primary,
shape: BoxShape.circle,
),
child: Text(
'${hadith.idInBook}',
style: TextStyle(
color: Theme.of(context).colorScheme.onPrimary,
fontWeight: FontWeight.bold,
),
),
),
const SizedBox(width: 8),
Expanded(
child: Text(
hadith.english.narrator,
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16,
),
),
),
],
),
const SizedBox(height: 8),
Text(
hadith.english.text.length > 100
? '${hadith.english.text.substring(0, 100)}...'
: hadith.english.text,
style: const TextStyle(fontSize: 14),
),
const SizedBox(height: 8),
Align(
alignment: Alignment.centerRight,
child: Text(
hadith.arabic.length > 50
? '${hadith.arabic.substring(0, 50)}...'
: hadith.arabic,
textDirection: TextDirection.rtl,
style: const TextStyle(
fontSize: 16,
fontFamily: 'Amiri',
),
),
),
],
),
),
),
);
}
}
class HadithDetailPage extends StatelessWidget {
final Hadith hadith;
const HadithDetailPage({super.key, required this.hadith});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Hadith ${hadith.idInBook}'),
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Narrator
Text(
hadith.english.narrator,
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
),
),
const SizedBox(height: 16),
// English text
Text(
hadith.english.text,
style: const TextStyle(fontSize: 16),
),
const SizedBox(height: 24),
// Arabic text
Directionality(
textDirection: TextDirection.rtl,
child: Text(
hadith.arabic,
style: const TextStyle(
fontSize: 20,
fontFamily: 'Amiri',
height: 1.5,
),
),
),
const SizedBox(height: 16),
// Metadata
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Hadith ID: ${hadith.id}'),
Text('Book ID: ${hadith.bookId}'),
Text('Chapter ID: ${hadith.chapterId}'),
],
),
),
),
],
),
),
);
}
}