working on filtering
This commit is contained in:
@@ -48,9 +48,9 @@ class ListPage extends StatelessWidget {
|
||||
Expanded(
|
||||
child: ListView.builder(
|
||||
padding: EdgeInsets.all(Constants.edgeInset),
|
||||
itemCount: provider.quotes.length,
|
||||
itemCount: provider.filteredQuotes.length,
|
||||
itemBuilder: (context, index) {
|
||||
final quote = provider.quotes[index];
|
||||
final quote = provider.filteredQuotes[index];
|
||||
|
||||
return
|
||||
ListTile(
|
||||
@@ -211,7 +211,6 @@ class ListPage extends StatelessWidget {
|
||||
}
|
||||
|
||||
Widget filterBar(BuildContext context, QuoteProvider provider) {
|
||||
int filterBy = 0; // 0 = by author, 1 = by quote
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(Constants.edgeInset),
|
||||
child: Row(
|
||||
@@ -231,7 +230,7 @@ class ListPage extends StatelessWidget {
|
||||
IconButton(
|
||||
icon: Icon(Icons.search),
|
||||
onPressed: () {
|
||||
// TODO: filtering
|
||||
provider.filterQuotes;
|
||||
},
|
||||
),
|
||||
IconButton(
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user