openZaloChat static method

Future<bool> openZaloChat(
  1. String phoneNumber, {
  2. BuildContext? context,
})

Mở Zalo chat

Implementation

static Future<bool> openZaloChat(
  String phoneNumber, {
  BuildContext? context,
}) async {
  try {
    final cleanNumber = _cleanPhoneNumber(phoneNumber);

    if (cleanNumber.isEmpty) {
      if (context != null && context.mounted) {
        _showError(context, 'Số điện thoại không hợp lệ');
      }
      return false;
    }

    // Convert to international format for Zalo
    String zaloNumber = cleanNumber;
    if (zaloNumber.startsWith('0')) {
      zaloNumber = '84${zaloNumber.substring(1)}'; // Vietnam country code
    } else if (!zaloNumber.startsWith('84') && !zaloNumber.startsWith('+')) {
      zaloNumber = '84$zaloNumber';
    }

    // Remove + if exists
    zaloNumber = zaloNumber.replaceAll('+', '');

    // Try deep link first (zalo://qr/p/)
    final deepLinkUri = Uri.parse('zalo://qr/p/$zaloNumber');
    if (await canLaunchUrl(deepLinkUri)) {
      await launchUrl(deepLinkUri, mode: LaunchMode.externalApplication);
      return true;
    }

    // Fallback to web link
    final webUri = Uri.parse('https://zalo.me/$zaloNumber');
    if (await canLaunchUrl(webUri)) {
      await launchUrl(webUri, mode: LaunchMode.externalApplication);
      return true;
    }

    if (context != null && context.mounted) {
      _showError(context, 'Không thể mở Zalo. Vui lòng cài đặt Zalo app.');
    }
    return false;
  } catch (e) {
    if (context != null && context.mounted) {
      _showError(context, 'Lỗi: $e');
    }
    return false;
  }
}