Skip to content

Commit

Permalink
Merge pull request #19 from Hypertext-Assassin-RSS/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Hypertext-Assassin-RSS authored Aug 28, 2024
2 parents f6bdb42 + 9a31331 commit 9398acb
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 29 deletions.
178 changes: 166 additions & 12 deletions lib/src/screens/explore.dart
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),
],
),
),
);
}
}
32 changes: 16 additions & 16 deletions lib/src/screens/homepage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,29 @@ class _HomePageState extends State<HomePage> {
fetchBooks();
}

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) {
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;
isLoading = false;
});
}
} else {
throw Exception('Failed to load books');
}
} catch (e) {
if (mounted) {
setState(() {
books = response.data;
isLoading = false;
});
}
} else {
throw Exception('Failed to load books');
}
} catch (e) {
if (mounted) {
setState(() {
isLoading = false;
});
print('Error fetching books: $e');
}
print('Error fetching books: $e');
}
}



Expand Down
2 changes: 1 addition & 1 deletion lib/src/widgets/bottomnav.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ class _BottomNavBarState extends State<BottomNavBar> {
]),
);
}
}
}

0 comments on commit 9398acb

Please sign in to comment.