working on filtering
This commit is contained in:
@@ -48,9 +48,9 @@ class ListPage extends StatelessWidget {
|
|||||||
Expanded(
|
Expanded(
|
||||||
child: ListView.builder(
|
child: ListView.builder(
|
||||||
padding: EdgeInsets.all(Constants.edgeInset),
|
padding: EdgeInsets.all(Constants.edgeInset),
|
||||||
itemCount: provider.quotes.length,
|
itemCount: provider.filteredQuotes.length,
|
||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, index) {
|
||||||
final quote = provider.quotes[index];
|
final quote = provider.filteredQuotes[index];
|
||||||
|
|
||||||
return
|
return
|
||||||
ListTile(
|
ListTile(
|
||||||
@@ -211,7 +211,6 @@ class ListPage extends StatelessWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Widget filterBar(BuildContext context, QuoteProvider provider) {
|
Widget filterBar(BuildContext context, QuoteProvider provider) {
|
||||||
int filterBy = 0; // 0 = by author, 1 = by quote
|
|
||||||
return Padding(
|
return Padding(
|
||||||
padding: const EdgeInsets.all(Constants.edgeInset),
|
padding: const EdgeInsets.all(Constants.edgeInset),
|
||||||
child: Row(
|
child: Row(
|
||||||
@@ -231,7 +230,7 @@ class ListPage extends StatelessWidget {
|
|||||||
IconButton(
|
IconButton(
|
||||||
icon: Icon(Icons.search),
|
icon: Icon(Icons.search),
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
// TODO: filtering
|
provider.filterQuotes;
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
IconButton(
|
IconButton(
|
||||||
|
|||||||
@@ -7,17 +7,20 @@ import 'package:frontend/repository/quote_repository.dart';
|
|||||||
|
|
||||||
class QuoteProvider extends ChangeNotifier {
|
class QuoteProvider extends ChangeNotifier {
|
||||||
List<Quote> _quotes = [];
|
List<Quote> _quotes = [];
|
||||||
|
List<Quote> _filteredQuotes = [];
|
||||||
Set<String> _authors = {};
|
Set<String> _authors = {};
|
||||||
bool _loading = false;
|
bool _loading = false;
|
||||||
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;
|
||||||
|
|
||||||
final QuoteRepository repo = QuoteRepository();
|
final QuoteRepository repo = QuoteRepository();
|
||||||
|
|
||||||
|
int filterBy = 0; // 0 = by author, 1 = by quote
|
||||||
var filterController = TextEditingController(text: '');
|
var filterController = TextEditingController(text: '');
|
||||||
|
|
||||||
void clearFilterController() {
|
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 {
|
Future<void> readQuotes() async {
|
||||||
_loading = true;
|
_loading = true;
|
||||||
_error = null;
|
_error = null;
|
||||||
@@ -42,6 +64,7 @@ class QuoteProvider extends ChangeNotifier {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
_quotes = await repo.readQuotes();
|
_quotes = await repo.readQuotes();
|
||||||
|
_filteredQuotes = _quotes;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
_error = e.toString();
|
_error = e.toString();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user