buildUri static method

Uri buildUri({
  1. required LaunchType type,
  2. String? value,
  3. String? emailSubject,
  4. String? emailBody,
  5. String? smsBody,
})

Builds a Uri based on the specified type and input parameters.

type - The category of launch (e.g., website, phone, email). value - The primary data string (e.g., URL, phone number, email address). emailSubject - Optional subject for email. emailBody - Optional body content for email. smsBody - Optional pre-filled text for SMS.

Implementation

static Uri buildUri({
  required LaunchType type,
  String? value,
  String? emailSubject,
  String? emailBody,
  String? smsBody,
}) {
  switch (type) {
    case LaunchType.whatsapp:
      // Value should be phone number with country code (no +)
      final cleanPhone = value?.replaceAll(RegExp(r'\D'), '') ?? '';
      return Uri.parse('https://wa.me/$cleanPhone');

    case LaunchType.phone:
      final cleanPhone = value?.replaceAll(RegExp(r'\s+'), '') ?? '';
      return Uri(scheme: 'tel', path: cleanPhone);

    case LaunchType.email:
      return Uri(
        scheme: 'mailto',
        path: value ?? '',
        query: _buildQuery({'subject': emailSubject, 'body': emailBody}),
      );

    case LaunchType.sms:
      return Uri(
        scheme: 'sms',
        path: value ?? '',
        query: _buildQuery({'body': smsBody}),
      );

    case LaunchType.website:
      if (value == null) return Uri();
      var url = value;
      if (!url.startsWith('http://') && !url.startsWith('https://')) {
        url = 'https://$url';
      }
      return Uri.parse(url);

    case LaunchType.map:
      // Simplified map URL (lat,long or address)
      return Uri.parse(
        'https://www.google.com/maps/search/?api=1&query=${Uri.encodeComponent(value ?? '')}',
      );

    case LaunchType.custom:
      return Uri.parse(value ?? '');
  }
}