filtering by author works

This commit is contained in:
Odweta
2026-03-23 11:50:17 +01:00
parent 5c7e2dea57
commit ff655e2dcf
2 changed files with 16 additions and 20 deletions
+2 -4
View File
@@ -30,7 +30,7 @@ class ListPage extends StatelessWidget {
return Center(child: Text(provider.error!)); return Center(child: Text(provider.error!));
} }
final quoteCount = provider.quotes.length; final quoteCount = provider.filteredQuotes.length;
return return
Column( Column(
@@ -229,9 +229,7 @@ class ListPage extends StatelessWidget {
), ),
IconButton( IconButton(
icon: Icon(Icons.search), icon: Icon(Icons.search),
onPressed: () { onPressed: provider.filterQuotes,
provider.filterQuotes;
},
), ),
IconButton( IconButton(
icon: Icon(Icons.more_horiz), icon: Icon(Icons.more_horiz),
+14 -16
View File
@@ -13,10 +13,23 @@ class QuoteProvider extends ChangeNotifier {
String? _error; String? _error;
List<Quote> get quotes => _quotes; List<Quote> get quotes => _quotes;
List<Quote> get filteredQuotes => _filteredQuotes;
Set<String> get authors => _authors; Set<String> get authors => _authors;
bool get loading => _loading; bool get loading => _loading;
String? get error => _error; String? get error => _error;
List<Quote> get filteredQuotes {
final query = filterController.text.toLowerCase();
return _quotes.where((quote) {
switch (filterBy) {
case 0:
return quote.author.toLowerCase().contains(query);
case 1:
return quote.body.toLowerCase().contains(query);
default:
return true;
}
}).toList();
}
final QuoteRepository repo = QuoteRepository(); final QuoteRepository repo = QuoteRepository();
@@ -39,21 +52,6 @@ class QuoteProvider extends ChangeNotifier {
} }
void filterQuotes() { 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(); notifyListeners();
} }