encapsulated snackbar call on error
This commit is contained in:
@@ -4,6 +4,7 @@ import 'package:provider/provider.dart';
|
|||||||
import '../model/bookmark.dart';
|
import '../model/bookmark.dart';
|
||||||
import '../model/maps_link_metadata.dart';
|
import '../model/maps_link_metadata.dart';
|
||||||
import '../service/bookmarks_provider.dart';
|
import '../service/bookmarks_provider.dart';
|
||||||
|
import '../service/notifying.dart';
|
||||||
import '../service/shared_link_provider.dart';
|
import '../service/shared_link_provider.dart';
|
||||||
import '../service/storage.dart';
|
import '../service/storage.dart';
|
||||||
import '../service/url_launcher.dart';
|
import '../service/url_launcher.dart';
|
||||||
@@ -62,8 +63,8 @@ class _CollectionPageState extends State<CollectionPage> {
|
|||||||
return ListTile(
|
return ListTile(
|
||||||
title: Text(bookmark.name),
|
title: Text(bookmark.name),
|
||||||
onTap: () => launchUrlFromString(bookmark.link).then((errorCode) {
|
onTap: () => launchUrlFromString(bookmark.link).then((errorCode) {
|
||||||
if (context.mounted && errorCode != UrlLaunchErrorCode.none) {
|
if (context.mounted) {
|
||||||
return showUrlError(context, errorCode);
|
return Notifying.showUrlErrorSnackbar(context, errorCode);
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
onLongPress: () => editBookmark(bookmark),
|
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)));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user