working on filtering

This commit is contained in:
Odweta
2026-03-23 11:36:02 +01:00
parent 2085f010f1
commit 5c7e2dea57
2 changed files with 26 additions and 4 deletions
+23
View File
@@ -7,17 +7,20 @@ import 'package:frontend/repository/quote_repository.dart';
class QuoteProvider extends ChangeNotifier {
List<Quote> _quotes = [];
List<Quote> _filteredQuotes = [];
Set<String> _authors = {};
bool _loading = false;
String? _error;
List<Quote> get quotes => _quotes;
List<Quote> get filteredQuotes => _filteredQuotes;
Set<String> get authors => _authors;
bool get loading => _loading;
String? get error => _error;
final QuoteRepository repo = QuoteRepository();
int filterBy = 0; // 0 = by author, 1 = by quote
var filterController = TextEditingController(text: '');
void clearFilterController() {
@@ -35,6 +38,25 @@ class QuoteProvider extends ChangeNotifier {
}
}
void filterQuotes() {
_filteredQuotes = [];
for (var quote in _quotes) {
switch (filterBy) {
case 0:
if (quote.author.toLowerCase() == filterController.text.toLowerCase()) {
_filteredQuotes.add(quote);
}
break;
case 1:
if (quote.body.toLowerCase().contains(filterController.text.toLowerCase())) {
_filteredQuotes.add(quote);
}
break;
}
}
notifyListeners();
}
Future<void> readQuotes() async {
_loading = true;
_error = null;
@@ -42,6 +64,7 @@ class QuoteProvider extends ChangeNotifier {
try {
_quotes = await repo.readQuotes();
_filteredQuotes = _quotes;
} catch (e) {
_error = e.toString();
}