Files
syncrow-app/lib/utils/helpers/url_launcher.dart
2024-02-17 16:27:27 +03:00

77 lines
2.1 KiB
Dart

import 'dart:io';
import 'package:url_launcher/url_launcher.dart';
class URLLauncher {
static Future<void> sendWhatsapp(
{String phoneNumber = '971581497062',
String text = 'Hi%2C%20I%20would%20like%20to',
String from = ''}) async {
Uri launchUri;
launchUri = Uri.parse("whatsapp://send?phone=%2B$phoneNumber&text=$text");
// Launch the App
try {
// SegmentEvents.sendTrackEvent(SegmentEventModel(
// eventName: 'Whatsapp opened', properties: {'From': from}));
//
// if (await canLaunchUrl(launchUri) == false) {
// CustomSnackBar.displaySnackBar('Could not open whatsapp');
// return;
// }
if (Platform.isAndroid) {
await launch(
launchUri.toString(),
);
} else {
await launchUrl(
launchUri,
);
}
} catch (error) {
// CustomSnackBar.displaySnackBar('Could not open whatsapp');
throw 'Could not launch $phoneNumber';
}
}
static Future<void> sendEmail(
{required String email, String subject = '', body = ''}) async {
String? encodeQueryParameters(Map<String, String> params) {
return params.entries
.map((MapEntry<String, String> e) =>
'${Uri.encodeComponent(e.key)}=${Uri.encodeComponent(e.value)}')
.join('&');
}
final emailLaunchUri = Uri(
scheme: 'mailto',
path: email,
query: encodeQueryParameters(<String, String>{
subject: body,
}),
);
try {
bool canLaunch = await canLaunchUrl(emailLaunchUri);
if (canLaunch) {
await launchUrl(emailLaunchUri);
}
} catch (err) {
throw 'Could not launch $emailLaunchUri';
}
}
static Future<void> launch(String url) async {
Uri parsedUrl = Uri.parse(url);
try {
bool canLaunch = await canLaunchUrl(parsedUrl);
if (canLaunch) {
await launchUrl(
parsedUrl,
// mode: LaunchMode.externalApplication,
);
}
} catch (err) {
throw 'Url launcher error: ${err.toString()}';
}
}
}