refactored search function using provider
This commit is contained in:
@@ -1,16 +1,24 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../../model/bookmark.dart';
|
||||
import '../../service/notifying.dart';
|
||||
import '../../service/search_provider.dart';
|
||||
import '../../service/storage.dart' show Storage;
|
||||
import '../../service/url_launcher.dart';
|
||||
|
||||
class SearchResultsWidget extends StatelessWidget {
|
||||
const SearchResultsWidget({super.key, required this.bookmarks});
|
||||
class SearchResultsWidget extends StatefulWidget {
|
||||
const SearchResultsWidget({super.key});
|
||||
|
||||
final Iterable<Bookmark> bookmarks;
|
||||
@override
|
||||
State<SearchResultsWidget> createState() => _SearchResultsWidgetState();
|
||||
}
|
||||
|
||||
class _SearchResultsWidgetState extends State<SearchResultsWidget> {
|
||||
final List<Bookmark> allBookmarks = Storage.loadBookmarks();
|
||||
|
||||
Widget bookmarkListItemBuilder(BuildContext context, int index) {
|
||||
final bookmark = bookmarks.elementAt(index);
|
||||
final bookmark = filteredBookmarks.elementAt(index);
|
||||
return ListTile(
|
||||
title: Text(bookmark.name),
|
||||
onTap: () => launchUrlFromString(bookmark.link).then((errorCode) {
|
||||
@@ -25,12 +33,18 @@ class SearchResultsWidget extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (bookmarks.isNotEmpty) {
|
||||
if (filteredBookmarks.isNotEmpty) {
|
||||
return ListView.builder(
|
||||
itemBuilder: bookmarkListItemBuilder,
|
||||
itemCount: bookmarks.length,
|
||||
itemCount: filteredBookmarks.length,
|
||||
);
|
||||
}
|
||||
return Center(child: Text('Start searching'));
|
||||
}
|
||||
|
||||
Iterable<Bookmark> get filteredBookmarks => allBookmarks.where(
|
||||
(bookmark) => bookmark.name.toLowerCase().contains(
|
||||
context.watch<SearchProvider>().searchText.toLowerCase(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user