11import 'package:dio/dio.dart' ;
22import 'package:flutter/material.dart' ;
33import 'dart:math' ;
4+ import 'package:flutter_dotenv/flutter_dotenv.dart' ;
45
56import 'banner.dart' ;
67import 'category.dart' ;
78import 'itemcards.dart' ;
89import 'trends.dart' ;
910import 'details.dart' ;
1011
12+
1113class HomePage extends StatefulWidget {
1214 const HomePage ({super .key});
1315
@@ -25,108 +27,111 @@ class _HomePageState extends State<HomePage> {
2527 fetchBooks ();
2628 }
2729
28- Future <void > fetchBooks () async {
29- try {
30- final response = await Dio ().get ('https://gutendex.com/books' );
31- if (response.statusCode == 200 ) {
32- if (mounted) { // Check if the widget is still mounted
33- setState (() {
34- books = response.data['results' ];
35- isLoading = false ;
36- });
37- }
38- } else {
39- throw Exception ('Failed to load books' );
40- }
41- } catch (e) {
42- if (mounted) { // Check if the widget is still mounted
30+ Future <void > fetchBooks () async {
31+ await dotenv.load (fileName: "assets/config/.env" );
32+ try {
33+ final response = await Dio ().get ('${dotenv .env ['API_BASE_URL' ]}/books' );
34+ if (response.statusCode == 200 ) {
35+ if (mounted) {
4336 setState (() {
37+ books = response.data;
4438 isLoading = false ;
4539 });
4640 }
41+ } else {
42+ throw Exception ('Failed to load books' );
43+ }
44+ } catch (e) {
45+ if (mounted) {
46+ setState (() {
47+ isLoading = false ;
48+ });
4749 }
50+ print ('Error fetching books: $e ' );
4851 }
52+ }
4953
5054
51- @override
52- Widget build (BuildContext context) {
53- final random = Random ();
54- final randomBook = books.isNotEmpty ? books[random.nextInt (books.length)] : null ;
5555
56- return Scaffold (
57- body: SingleChildScrollView (
58- scrollDirection: Axis .vertical,
59- child: Column (
60- children: [
61- isLoading
62- ? const Center (child: CircularProgressIndicator ())
63- : BannerSection (book: randomBook),
64- const SizedBox (height: 5 ),
65- const CategoryCard (),
66- const SizedBox (height: 5 ),
67- SingleChildScrollView (
68- scrollDirection: Axis .horizontal,
69- child: Row (
70- children: books.map ((book) {
71- return InkWell (
72- onTap: () {
73- Navigator .push (
74- context,
75- MaterialPageRoute (
76- builder: (context) => DetailsScreen (
77- imageUrl: book['formats' ]['image/jpeg' ] ?? 'assets/images/imgae_not.jpg' ,
78- title: book['title' ] ?? 'Unknown Title' ,
79- author: book['authors' ].isNotEmpty ? book['authors' ][0 ]['name' ] : 'Unknown Author' ,
80- description: book['description' ] ?? 'No description available.' ,
81- bookUrl: book['formats' ]['text/html' ]
82- ),
56+ @override
57+ Widget build (BuildContext context) {
58+ final random = Random ();
59+ final randomBook = books.isNotEmpty ? books[random.nextInt (books.length)] : null ;
60+
61+ return Scaffold (
62+ body: SingleChildScrollView (
63+ scrollDirection: Axis .vertical,
64+ child: Column (
65+ children: [
66+ isLoading
67+ ? const Center (child: CircularProgressIndicator ())
68+ : BannerSection (book: randomBook),
69+ const SizedBox (height: 5 ),
70+ const CategoryCard (),
71+ const SizedBox (height: 5 ),
72+ SingleChildScrollView (
73+ scrollDirection: Axis .horizontal,
74+ child: Row (
75+ children: books.map ((book) {
76+ return InkWell (
77+ onTap: () {
78+ Navigator .push (
79+ context,
80+ MaterialPageRoute (
81+ builder: (context) => DetailsScreen (
82+ imageUrl: book['cover_url' ] ?? 'assets/images/imgae_not.jpg' ,
83+ title: book['title' ] ?? 'Unknown Title' ,
84+ author: book['author' ] ?? 'Unknown Author' ,
85+ description: book['description' ] ?? 'No description available.' ,
86+ bookUrl: book['pdf_url' ]
8387 ),
84- );
85- },
86- child : ItemCards (
87- imagepic : book[ 'formats' ][ 'image/jpeg' ] ?? 'assets/images/imgae_not.jpg' ,
88- text1 : book['title ' ] ?? 'Unknown Title ' ,
89- text2 : book['authors' ].isNotEmpty ? book[ 'authors' ][ 0 ][ 'name' ] : ' Unknown Author ' ,
90- ) ,
91- );
92- }). toList (),
93- ),
88+ ),
89+ );
90+ },
91+ child : ItemCards (
92+ imagepic : book['cover_url ' ] ?? 'assets/images/imgae_not.jpg ' ,
93+ text1 : book['title' ] ?? ' Unknown Title ' ,
94+ text2 : book[ 'author' ] ?? 'Unknown Author' ,
95+ ),
96+ );
97+ }). toList ( ),
9498 ),
95- const SizedBox (height : 10 ),
96- const Trends ( ),
97- const SizedBox (height : 15 ),
98- SingleChildScrollView (
99- scrollDirection : Axis .horizontal,
100- child : Row (
101- children : books. map ((book) {
102- return InkWell (
103- onTap : () {
104- Navigator . push (
105- context,
106- MaterialPageRoute (
107- builder : (context) => DetailsScreen (
108- imageUrl : book[ 'formats' ][ 'image/jpeg' ] ?? 'assets/images/imgae_not.jpg' ,
109- title : book['title ' ] ?? 'Unknown Title ' ,
110- author : book['authors' ].isNotEmpty ? book[ 'authors' ][ 0 ][ 'name' ] : ' Unknown Author ' ,
111- description : book['description ' ] ?? 'No description available.' ,
112- bookUrl : book['formats' ][ 'text/html' ]
113- ),
99+ ),
100+ const SizedBox (height : 10 ),
101+ const Trends ( ),
102+ const SizedBox (height : 15 ),
103+ SingleChildScrollView (
104+ scrollDirection : Axis .horizontal,
105+ child : Row (
106+ children : books. map ((book) {
107+ return InkWell (
108+ onTap : () {
109+ Navigator . push (
110+ context,
111+ MaterialPageRoute (
112+ builder : (context) => DetailsScreen (
113+ imageUrl : book['cover_url ' ] ?? 'assets/images/imgae_not.jpg ' ,
114+ title : book['title' ] ?? ' Unknown Title ' ,
115+ author : book['author ' ] ?? 'Unknown Author' ,
116+ description : book['description' ] ?? 'No description available.' ,
117+ bookUrl : book[ 'pdf_url' ]
114118 ),
115- );
116- },
117- child : ItemCards (
118- imagepic : book[ 'formats' ][ 'image/jpeg' ] ?? 'assets/images/imgae_not.jpg' ,
119- text1 : book['title ' ] ?? 'Unknown Title ' ,
120- text2 : book['authors' ].isNotEmpty ? book[ 'authors' ][ 0 ][ 'name' ] : ' Unknown Author ' ,
121- ) ,
122- );
123- }). toList (),
124- ),
119+ ),
120+ );
121+ },
122+ child : ItemCards (
123+ imagepic : book['cover_url ' ] ?? 'assets/images/imgae_not.jpg ' ,
124+ text1 : book['title' ] ?? ' Unknown Title ' ,
125+ text2 : book[ 'author' ] ?? 'Unknown Author' ,
126+ ),
127+ );
128+ }). toList ( ),
125129 ),
126- const SizedBox (height : 20 ),
127- ] ,
128- ) ,
130+ ),
131+ const SizedBox (height : 20 ) ,
132+ ] ,
129133 ),
130- );
134+ ),
135+ );
131136 }
132137}
0 commit comments