-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from Hypertext-Assassin-RSS/dev
Dev
- Loading branch information
Showing
3 changed files
with
183 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,198 @@ | ||
import 'package:dio/dio.dart'; | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:http/http.dart' as http; | ||
import 'dart:convert'; | ||
import 'details.dart'; // Import the DetailsScreen | ||
import 'package:flutter_dotenv/flutter_dotenv.dart'; | ||
|
||
import 'category.dart'; | ||
import 'itemcards.dart'; | ||
import 'trends.dart'; | ||
import 'details.dart'; | ||
|
||
class ExploreScreen extends StatefulWidget { | ||
const ExploreScreen({super.key}); | ||
|
||
@override | ||
_ExploreScreenState createState() => _ExploreScreenState(); | ||
State<ExploreScreen> createState() => _ExploreScreenState(); | ||
} | ||
|
||
class _ExploreScreenState extends State<ExploreScreen> { | ||
List books = []; | ||
List filteredBooks = []; | ||
bool isLoading = true; | ||
bool _isSearchVisible = false; | ||
final TextEditingController _searchController = TextEditingController(); | ||
final FocusNode _searchFocusNode = FocusNode(); | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
fetchBooks(); | ||
_searchController.addListener(_filterBooks); | ||
} | ||
|
||
Future<void> fetchBooks() async { | ||
await dotenv.load(fileName: "assets/config/.env"); | ||
try { | ||
final response = await Dio().get('${dotenv.env['API_BASE_URL']}/books'); | ||
if (response.statusCode == 200) { | ||
if (mounted) { | ||
setState(() { | ||
books = response.data; | ||
filteredBooks = books; | ||
isLoading = false; | ||
}); | ||
} | ||
} else { | ||
throw Exception('Failed to load books'); | ||
} | ||
} catch (e) { | ||
if (mounted) { | ||
setState(() { | ||
isLoading = false; | ||
}); | ||
} | ||
if (kDebugMode) { | ||
print('Error fetching books: $e'); | ||
} | ||
} | ||
} | ||
|
||
void _filterBooks() { | ||
setState(() { | ||
filteredBooks = books | ||
.where((book) => | ||
book['title'].toLowerCase().contains(_searchController.text.toLowerCase())) | ||
.toList(); | ||
if (kDebugMode) { | ||
print('Filtered Books: ${filteredBooks.length}'); | ||
} | ||
}); | ||
} | ||
|
||
void _toggleSearchBar() { | ||
setState(() { | ||
_isSearchVisible = !_isSearchVisible; | ||
if (_isSearchVisible) { | ||
_searchFocusNode.requestFocus(); | ||
} else { | ||
_searchFocusNode.unfocus(); | ||
_searchController.clear(); | ||
filteredBooks = books; | ||
} | ||
}); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
_searchController.removeListener(_filterBooks); | ||
_searchController.dispose(); | ||
_searchFocusNode.dispose(); | ||
super.dispose(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
// Scaffold is a layout for | ||
// the major Material Components. | ||
return Scaffold( | ||
appBar: AppBar( | ||
title: const Text('Example title'), | ||
actions: const [ | ||
title: _isSearchVisible | ||
? TextField( | ||
controller: _searchController, | ||
focusNode: _searchFocusNode, | ||
decoration: const InputDecoration( | ||
hintText: 'Search...', | ||
border: InputBorder.none, | ||
), | ||
style: const TextStyle(color: Colors.black), | ||
) | ||
: const Text('Search', | ||
style: TextStyle( | ||
fontSize: 22, | ||
fontWeight: FontWeight.w800, | ||
color: Colors.black, | ||
), | ||
), | ||
actions: [ | ||
IconButton( | ||
icon: Icon(Icons.search), | ||
icon: Icon(_isSearchVisible ? Icons.close : Icons.search), | ||
tooltip: 'Search', | ||
onPressed: null, | ||
onPressed: _toggleSearchBar, | ||
), | ||
], | ||
), | ||
// body is the majority of the screen. | ||
body: const Center( | ||
child: Text('Hello, world!'), | ||
), | ||
body: isLoading | ||
? const Center(child: CircularProgressIndicator()) | ||
: SingleChildScrollView( | ||
scrollDirection: Axis.vertical, | ||
child: Column( | ||
children: [ | ||
const SizedBox(height: 5), | ||
const CategoryCard(), | ||
const SizedBox(height: 5), | ||
SingleChildScrollView( | ||
scrollDirection: Axis.horizontal, | ||
child: Row( | ||
children: filteredBooks.map((book) { | ||
return InkWell( | ||
onTap: () { | ||
Navigator.push( | ||
context, | ||
MaterialPageRoute( | ||
builder: (context) => DetailsScreen( | ||
imageUrl: book['cover_url'] ?? 'assets/images/imgae_not.jpg', | ||
title: book['title'] ?? 'Unknown Title', | ||
author: book['author'] ?? 'Unknown Author', | ||
description: book['description'] ?? 'No description available.', | ||
bookUrl: book['pdf_url'], | ||
), | ||
), | ||
); | ||
}, | ||
child: ItemCards( | ||
imagepic: book['cover_url'] ?? 'assets/images/imgae_not.jpg', | ||
text1: book['title'] ?? 'Unknown Title', | ||
text2: book['author'] ?? 'Unknown Author', | ||
), | ||
); | ||
}).toList(), | ||
), | ||
), | ||
const SizedBox(height: 10), | ||
const Trends(), | ||
const SizedBox(height: 15), | ||
SingleChildScrollView( | ||
scrollDirection: Axis.horizontal, | ||
child: Row( | ||
children: books.map((book) { | ||
return InkWell( | ||
onTap: () { | ||
Navigator.push( | ||
context, | ||
MaterialPageRoute( | ||
builder: (context) => DetailsScreen( | ||
imageUrl: book['cover_url'] ?? 'assets/images/imgae_not.jpg', | ||
title: book['title'] ?? 'Unknown Title', | ||
author: book['author'] ?? 'Unknown Author', | ||
description: book['description'] ?? 'No description available.', | ||
bookUrl: book['pdf_url'], | ||
), | ||
), | ||
); | ||
}, | ||
child: ItemCards( | ||
imagepic: book['cover_url'] ?? 'assets/images/imgae_not.jpg', | ||
text1: book['title'] ?? 'Unknown Title', | ||
text2: book['author'] ?? 'Unknown Author', | ||
), | ||
); | ||
}).toList(), | ||
), | ||
), | ||
const SizedBox(height: 20), | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,4 +56,4 @@ class _BottomNavBarState extends State<BottomNavBar> { | |
]), | ||
); | ||
} | ||
} | ||
} |