|
| 1 | +import 'package:dio/dio.dart'; |
| 2 | +import 'package:flutter/foundation.dart'; |
1 | 3 | import 'package:flutter/material.dart'; |
2 | | -import 'package:http/http.dart' as http; |
3 | | -import 'dart:convert'; |
4 | | -import 'details.dart'; // Import the DetailsScreen |
| 4 | +import 'package:flutter_dotenv/flutter_dotenv.dart'; |
| 5 | + |
| 6 | +import 'category.dart'; |
| 7 | +import 'itemcards.dart'; |
| 8 | +import 'trends.dart'; |
| 9 | +import 'details.dart'; |
5 | 10 |
|
6 | 11 | class ExploreScreen extends StatefulWidget { |
7 | 12 | const ExploreScreen({super.key}); |
8 | 13 |
|
9 | 14 | @override |
10 | | - _ExploreScreenState createState() => _ExploreScreenState(); |
| 15 | + State<ExploreScreen> createState() => _ExploreScreenState(); |
11 | 16 | } |
12 | 17 |
|
13 | 18 | class _ExploreScreenState extends State<ExploreScreen> { |
14 | 19 | List books = []; |
| 20 | + List filteredBooks = []; |
| 21 | + bool isLoading = true; |
| 22 | + bool _isSearchVisible = false; |
| 23 | + final TextEditingController _searchController = TextEditingController(); |
| 24 | + final FocusNode _searchFocusNode = FocusNode(); |
15 | 25 |
|
16 | 26 | @override |
17 | 27 | void initState() { |
18 | 28 | super.initState(); |
| 29 | + fetchBooks(); |
| 30 | + _searchController.addListener(_filterBooks); |
19 | 31 | } |
20 | 32 |
|
| 33 | + Future<void> fetchBooks() async { |
| 34 | + await dotenv.load(fileName: "assets/config/.env"); |
| 35 | + try { |
| 36 | + final response = await Dio().get('${dotenv.env['API_BASE_URL']}/books'); |
| 37 | + if (response.statusCode == 200) { |
| 38 | + if (mounted) { |
| 39 | + setState(() { |
| 40 | + books = response.data; |
| 41 | + filteredBooks = books; |
| 42 | + isLoading = false; |
| 43 | + }); |
| 44 | + } |
| 45 | + } else { |
| 46 | + throw Exception('Failed to load books'); |
| 47 | + } |
| 48 | + } catch (e) { |
| 49 | + if (mounted) { |
| 50 | + setState(() { |
| 51 | + isLoading = false; |
| 52 | + }); |
| 53 | + } |
| 54 | + if (kDebugMode) { |
| 55 | + print('Error fetching books: $e'); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
21 | 59 |
|
| 60 | + void _filterBooks() { |
| 61 | + setState(() { |
| 62 | + filteredBooks = books |
| 63 | + .where((book) => |
| 64 | + book['title'].toLowerCase().contains(_searchController.text.toLowerCase())) |
| 65 | + .toList(); |
| 66 | + if (kDebugMode) { |
| 67 | + print('Filtered Books: ${filteredBooks.length}'); |
| 68 | + } |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + void _toggleSearchBar() { |
| 73 | + setState(() { |
| 74 | + _isSearchVisible = !_isSearchVisible; |
| 75 | + if (_isSearchVisible) { |
| 76 | + _searchFocusNode.requestFocus(); |
| 77 | + } else { |
| 78 | + _searchFocusNode.unfocus(); |
| 79 | + _searchController.clear(); |
| 80 | + filteredBooks = books; |
| 81 | + } |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + @override |
| 86 | + void dispose() { |
| 87 | + _searchController.removeListener(_filterBooks); |
| 88 | + _searchController.dispose(); |
| 89 | + _searchFocusNode.dispose(); |
| 90 | + super.dispose(); |
| 91 | + } |
22 | 92 |
|
23 | 93 | @override |
24 | 94 | Widget build(BuildContext context) { |
25 | 95 | // Scaffold is a layout for |
26 | 96 | // the major Material Components. |
27 | 97 | return Scaffold( |
28 | 98 | appBar: AppBar( |
29 | | - title: const Text('Example title'), |
30 | | - actions: const [ |
| 99 | + title: _isSearchVisible |
| 100 | + ? TextField( |
| 101 | + controller: _searchController, |
| 102 | + focusNode: _searchFocusNode, |
| 103 | + decoration: const InputDecoration( |
| 104 | + hintText: 'Search...', |
| 105 | + border: InputBorder.none, |
| 106 | + ), |
| 107 | + style: const TextStyle(color: Colors.black), |
| 108 | + ) |
| 109 | + : const Text('Search', |
| 110 | + style: TextStyle( |
| 111 | + fontSize: 22, |
| 112 | + fontWeight: FontWeight.w800, |
| 113 | + color: Colors.black, |
| 114 | + ), |
| 115 | + ), |
| 116 | + actions: [ |
31 | 117 | IconButton( |
32 | | - icon: Icon(Icons.search), |
| 118 | + icon: Icon(_isSearchVisible ? Icons.close : Icons.search), |
33 | 119 | tooltip: 'Search', |
34 | | - onPressed: null, |
| 120 | + onPressed: _toggleSearchBar, |
35 | 121 | ), |
36 | 122 | ], |
37 | 123 | ), |
38 | | - // body is the majority of the screen. |
39 | | - body: const Center( |
40 | | - child: Text('Hello, world!'), |
41 | | - ), |
| 124 | + body: isLoading |
| 125 | + ? const Center(child: CircularProgressIndicator()) |
| 126 | + : SingleChildScrollView( |
| 127 | + scrollDirection: Axis.vertical, |
| 128 | + child: Column( |
| 129 | + children: [ |
| 130 | + const SizedBox(height: 5), |
| 131 | + const CategoryCard(), |
| 132 | + const SizedBox(height: 5), |
| 133 | + SingleChildScrollView( |
| 134 | + scrollDirection: Axis.horizontal, |
| 135 | + child: Row( |
| 136 | + children: filteredBooks.map((book) { |
| 137 | + return InkWell( |
| 138 | + onTap: () { |
| 139 | + Navigator.push( |
| 140 | + context, |
| 141 | + MaterialPageRoute( |
| 142 | + builder: (context) => DetailsScreen( |
| 143 | + imageUrl: book['cover_url'] ?? 'assets/images/imgae_not.jpg', |
| 144 | + title: book['title'] ?? 'Unknown Title', |
| 145 | + author: book['author'] ?? 'Unknown Author', |
| 146 | + description: book['description'] ?? 'No description available.', |
| 147 | + bookUrl: book['pdf_url'], |
| 148 | + ), |
| 149 | + ), |
| 150 | + ); |
| 151 | + }, |
| 152 | + child: ItemCards( |
| 153 | + imagepic: book['cover_url'] ?? 'assets/images/imgae_not.jpg', |
| 154 | + text1: book['title'] ?? 'Unknown Title', |
| 155 | + text2: book['author'] ?? 'Unknown Author', |
| 156 | + ), |
| 157 | + ); |
| 158 | + }).toList(), |
| 159 | + ), |
| 160 | + ), |
| 161 | + const SizedBox(height: 10), |
| 162 | + const Trends(), |
| 163 | + const SizedBox(height: 15), |
| 164 | + SingleChildScrollView( |
| 165 | + scrollDirection: Axis.horizontal, |
| 166 | + child: Row( |
| 167 | + children: books.map((book) { |
| 168 | + return InkWell( |
| 169 | + onTap: () { |
| 170 | + Navigator.push( |
| 171 | + context, |
| 172 | + MaterialPageRoute( |
| 173 | + builder: (context) => DetailsScreen( |
| 174 | + imageUrl: book['cover_url'] ?? 'assets/images/imgae_not.jpg', |
| 175 | + title: book['title'] ?? 'Unknown Title', |
| 176 | + author: book['author'] ?? 'Unknown Author', |
| 177 | + description: book['description'] ?? 'No description available.', |
| 178 | + bookUrl: book['pdf_url'], |
| 179 | + ), |
| 180 | + ), |
| 181 | + ); |
| 182 | + }, |
| 183 | + child: ItemCards( |
| 184 | + imagepic: book['cover_url'] ?? 'assets/images/imgae_not.jpg', |
| 185 | + text1: book['title'] ?? 'Unknown Title', |
| 186 | + text2: book['author'] ?? 'Unknown Author', |
| 187 | + ), |
| 188 | + ); |
| 189 | + }).toList(), |
| 190 | + ), |
| 191 | + ), |
| 192 | + const SizedBox(height: 20), |
| 193 | + ], |
| 194 | + ), |
| 195 | + ), |
42 | 196 | ); |
43 | 197 | } |
44 | 198 | } |
0 commit comments