import 'dart:io'; import 'package:url_launcher/url_launcher.dart'; class URLLauncher { static Future 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 sendEmail( {required String email, String subject = '', body = ''}) async { String? encodeQueryParameters(Map params) { return params.entries .map((MapEntry e) => '${Uri.encodeComponent(e.key)}=${Uri.encodeComponent(e.value)}') .join('&'); } final emailLaunchUri = Uri( scheme: 'mailto', path: email, query: encodeQueryParameters({ subject: body, }), ); try { bool canLaunch = await canLaunchUrl(emailLaunchUri); if (canLaunch) { await launchUrl(emailLaunchUri); } } catch (err) { throw 'Could not launch $emailLaunchUri'; } } static Future 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()}'; } } }