@@ -16,85 +16,29 @@ class _ExploreScreenState extends State<ExploreScreen> {
1616 @override
1717 void initState () {
1818 super .initState ();
19- fetchBooks ();
2019 }
2120
22- Future <void > fetchBooks () async {
23- final response = await http.get (Uri .parse ('https://gutendex.com/books/?languages=en&subjects=drama,fiction' ));
24- if (response.statusCode == 200 ) {
25- final data = jsonDecode (response.body);
26- setState (() {
27- books = data['results' ];
28- });
29- } else {
30- throw Exception ('Failed to load books' );
31- }
32- }
21+
3322
3423 @override
3524 Widget build (BuildContext context) {
25+ // Scaffold is a layout for
26+ // the major Material Components.
3627 return Scaffold (
37- body: _buildBookGrid (),
38- );
39- }
40-
41- Widget _buildBookGrid () {
42- if (books.isEmpty) {
43- return const Center (child: CircularProgressIndicator ());
44- }
45-
46- return GridView .builder (
47- padding: const EdgeInsets .all (10 ),
48- gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount (
49- crossAxisCount: 2 ,
50- childAspectRatio: 0.6 ,
51- crossAxisSpacing: 10 ,
52- mainAxisSpacing: 10 ,
53- ),
54- itemCount: books.length,
55- itemBuilder: (context, index) {
56- final book = books[index];
57- return GestureDetector (
58- onTap: () {
59- Navigator .push (
60- context,
61- MaterialPageRoute (
62- builder: (context) => DetailsScreen (
63- imageUrl: book['formats' ]['image/jpeg' ] ?? 'https://via.placeholder.com/150' ,
64- title: book['title' ],
65- author: book['authors' ].isNotEmpty ? book['authors' ][0 ]['name' ] : 'Unknown' ,
66- description: book['subjects' ].join (', ' ),
67- bookUrl: book['formats' ]['text/html' ]
68- ),
69- ),
70- );
71- },
72- child: Card (
73- child: Column (
74- crossAxisAlignment: CrossAxisAlignment .stretch,
75- children: [
76- Expanded (
77- child: Image .network (
78- book['formats' ]['image/jpeg' ] ?? 'https://via.placeholder.com/150' ,
79- fit: BoxFit .cover,
80- ),
81- ),
82- Padding (
83- padding: const EdgeInsets .all (8.0 ),
84- child: Text (
85- book['title' ],
86- style: const TextStyle (
87- fontWeight: FontWeight .bold,
88- fontSize: 16 ,
89- ),
90- textAlign: TextAlign .center,
91- ),
92- ),
93- ],
94- ),
28+ appBar: AppBar (
29+ title: const Text ('Example title' ),
30+ actions: const [
31+ IconButton (
32+ icon: Icon (Icons .search),
33+ tooltip: 'Search' ,
34+ onPressed: null ,
9535 ),
96- );
97- },
36+ ],
37+ ),
38+ // body is the majority of the screen.
39+ body: const Center (
40+ child: Text ('Hello, world!' ),
41+ ),
9842 );
9943 }
10044}
0 commit comments