openWhatsApp static method
WhatsApp chat
Implementation
static Future<bool> openWhatsApp(
String phoneNumber, {
String? message,
BuildContext? context,
}) async {
try {
final cleanNumber = _cleanPhoneNumber(phoneNumber);
// Remove leading 0 if exists and add country code if needed
String whatsappNumber = cleanNumber;
if (whatsappNumber.startsWith('0')) {
whatsappNumber =
'84${whatsappNumber.substring(1)}'; // Vietnam country code
}
String whatsappUri = 'https://wa.me/$whatsappNumber';
if (message != null && message.isNotEmpty) {
whatsappUri += '?text=${Uri.encodeComponent(message)}';
}
final uri = Uri.parse(whatsappUri);
if (await canLaunchUrl(uri)) {
await launchUrl(uri, mode: LaunchMode.externalApplication);
return true;
} else {
if (context != null && context.mounted) {
_showError(context, 'Không thể mở WhatsApp');
}
return false;
}
} catch (e) {
if (context != null && context.mounted) {
_showError(context, 'Lỗi: $e');
}
return false;
}
}