makeZaloCall static method

Future<bool> makeZaloCall(
  1. String phoneNumber, {
  2. BuildContext? context,
  3. bool showConfirmation = false,
})

Gọi điện qua Zalo

Implementation

static Future<bool> makeZaloCall(
  String phoneNumber, {
  BuildContext? context,
  bool showConfirmation = false,
}) 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;
    }

    // Show confirmation dialog if needed
    if (showConfirmation && context != null) {
      final confirmed = await _showZaloConfirmDialog(context, cleanNumber);
      if (!confirmed) return false;
    }

    // Convert to international format
    String zaloNumber = cleanNumber;
    if (zaloNumber.startsWith('0')) {
      zaloNumber = '84${zaloNumber.substring(1)}';
    } else if (!zaloNumber.startsWith('84') && !zaloNumber.startsWith('+')) {
      zaloNumber = '84$zaloNumber';
    }
    zaloNumber = zaloNumber.replaceAll('+', '');

    // Zalo call deep link
    final uri = Uri.parse('zalo://call/$zaloNumber');

    if (await canLaunchUrl(uri)) {
      await launchUrl(uri, mode: LaunchMode.externalApplication);
      return true;
    } else {
      // Fallback: open chat instead
      if (context != null && context.mounted) {
        _showInfo(context, 'Không thể gọi trực tiếp. Đang mở Zalo chat...');
      }
      // ignore: use_build_context_synchronously
      return await openZaloChat(phoneNumber, context: context);
    }
  } catch (e) {
    if (context != null && context.mounted) {
      _showError(context, 'Lỗi: $e');
    }
    return false;
  }
}