Sufiyan0910 / New-Code

0 stars 0 forks source link

Minor Project #3

Open Sufiyan0910 opened 6 months ago

Sufiyan0910 commented 6 months ago

Code :-

1.) (Hadith books) import 'dart:convert'; import 'package:http/http.dart' as http; import 'package:flutter/material.dart'; import 'HadithChapters.dart';

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

@override State createState() => _HadithBooksState(); }

class _HadithBooksState extends State { late Map rawdatamap = {}; late List datalist = [];

void getapi() async { var apiKey = "\$2y\$10\$BylaBcXs5Lw7ZOtYmQ3PXO1x15zpp26oc1FeGktdmF6YeYoRd88e"; var res = await http .get(Uri.parse("https://hadithapi.com/api/books?apiKey=$apiKey")); // print("SMASB" + res.body);

if (res.statusCode == 200) {
  setState(() {
    rawdatamap = jsonDecode(res.body);

    datalist = rawdatamap["books"];
  });
}

}

@override void initState() { // TODO: implement initState super.initState();

getapi();

}

@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text( "Hadith Books Collection", style: TextStyle(color: Color(0XFFF2F2F2)), ), centerTitle: true, backgroundColor: Color(0XFF0D0D0D), ), body: datalist.isEmpty ? Center( child: CircularProgressIndicator(), ) : ListView.builder( itemBuilder: (context, index) { return Padding( padding: const EdgeInsets.all(16.0), child: ListTile( onTap: () { var bookSlug = datalist[index]["bookSlug"];

                  Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) => HadithChapters(bookSlug),
                      ));
                },
                tileColor:  Color(0XFF404040),
                textColor: Color(0XFFF2F2F2),
                title: Text(
                  datalist[index]["bookName"].toString(),
                  style: TextStyle(fontFamily: "alq"),
                ),
                // subtitle: Text(datalist[index]["writerName"].toString()),
                leading: Text("${index + 1}"),
                trailing: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    datalist[index]["hadiths_count"] == "0"
                        ? Text("")
                        :

                    Text("Ahadiths :" +
                        datalist[index]["hadiths_count"].toString()),
                    Text("Chapters :" +
                        datalist[index]["chapters_count"].toString()),
                  ],
                ),
              ),
            );
          },
          itemCount: datalist.length,
        ),
  backgroundColor: Color(0XFFF2F2F2),
);

} }

2.) Hadith chapters import 'dart:convert';

import 'package:flutter/material.dart';

import 'package:http/http.dart' as http; import 'Hadiths.dart';

// ignore: must_be_immutable class HadithChapters extends StatefulWidget { var bookSlug; HadithChapters(this.bookSlug, {super.key});

@override State createState() => _HadithChaptersState(); }

class _HadithChaptersState extends State { late Map rawdatamap = {}; late List datalist = []; void getapi() async { var slug = widget.bookSlug; var apiKey = "\$2y\$10\$BylaBcXs5Lw7ZOtYmQ3PXO1x15zpp26oc1FeGktdmF6YeYoRd88e"; var res = await http.get( Uri.parse("https://hadithapi.com/api/$slug/chapters?apiKey=$apiKey"));

// print("SMASB" + res.body);

if (res.statusCode == 200) {
  setState(() {
    rawdatamap = jsonDecode(res.body);
    datalist = rawdatamap["chapters"];
  });
}

}

@override void initState() { // TODO: implement initState super.initState();

getapi();

}

@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text( "Hadith Books Chapters", style: TextStyle(color: Color(0XFFF2F2F2)), ), centerTitle: true, backgroundColor: Color(0XFF0D0D0D), ), body: datalist.isEmpty ? Center( child: CircularProgressIndicator(), ) : ListView.builder( itemBuilder: (context, index) { return Padding( padding: const EdgeInsets.all(8.0), child: ListTile( onTap: () { var bookSlug = datalist[index]["bookSlug"]; var chapterNumber = datalist[index]["chapterNumber"];

                  print(bookSlug + chapterNumber);
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) =>
                            Hadiths(bookSlug, chapterNumber),
                      ));
                },
                tileColor: Color(0XFFF2F2F2),
                textColor: Color(0XFF404040),
                title: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                     CircleAvatar(
                    backgroundColor: Color(0XFF0D0D0D),
                    child: Text(
                      "${index + 1}",
                      style: TextStyle(color: Color(0XFFF2F2F2)),
                    )),
                      SizedBox(
                      height: 15,
                    ),
                    Text(
                      datalist[index]["chapterArabic"].toString(),
                      style: TextStyle(fontFamily: "alq", fontSize: 15),
                      textDirection: TextDirection.rtl,
                      textAlign: TextAlign.justify,
                    ),
                    SizedBox(
                      height: 10,
                    ),
                    Text(datalist[index]["chapterUrdu"].toString(),
                        textDirection: TextDirection.rtl,
                        textAlign: TextAlign.justify,
                        style: TextStyle(fontFamily: "jameel")),
                    SizedBox(
                      height: 10,
                    ),
                    Text(
                      datalist[index]["chapterEnglish"].toString(),
                      textAlign: TextAlign.center,
                    ),
                  ],
                ),

              ),
            );
          },
          itemCount: datalist.length,
        ),
  backgroundColor: Color(0XFFD9D9D9),
);

} }

3.) Hadith Packages import 'package:flutter/material.dart';

import 'package:hadith/hadith.dart';

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

@override State createState() => _HomeState(); }

class _HomeState extends State { void hadith() { // Get available collections // print(getCollections());

// Get a single collection
// print(getCollection(Collections.bukhari));

// // Get collection data
// print(getCollectionData(Collections.bukhari, Languages.en));

//Get books of a collection
// print(getBooks(Collections.bukhari));

// Get a single book
// print(getBook(Collections.bukhari, 1));

// // Get book data
// print(getBookData(Collections.bukhari, 1, Languages.en));

// // Get hadiths of a book
// print(getHadiths(Collections.bukhari, 1));

// // Get a single hadith
// print(getHadith(Collections.bukhari, 1, 1));

// Get hadith data
// print(getHadithData(Collections.bukhari, 1, 1, Languages.en));

// // Get hadith data by hadith number
// print(getHadithDataByNumber(Collections.bukhari, '1', Languages.en));
// print(getHadithDataByNumber(Collections.muslim, '36 b', Languages.en));

// // Get collection URL
// print(getCollectionURL(Collections.bukhari));

// // Get book URL
// print(getBookURL(Collections.bukhari, 1));

}

List datalist = Collections.values;

@override void initState() { // TODO: implement initState super.initState(); hadith(); }

@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("Hadith Collections "), centerTitle: true, ), backgroundColor: Colors.indigo[900], body: ListView.builder( itemBuilder: (context, index) { return ListTile( onTap: () { var name = datalist[index]; Navigator.push( context, MaterialPageRoute( builder: (context) => BOHadith(name), ));

          ;
        },
        leading: Text("${index + 1}"),
        tileColor: Colors.indigo[900],
        textColor: Colors.white,
        title: Text(
          getCollection(datalist[index]).collection[1].title.toString(),
          style: TextStyle(fontFamily: "alq", fontSize: 20),
        ),
        subtitle: Text(
            getCollection(datalist[index]).collection[0].title.toString()),
        trailing:
            Text(getCollection(datalist[index]).totalHadith.toString()),
      );
    },
    itemCount: datalist.length,
  ),
);

} }

class BOHadith extends StatefulWidget { final nameofcollection; BOHadith(this.nameofcollection, {super.key});

@override State createState() => _BOHadithState(); }

class _BOHadithState extends State { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("Books of Hadiths "), centerTitle: true, ), body: ListView.builder( itemBuilder: (context, index) { return ListTile( onTap: () {}, leading: Text("${index + 1}"), title: Text( getBooks(widget.nameofcollection)[index].book[1].name.toString(), style: TextStyle(fontFamily: "alq", fontSize: 20), ), subtitle: Text(getBooks(widget.nameofcollection)[index] .book[0] .name .toString()), trailing: Column( crossAxisAlignment: CrossAxisAlignment.end, children: [ Text("Hadiths" + getBooks(widget.nameofcollection)[index] .hadithStartNumber .toString() + " To " + getBooks(widget.nameofcollection)[index] .hadithEndNumber .toString()), Text("Total Hadiths :" + getBooks(widget.nameofcollection)[index] .numberOfHadith .toString()), ], ), ); }, itemCount: getBooks(widget.nameofcollection).length, ), ); } }

4.) Hadiths import 'dart:convert';

import 'package:flutter/material.dart';

import 'package:http/http.dart' as http;

// ignore: must_be_immutable class Hadiths extends StatefulWidget { var slug; var number; Hadiths(this.slug, this.number, {super.key});

@override State createState() => _HadithsState(); }

class _HadithsState extends State { late Map rawdatamap = {}; late List datalist = []; void getapi() async { var bookslug = widget.slug; var number = widget.number; var apiKey = "\$2y\$10\$BylaBcXs5Lw7ZOtYmQ3PXO1x15zpp26oc1FeGktdmF6YeYoRd88e"; var res = await http.get(Uri.parse( "https://hadithapi.com/public/api/hadiths?apiKey=$apiKey&book=$bookslug&chapter=$number&paginate=100000"));

// print("SMASB" + res.body);

if (res.statusCode == 200) {
  setState(() {
    rawdatamap = jsonDecode(res.body);

    datalist = rawdatamap["hadiths"]["data"];
  });
}

}

@override void initState() { // TODO: implement initState super.initState(); getapi(); }

@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text( "Hadith Books Collection", style: TextStyle(color:Color(0XFFF2F2F2),), ), centerTitle: true, backgroundColor: Color(0XFF595959), ), body: datalist.isEmpty ? Center( child: CircularProgressIndicator(), ) : ListView.builder( itemBuilder: (context, index) { return Card( color: Color(0XFFD9D9D9), child: Column( children: [ ListTile( title: Text("Hadith Number #" + datalist[index]["hadithNumber"].toString()), trailing: Text("Status :" + datalist[index]["status"].toString())), Card( color: Color(0XFFF2F2F2), child: Container( width: double.infinity, child: Padding( padding: const EdgeInsets.all(16.0), child: Text( datalist[index]["hadithArabic"].toString(), style: const TextStyle( fontFamily: "alq", fontSize: 20, color: Color.fromARGB(255, 0, 77, 64) ), textAlign: TextAlign.justify, textDirection: TextDirection.rtl, ), ), ), ), Card( color: Color(0XFFF2F2F2), child: Container( width: double.infinity, child: Padding( padding: const EdgeInsets.all(16.0), child: Text( datalist[index]["hadithUrdu"].toString(), style: const TextStyle( fontFamily: "jameel", fontSize: 20, ), textAlign: TextAlign.justify, textDirection: TextDirection.rtl, ), ), ), ), Card( color: Color(0XFFF2F2F2), child: Container( width: double.infinity, child: Padding( padding: const EdgeInsets.all(16.0), child: Text( datalist[index]["hadithEnglish"].toString(), style: const TextStyle( fontFamily: "alq", fontSize: 20, ), textAlign: TextAlign.justify, textDirection: TextDirection.ltr, ), ), ), ), ], ), ); }, itemCount: datalist.length, ), );

} }

5.) Main import 'package:flutter/material.dart'; import 'SplashScreen.dart';

void main() { runApp(const MyApp()); }

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

@override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, title: 'Flutter Demo', theme: ThemeData( colorScheme: ColorScheme.fromSeed(seedColor: Color(0XFF404040)), useMaterial3: true, ), home: const SplashScreen(), ); } }

6.) Search Hadith import 'dart:convert'; import 'dart:math';

import 'package:flutter/material.dart';

import 'package:http/http.dart' as http; import 'package:custom_radio_grouped_button/custom_radio_grouped_button.dart';

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

@override State createState() => _SearchHadithState(); }

class _SearchHadithState extends State { late Map rawdatamap = {}; late List datalist = []; late String searchInput = ""; late String selectlang = ""; bool isLoading = false;

void getapi(String value) async { setState(() { isLoading = true; });

var apiKey =
    "\$2y\$10\$BylaBcXs5Lw7ZOtYmQ3PXO1x15zpp26oc1FeGktdmF6YeYoRd88e";

var hadithApiBase =
    "https://hadithapi.com/public/api/hadiths?apiKey=$apiKey&paginate=100000";

String queryParam = '';

if (value == 'Arabic') {
  queryParam = 'hadithArabic';
} else if (value == 'Urdu') {
  queryParam = 'hadithUrdu';
} else if (value == 'English') {
  queryParam = 'hadithEnglish';
} else if (value == 'Hadith No') {
  queryParam = 'hadithNumber';
} else {
  print("Please select a search type first.");
  return;
}

var res =
    await http.get(Uri.parse("$hadithApiBase&$queryParam=$searchInput"));
if (res.statusCode == 200) {
  setState(() {
    rawdatamap = jsonDecode(res.body);
    datalist = rawdatamap["hadiths"]["data"];
    isLoading = false;
  });
}
// print("SMASB" + res.body);

}

@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text( "Search In All Hadith Books ", style: TextStyle( color: Color(0XFFF2F2F2), ), ), centerTitle: true, backgroundColor: Color(0XFF595959), ), body: Column( children: [ Padding( padding: const EdgeInsets.all(16.0), child: Text( "سرچ کے لیے مندرجہ ذیل آپشن کو پہلے منتخب کیجیے", style: const TextStyle( fontFamily: "jameel", fontSize: 20, ), textAlign: TextAlign.justify, textDirection: TextDirection.rtl, )), CustomRadioButton( absoluteZeroSpacing: true, elevation: 0, unSelectedBorderColor: Color(0XFFD9D9D9), enableShape: true, selectedBorderColor: Color(0XFF0D0D0D), selectedColor: Color(0XFF0D0D0D), unSelectedColor: Color(0XFFD9D9D9), buttonLables: ['Arabic', 'Urdu', 'English', 'Hadith No'], buttonValues: ['Arabic', 'Urdu', 'English', 'Hadith No'], buttonTextStyle: const ButtonTextStyle( selectedColor: Colors.white, unSelectedColor: Colors.black, textStyle: TextStyle(fontSize: 16)), radioButtonValue: (value) { setState(() { selectlang = value; }); }, ), Padding( padding: const EdgeInsets.all(8.0), child: TextField( decoration: selectlang == 'Urdu' || selectlang == 'Arabic' ? InputDecoration( hintText: " ..... تلاش کریں ", suffixIcon: Icon(Icons.search_rounded)) : InputDecoration( hintText: "Search Here..... ", prefixIcon: Icon(Icons.search_rounded)), onChanged: (value) { searchInput = value; print("search value smas=>" + searchInput); }, style: selectlang == 'Urdu' || selectlang == 'Arabic' ? TextStyle(fontFamily: "jameel") : TextStyle(fontFamily: "alq"), textAlign: selectlang == 'Urdu' || selectlang == 'Arabic' ? TextAlign.right : TextAlign.left, textDirection: selectlang == 'Urdu' || selectlang == 'Arabic' ? TextDirection.rtl : TextDirection.ltr, ), ), ElevatedButton( onPressed: () { getapi(selectlang); }, child: Text("Search")), SizedBox( height: 10, ), Expanded( child: isLoading ? const Center( child: CircularProgressIndicator(), ) : ListView.builder( itemBuilder: (context, index) { String hadithdata = '';

                  if (selectlang == 'Arabic') {
                    hadithdata = 'hadithArabic';
                  } else if (selectlang == 'Urdu') {
                    hadithdata = 'hadithUrdu';
                  } else if (selectlang == 'English') {
                    hadithdata = 'hadithEnglish';
                  } else {
                    print("Please select a search type first.");
                  }

                  String searchResult =
                      datalist[index][hadithdata].toString();
                  print("below smas" + hadithdata);
                  String searchTerm = searchInput;
                  int startIndex = searchResult.indexOf(searchTerm);

                  if (startIndex != -1) {
                    String first20Words = searchResult.substring(startIndex,
                        min(startIndex + 50, searchResult.length));

                    return Card(
                      color: Color(0XFFD9D9D9),
                      child: Column(
                        children: [
                          ListTile(
                              title: Text("Hadith Number #" +
                                  datalist[index]["hadithNumber"]
                                      .toString()),
                              trailing: Text("Status :" +
                                  datalist[index]["status"].toString())),
                          Card(
                            color: Color(0XFFF2F2F2),
                            child: Container(
                              width: double.infinity,
                              child: Padding(
                                  padding: const EdgeInsets.all(16.0),
                                  child: Text(
                                    first20Words.toString(),
                                    style: selectlang == 'Urdu' ||
                                            selectlang == 'Arabic'
                                        ? TextStyle(fontFamily: "jameel")
                                        : TextStyle(fontFamily: "alq"),
                                    textAlign: selectlang == 'Urdu' ||
                                            selectlang == 'Arabic'
                                        ? TextAlign.right
                                        : TextAlign.left,
                                    textDirection: selectlang == 'Urdu' ||
                                            selectlang == 'Arabic'
                                        ? TextDirection.rtl
                                        : TextDirection.ltr,
                                  )),
                            ),
                          ),
                        ],
                      ),
                    );
                  }
                },
                itemCount: datalist.length,
              ),
      ),
    ],
  ),
);

} }

7.) Splash Screen import 'dart:async';

import 'package:flutter/material.dart'; import 'TabsScr.dart';

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

@override State createState() => _SplashScreenState(); }

class _SplashScreenState extends State { void initState() { // TODO: implement initState super.initState();

Timer(Duration(seconds: 5), (() {
  Navigator.pushReplacement(
      context, MaterialPageRoute(builder: (context) => TabsScr()));
}));

}

@override Widget build(BuildContext context) { return Scaffold( body: Center( child: Container( width: double.infinity, height: double.infinity, decoration: const BoxDecoration( color: Color(0XFF262626), ), child:Image.asset("assets/hadith.jpg",width: double.infinity,height:double.infinity ,) ), ), backgroundColor: Color(0XFFD9D9D9)); } } 8.) TabScr import 'package:flutter/material.dart'; import 'HadithBooks.dart'; import 'SearchHadith.dart';

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

@override State createState() => _TabsScrState(); }

class _TabsScrState extends State { @override Widget build(BuildContext context) { return DefaultTabController( length: 2, child: Scaffold( appBar: AppBar( title: Text( " احادیث نبویﷺ", style: TextStyle(fontFamily: "alq",), textDirection: TextDirection.rtl, textAlign: TextAlign.justify, ), centerTitle: true, bottom: TabBar( labelColor: Color(0XFF404040), unselectedLabelColor: Colors.grey, indicatorColor: Color(0XFF0D0D0D), tabs: [ Tab( text: "Hadith Books", ), Tab( text: "Search Hadiths", ), ], ), ), body: TabBarView(children: [ HadithBooks(), SearchHadith(), ])), ); } }