refactored search function using provider
This commit is contained in:
@@ -3,6 +3,7 @@ import 'package:provider/provider.dart';
|
||||
import 'pages/collection_page.dart';
|
||||
import 'pages/collections_list_page.dart';
|
||||
import 'pages/search_page.dart';
|
||||
import 'service/search_provider.dart';
|
||||
import 'service/shared_link_provider.dart';
|
||||
import 'service/storage.dart';
|
||||
import 'service/share_intent_service.dart';
|
||||
@@ -12,8 +13,11 @@ void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await Storage.initialize();
|
||||
runApp(
|
||||
ChangeNotifierProvider(
|
||||
create: (_) => SharedLinkProvider(),
|
||||
MultiProvider(
|
||||
providers: [
|
||||
ChangeNotifierProvider(create: (_) => SharedLinkProvider()),
|
||||
ChangeNotifierProvider(create: (_) => SearchProvider()),
|
||||
],
|
||||
child: const MapsBookmarks(),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -1,23 +1,14 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../model/bookmark.dart';
|
||||
import '../service/storage.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../service/search_provider.dart';
|
||||
import '../widgets/search_widgets/search_bar_widget.dart';
|
||||
import '../widgets/search_widgets/search_results_widget.dart';
|
||||
|
||||
class SearchPage extends StatefulWidget {
|
||||
class SearchPage extends StatelessWidget {
|
||||
const SearchPage({super.key});
|
||||
|
||||
static const String routeName = '/search';
|
||||
|
||||
@override
|
||||
State<SearchPage> createState() => _SearchPageState();
|
||||
}
|
||||
|
||||
class _SearchPageState extends State<SearchPage> {
|
||||
final List<Bookmark> allBookmarks = Storage.loadBookmarks();
|
||||
String searchString = '';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
@@ -25,17 +16,9 @@ class _SearchPageState extends State<SearchPage> {
|
||||
body: Column(
|
||||
children: [
|
||||
SearchBarWidget(
|
||||
onEditingComplete: (searchString) => setState(() {
|
||||
this.searchString = searchString;
|
||||
}),
|
||||
),
|
||||
Expanded(
|
||||
child: SearchResultsWidget(
|
||||
bookmarks: allBookmarks.where(
|
||||
(bookmark) => bookmark.name.contains(searchString),
|
||||
),
|
||||
),
|
||||
onEditingComplete: context.read<SearchProvider>().setSearchText,
|
||||
),
|
||||
Expanded(child: SearchResultsWidget()),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
12
lib/service/search_provider.dart
Normal file
12
lib/service/search_provider.dart
Normal file
@@ -0,0 +1,12 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class SearchProvider extends ChangeNotifier {
|
||||
String _searchText = '';
|
||||
|
||||
void setSearchText(String searchText, {bool silent = false}) {
|
||||
_searchText = searchText;
|
||||
if (!silent) notifyListeners();
|
||||
}
|
||||
|
||||
String get searchText => _searchText;
|
||||
}
|
||||
@@ -7,10 +7,10 @@ class SearchBarWidget extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
TextEditingController searchBarController = TextEditingController();
|
||||
return TextField(
|
||||
controller: searchBarController,
|
||||
onEditingComplete: () => onEditingComplete(searchBarController.text),
|
||||
);
|
||||
return TextField(onChanged: (text) => onChanged(text, context));
|
||||
}
|
||||
|
||||
void onChanged(String text, BuildContext context) {
|
||||
if (context.mounted) onEditingComplete(text);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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