diff --git a/lib/pages/collection_page.dart b/lib/pages/collection_page.dart index 32a85bb..cf8432a 100644 --- a/lib/pages/collection_page.dart +++ b/lib/pages/collection_page.dart @@ -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 { 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 { ), ); } - - 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))); - } } diff --git a/lib/service/notifying.dart b/lib/service/notifying.dart new file mode 100644 index 0000000..7c4dca1 --- /dev/null +++ b/lib/service/notifying.dart @@ -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); + } +}