Compare commits
3 Commits
c0f92fac58
...
143206cd72
| Author | SHA1 | Date | |
|---|---|---|---|
| 143206cd72 | |||
| 36e4eaee17 | |||
| 11a43a39fa |
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'pages/collection_page.dart';
|
||||
import 'pages/collections_list_page.dart';
|
||||
import 'pages/search_page.dart';
|
||||
import 'service/shared_link_provider.dart';
|
||||
import 'service/storage.dart';
|
||||
import 'service/share_intent_service.dart';
|
||||
@@ -68,6 +69,7 @@ class _MapsBookmarksState extends State<MapsBookmarks>
|
||||
routes: {
|
||||
CollectionsListPage.routeName: (context) => const CollectionsListPage(),
|
||||
CollectionPage.routeName: (context) => const CollectionPage(),
|
||||
SearchPage.routeName: (context) => const SearchPage(),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import 'package:provider/provider.dart';
|
||||
import '../model/bookmark.dart';
|
||||
import '../model/maps_link_metadata.dart';
|
||||
import '../service/bookmarks_provider.dart';
|
||||
import '../service/notifying.dart';
|
||||
import '../service/shared_link_provider.dart';
|
||||
import '../service/storage.dart';
|
||||
import '../service/url_launcher.dart';
|
||||
@@ -62,8 +63,8 @@ class _CollectionPageState extends State<CollectionPage> {
|
||||
return ListTile(
|
||||
title: Text(bookmark.name),
|
||||
onTap: () => launchUrlFromString(bookmark.link).then((errorCode) {
|
||||
if (context.mounted && errorCode != UrlLaunchErrorCode.none) {
|
||||
return showUrlError(context, errorCode);
|
||||
if (context.mounted) {
|
||||
return Notifying.showUrlErrorSnackbar(context, errorCode);
|
||||
}
|
||||
}),
|
||||
onLongPress: () => editBookmark(bookmark),
|
||||
@@ -109,16 +110,4 @@ class _CollectionPageState extends State<CollectionPage> {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void showUrlError(BuildContext context, UrlLaunchErrorCode errorCode) {
|
||||
String errorText = '';
|
||||
if (errorCode == UrlLaunchErrorCode.couldNotLaunch) {
|
||||
errorText = 'Could not launch Url';
|
||||
} else {
|
||||
errorText = 'Invalid Url';
|
||||
}
|
||||
ScaffoldMessenger.of(
|
||||
context,
|
||||
).showSnackBar(SnackBar(content: Text(errorText)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import '../service/shared_link_provider.dart';
|
||||
import '../service/storage.dart';
|
||||
import '../widgets/create_bookmark_collection_dialog.dart';
|
||||
import 'collection_page.dart';
|
||||
import 'search_page.dart' show SearchPage;
|
||||
|
||||
class CollectionsListPage extends StatefulWidget {
|
||||
const CollectionsListPage({super.key});
|
||||
@@ -88,6 +89,12 @@ class _CollectionsListPageState extends State<CollectionsListPage> {
|
||||
TextButton(
|
||||
onPressed: () => provider.removeCurrentMapsLink(),
|
||||
child: Text('Cancel'),
|
||||
)
|
||||
else
|
||||
IconButton(
|
||||
onPressed: () =>
|
||||
Navigator.of(context).pushNamed(SearchPage.routeName),
|
||||
icon: Icon(Icons.search),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
43
lib/pages/search_page.dart
Normal file
43
lib/pages/search_page.dart
Normal file
@@ -0,0 +1,43 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../model/bookmark.dart';
|
||||
import '../service/storage.dart';
|
||||
import '../widgets/search_widgets/search_bar_widget.dart';
|
||||
import '../widgets/search_widgets/search_results_widget.dart';
|
||||
|
||||
class SearchPage extends StatefulWidget {
|
||||
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(
|
||||
appBar: AppBar(title: Text('Search')),
|
||||
body: Column(
|
||||
children: [
|
||||
SearchBarWidget(
|
||||
onEditingComplete: (searchString) => setState(() {
|
||||
this.searchString = searchString;
|
||||
}),
|
||||
),
|
||||
Expanded(
|
||||
child: SearchResultsWidget(
|
||||
bookmarks: allBookmarks.where(
|
||||
(bookmark) => bookmark.name.contains(searchString),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
37
lib/service/notifying.dart
Normal file
37
lib/service/notifying.dart
Normal file
@@ -0,0 +1,37 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'url_launcher.dart' show UrlLaunchErrorCode;
|
||||
|
||||
class Notifying {
|
||||
static void showSnackbar(
|
||||
BuildContext context, {
|
||||
required String text,
|
||||
bool isError = false,
|
||||
}) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
text,
|
||||
style: isError
|
||||
? TextStyle(color: Theme.of(context).colorScheme.error)
|
||||
: null,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
static void showUrlErrorSnackbar(
|
||||
BuildContext context,
|
||||
UrlLaunchErrorCode errorCode,
|
||||
) {
|
||||
String errorText = '';
|
||||
if (errorCode == UrlLaunchErrorCode.none) {
|
||||
return;
|
||||
} else if (errorCode == UrlLaunchErrorCode.couldNotLaunch) {
|
||||
errorText = 'Could not launch Url';
|
||||
} else {
|
||||
errorText = 'Invalid Url';
|
||||
}
|
||||
showSnackbar(context, text: errorText, isError: true);
|
||||
}
|
||||
}
|
||||
16
lib/widgets/search_widgets/search_bar_widget.dart
Normal file
16
lib/widgets/search_widgets/search_bar_widget.dart
Normal file
@@ -0,0 +1,16 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class SearchBarWidget extends StatelessWidget {
|
||||
const SearchBarWidget({super.key, required this.onEditingComplete});
|
||||
|
||||
final Function(String searchString) onEditingComplete;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
TextEditingController searchBarController = TextEditingController();
|
||||
return TextField(
|
||||
controller: searchBarController,
|
||||
onEditingComplete: () => onEditingComplete(searchBarController.text),
|
||||
);
|
||||
}
|
||||
}
|
||||
36
lib/widgets/search_widgets/search_results_widget.dart
Normal file
36
lib/widgets/search_widgets/search_results_widget.dart
Normal file
@@ -0,0 +1,36 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../model/bookmark.dart';
|
||||
import '../../service/notifying.dart';
|
||||
import '../../service/url_launcher.dart';
|
||||
|
||||
class SearchResultsWidget extends StatelessWidget {
|
||||
const SearchResultsWidget({super.key, required this.bookmarks});
|
||||
|
||||
final Iterable<Bookmark> bookmarks;
|
||||
|
||||
Widget bookmarkListItemBuilder(BuildContext context, int index) {
|
||||
final bookmark = bookmarks.elementAt(index);
|
||||
return ListTile(
|
||||
title: Text(bookmark.name),
|
||||
onTap: () => launchUrlFromString(bookmark.link).then((errorCode) {
|
||||
if (context.mounted && errorCode != UrlLaunchErrorCode.none) {
|
||||
Notifying.showUrlErrorSnackbar(context, errorCode);
|
||||
} else if (context.mounted) {
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (bookmarks.isNotEmpty) {
|
||||
return ListView.builder(
|
||||
itemBuilder: bookmarkListItemBuilder,
|
||||
itemCount: bookmarks.length,
|
||||
);
|
||||
}
|
||||
return Center(child: Text('Start searching'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user