getLink method

String getLink(
  1. String phoneNumber,
  2. String? message,
  3. bool? shortLink,
  4. List<String>? bold,
  5. List<String>? italic,
  6. List<String>? strikethrough,
  7. List<String>? monospace,
)

Implementation

String getLink(
  String phoneNumber,
  String? message,
  bool? shortLink,
  List<String>? bold,
  List<String>? italic,
  List<String>? strikethrough,
  List<String>? monospace,
) {
  // Helper function to apply formatting on text based on symbol parameter
  String applyFormatting(String text, String symbol) {
    return '$symbol$text$symbol';
  }

  // Apply formatting based on parameters
  var body = message ?? '';

  if (bold != null) {
    for (var word in bold) {
      body = body.replaceAll(word, applyFormatting(word, '*'));
    }
  }

  if (italic != null) {
    for (var word in italic) {
      body = body.replaceAll(word, applyFormatting(word, '_'));
    }
  }

  if (strikethrough != null) {
    for (var word in strikethrough) {
      body = body.replaceAll(word, applyFormatting(word, '~'));
    }
  }

  if (monospace != null) {
    for (var word in monospace) {
      body = body.replaceAll(word, applyFormatting(word, '```'));
    }
  }

  // Remove '+' from phone number
  phoneNumber = phoneNumber.replaceAll("+", "");

  // Generate the appropriate link
  if (shortLink != null && shortLink) {
    return 'https://wa.me/$phoneNumber?text=${Uri.encodeComponent(body)}';
  } else {
    return 'https://api.whatsapp.com/send?phone=$phoneNumber&text=${Uri.encodeComponent(body)}';
  }
}